leetcode_119_Pascal's Triangle II

来源:互联网 发布:app软件大全 编辑:程序博客网 时间:2024/04/30 09:12

麻烦各位朋友帮忙顶一下增加人气,如有错误或疑问请留言纠正,谢谢微笑


Pascal's Triangle II

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?


//方法一:自测Acceptedclass Solution {public:    vector<int> getRow(int rowIndex) {//创建rowIndex+2个0        vector<int> ans(rowIndex+2,0);vector<int> temp(rowIndex+2,0);ans[1]=1;//first rowfor(int i=1; i<=rowIndex; i++){temp = ans;for(int j=1; j<=i+1; j++)ans[j] = temp[j] +temp[j-1];}//删除第一个0ans.erase(ans.begin());for(int i=0; i<rowIndex; i++)cout<<ans[i];cout<<endl;return ans;    }};

//vs2012测试代码#include<iostream>#include<vector>using namespace std;class Solution {public:    vector<int> getRow(int rowIndex) {//创建rowIndex+2个0        vector<int> ans(rowIndex+2,0);vector<int> temp(rowIndex+2,0);ans[1]=1;//first rowfor(int i=1; i<=rowIndex; i++){temp = ans;for(int j=1; j<=i+1; j++)ans[j] = temp[j] +temp[j-1];}//删除第一个0ans.erase(ans.begin());for(int i=0; i<=rowIndex; i++)cout<<ans[i];cout<<endl;return ans;    }};int main(){int num;cin>>num;Solution lin;lin.getRow(num);return 0;}


1 0
原创粉丝点击