LeetCode Solutions
119. Pascal's Triangle II
Time: $O(n^2)$ Space: $O(n)$
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex + 1, 1);
for (int i = 2; i < rowIndex + 1; ++i)
for (int j = 1; j < i; ++j)
ans[i - j] += ans[i - j - 1];
return ans;
}
};
class Solution {
public List<Integer> getRow(int rowIndex) {
Integer[] ans = new Integer[rowIndex + 1];
Arrays.fill(ans, 1);
for (int i = 2; i < rowIndex + 1; ++i)
for (int j = 1; j < i; ++j)
ans[i - j] += ans[i - j - 1];
return Arrays.asList(ans);
}
}
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
ans = [1] * (rowIndex + 1)
for i in range(2, rowIndex + 1):
for j in range(1, i):
ans[i - j] += ans[i - j - 1]
return ans