Leetcode 119 Pascal's Triangle II 数论递推

杨辉三角,这次要输出第rowIndex行

用滚动数组t进行递推

 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; 

 1 class Solution {
 2 public:
 3     vector<int> getRow(int rowIndex) {
 4         if(rowIndex < 2) return vector<int>(rowIndex + 1,1);
 5         int n = rowIndex;
 6         vector<int> t[2];
 7         for(int i = 0; i < 2; ++i){
 8            t[i].resize(n + 1, 1);
 9         }
10         for(int i = 2; i <= n; ++i){
11             for(int j = 1; j < i; ++j){
12                 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1];   
13             }
14         }
15         return t[(n+1) % 2];
16     }
17 };

 

posted @ 2016-03-07 20:13  Breeze0806  阅读(129)  评论(0编辑  收藏  举报