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


  • 1
    S

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

    Reference: https://oj.leetcode.com/discuss/8364/here-is-my-brief-o-k-solution (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){
    		row.add(1);
    		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.