1ms solution in Java


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

Log in to reply
 

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