Beat 100% Fast Easy O(1) space Java Solution


  • -2
    M
    public List<Integer> getRow(int rowIndex) {
    
    	List<Integer> ans = new LinkedList<Integer>();
    
    	int[] pascal = new int[rowIndex + 1];
    
    	pascal[0] = 1;
    	int current, previous;
    	for (int row = 1; row <= rowIndex; row++) {
    		previous = 1;
    		for (int pos = 1; pos <= row; pos++) {
    			current = pascal[pos];
    			pascal[pos] = previous + pascal[pos];
    			previous = current;
    		}
    	}
    	for (int temp : pascal)
    		ans.add(temp);
    
    	return ans;
    
    }

  • 0
    J

    It's not O(1). It's O(k) at best.


Log in to reply
 

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