LeetCode Pascal's Triangle II

来源:互联网 发布:手机淘宝一阳指在哪里 编辑:程序博客网 时间:2024/06/05 10:34

Given an index k, return the kth 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?


class Solution {public:vector<int> getRow(int rowIndex) {vector<int> ivec;if (rowIndex < 0)return ivec;else if (rowIndex == 0) {ivec.push_back(1);return ivec;}else {ivec.push_back(1);for (int i = 1; i <= rowIndex; i++) {for (int j = i -1; j > 0; j--) {ivec[j] += ivec[j - 1];}ivec.push_back(1);}return ivec;}}};


0 0
原创粉丝点击