My easy-understand Java solution 188ms


  • 0
    W
    public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> tri = new ArrayList<List<Integer>>();
        if (numRows<1) return tri;
        for(int i=0;i<numRows;i++){
            List<Integer> li = new ArrayList<Integer>();
            tri.add(li);
        }
      //work start here
        tri.get(0).add(1);
        for(int i=1;i<numRows;i++){
            tri.get(i).add(1);
            for(int j=0;j<i-1;j++){
                int temp = tri.get(i-1).get(j)+tri.get(i-1).get(j+1);
                tri.get(i).add(temp);
            }
            tri.get(i).add(1);
        }
        return tri;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.