119. Pascal's Triangle II

Total Accepted: 66540
Total Submissions: 211819
Difficulty: Easy

Given an index _k_, return the _k_th row of the Pascal’s triangle.

For example, given _k_ = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only _O_(_k_) extra space?

Java:

1
2
3
4
5
6
7
8
9
10
11
public class Solution {
    public List<Integer> getRow(int rowIndex) {
        Integer[] arr = new Integer[rowIndex + 1];
        int end = (rowIndex + 2) >> 1;
        arr[0] = arr[rowIndex] = 1;
        long j = rowIndex;
        for (int i = 1; i < end; ++i, --j)
            arr[i] = arr[rowIndex - i] = (int) (j * arr[i - 1] / i);
        return Arrays.asList(arr);
    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。