My java solution, any better code?


  • 0
    Y
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < numRows; i++)
            result.add(getRow(i));
        return result;
    }
    
    public List<Integer> getRow(int row) {
        List<Integer> result = new ArrayList<>();
    
        for (int i = 0; i < row + 1; i++) {
            result.add(0, 1);
            for (int j = 1; j < result.size() - 1; j++) {
                result.set(j, result.get(j) + result.get(j + 1));
            }
        }
    
        return result;
    }

Log in to reply
 

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