Solution in Java


  • -1
    G
    public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        List<Integer> prev = new ArrayList<Integer>();
        if(numRows == 0) return result;
        for(int i=0;i<numRows;i++){
            if(prev.size() == 0){
                List<Integer> temp = new ArrayList<Integer>();
                temp.add(1);
                result.add(temp);
                prev = temp;
            } else {
                List<Integer> temp = new ArrayList<Integer>();
                temp.add(1);
                for(int j=1; j<=i;j++){
                    if(j<i)temp.add(prev.get(j-1)+prev.get(j));
                        else temp.add(1);
                }
                result.add(temp);
                prev = temp;
            }
        }
        return result;
    }
    

    }


Log in to reply
 

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