LeetCode --- Pascal's Triangle II

题目链接

题意:在杨辉三角中,给定整数k,输出第k行。

附上代码:

 1 class Solution {
 2 public:
 3     vector<int> getRow(int rowIndex) {
 4         vector<int> ans(rowIndex+1);
 5         // 注意第0行为1
 6         ans[0] = 1;
 7         for (int i = 1; i <= rowIndex; i++) {
 8             ans[i] = 1;
 9             for (int j = i-1; j > 0; j--) {
10                 ans[j] = ans[j] + ans[j-1];
11             }
12         }
13         return ans;
14     }
15 };

 

posted on 2014-05-09 12:24  Stomach_ache  阅读(114)  评论(0)    收藏  举报

导航