【leetcode】119. Pascal's Triangle II
程序员文章站
2022-04-01 12:08:29
...
题目:
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal’s triangle.
Note that the row index starts from 0.
In Pascal’s triangle, each number is the sum of the two numbers directly above it.
思路:
直接看图就好。规律很明显。
代码实现:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+1, 1);
if (rowIndex <= 1){
return ans;
}
for (int i = 2; i <= rowIndex; ++i){
int pre = 1;
for (int j = 1; j < i; ++j){
int t = ans[j];
ans[j] += pre;
pre = t;
}
}
return ans;
}
};
上一篇: ps阈值主要用来做什么?
下一篇: ps中什么是位图
推荐阅读
-
Leetcode No.119 Pascal's Triangle II(c++实现)
-
杨辉三角(pascal's triangle)
-
LeetCode-119. Pascal's Triangle II
-
119. Pascal's Triangle II [杨辉三角形2]
-
119. Pascal's Triangle II
-
leetcode -- 119. Pascal's Triangle II
-
2018.05.03 leetcode #119. Pascal's Triangle II
-
【leetcode】119. Pascal's Triangle II
-
LeetCode 119. Pascal's Triangle II
-
[LeetCode]119. Pascal's Triangle II