Java O(n) solution, concise and easy to understand


  • 3
    R
    public List<Integer> getRow(int rowIndex) {
    		List<Integer> row = null, pre = null;
    		for (int i = 0; i <= rowIndex; i++) {
    			row = new ArrayList<>();
    			for (int j = 0; j <= i; j++) {
    				row.add(j == 0 || i == j ? 1 : pre.get(j) + pre.get(j-1));
    			}
    			pre = row;
    		}
    		return row;
    	}

Log in to reply
 

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