Simple java solution using math


  • 2
    W

    Using math to solve this problem.

    C(a, b) = C(a, b-1) * (a-b+1) / b

    And C is symmetric.

    public List<Integer> getRow(int rowIndex) {
    	List<Integer> result = new ArrayList<Integer> ();
    	if (rowIndex < 0) return result;
    	
    	result.add(1);
    	for (int i = 1; i <= rowIndex/2; i++) {
    		long temp = result.get(i - 1);
    		temp *= rowIndex - i + 1;
    		temp /= i;
    		result.add((int) (temp));
    	}
        // C is symmetric
    	for (int i = rowIndex/2 + 1; i <= rowIndex; i++) {
    		result.add(result.get(rowIndex - i));
    	}
    	
    	return result;
    }

Log in to reply
 

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