Easy to understand Java solution


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

Log in to reply
 

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