Simple Java solution O(n^2) time O(1) space


  • 1

    Based on the fact that
    C(line, i) = C(line, i-1) * (line - i + 1) / i

    public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> result = new ArrayList<>();
            for (int line = 1; line <= numRows; line++){
                List<Integer> list = new ArrayList<>();
                for (int i = 1,C=1; i <= line; i++){
                    list.add(C);
                    C = C * (line - i) / i;  
                }
                result.add(list);
            }
            return result;
        }
    

Log in to reply
 

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