Java solution, easy to understand.


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

    }


Log in to reply
 

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