Java AC solution


  • 0
    A
    public List<List<Integer>> generate(final int numRows) {
            if (numRows == 0) {
                return Collections.emptyList();
            } else if (numRows == 1) {
                return Arrays.asList(Arrays.asList(1));
            }
    
            List<List<Integer>> result = new ArrayList<>();
            result.add(Arrays.asList(1));
            result.add(Arrays.asList(1, 1));
    
            for (int i = 2; i < numRows; i++) {
                List<Integer> current = new ArrayList<>();
                List<Integer> previous = result.get(i-1);
                current.add(previous.get(0));
                for (int j = 0; j < previous.size() - 1; j++) {
                    current.add(previous.get(j) + previous.get(j+1));
                }
                current.add(previous.get(previous.size() - 1));
    
                result.add(current);
            }
    
            return result;
        }
    

Log in to reply
 

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