您的位置:首页 > 编程语言 > C语言/C++

Pascal's Triangle

2016-06-10 16:29 302 查看

c++

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> res;
if (numRows <= 0)
return res;
res.push_back(vector<int>(1, 1));
for (int i = 1; i < numRows; ++i) {
vector<int> tmp;
tmp.push_back(1);
for (int j = 0; j < i - 1; ++j) {
tmp.push_back(res[i-1][j] + res[i - 1][j + 1]);
}
tmp.push_back(1);
res.push_back(tmp);
}
return res;
}
};


python

class Solution(object):
def generate(self, numRows):
"""
:type numRows: int
:rtype: List[List[int]]
"""
if numRows <=0:
return []
res = []
a = [1]
res.append(a)
for x in xrange(numRows-1):
a = [sum(i) for i in zip([0] + a, a + [0])]
res.append(a)
return res
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言