Share neat Java solution, is it O(n^2)?

  • 1

    Calculate each row from end to beginning and put current row to result. Is this solution O(n^2) time complexity?

    Reference: (Pascal's Triangle II)

    public List<List<Integer>> generate(int numRows) {
    	List<List<Integer>> result = new ArrayList<List<Integer>> ();
    	List<Integer> row = new ArrayList<Integer>();
    	for(int i = 0; i < numRows; ++i){
    		result.add(new ArrayList<Integer>(row));
    		for(int j = i; j > 0; --j){
    			row.set(j, row.get(j)+row.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.