C++输出杨辉三角第K行:
class Solution{
public:
vector<int> getRow(int rowIndex){
vector<int> result;
for(int i = 0;i <= rowIndex; ++i){
result.push_back(1);
for(int j = i - 1; j > 0; --j){
result[j] += result[j-1];
}
}
return result;
}
}
python输出杨辉三角,使用生成器yield和zip函数。
https://www.jianshu.com/p/679a073d10c3
def triangles():
n = [1]
while True:
yield n
n = [x+y for x,y in zip([0] + n,n+[0])]
n = 0
for t in triangles():
print(t)
n = n + 1
if n == 10:
break