My concise Java Solution 1ms


  • 0
    R
    public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> triangle = new ArrayList<>();
            if(numRows <= 0){
                return triangle;
            }
            for(int i = 0; i < numRows; i++){
                List<Integer> list = new ArrayList<>();
                    for(int j = 0; j <= i; j++){
                        if(j== 0 || j == i){
                            list.add(1);
                        }
                        else {
                            list.add(triangle.get(i-1).get(j) + triangle.get(i-1).get(j-1));
                        }
                    }
                    triangle.add(list);
                
            }
            return triangle;
        }

Log in to reply
 

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