Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5, Return

[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]

Solution

public class Solution {
      public List<List<Integer>> generate(int numRows) {
        if (numRows < 1) return new ArrayList<>();

        List<Integer> cur = new ArrayList<>();
        cur.add(1);

        List<Integer> cur1 = new ArrayList<>();
        cur1.add(1);
        cur1.add(1);

        List<List<Integer>> result = new ArrayList<>();
        result.add(cur);
        result.add(cur1);

        if (numRows == 1) {
            return Arrays.asList(cur);
        } else if (numRows == 2) {
            return Arrays.asList(cur,cur1);
        } else {

            for (int i = 3; i <= numRows; i ++) {
                List<Integer> prev = result.get(i-2);

                List<Integer> newResult = new ArrayList<>();
                newResult.add(1);
                for(int j = 1; j < i -1; j ++) {
                    newResult.add(prev.get(j-1) + prev.get(j));
                }
                newResult.add(1);
                result.add(newResult);
            }
        }

        return result;
    }
}

results matching ""

    No results matching ""