118パスカルの三角形(二次元ベクトル)

リンク:https://leetcode-cn.com/problems/pascals-triangle/

numRowsの数ヤンホイトライアングルと世代前の非負整数numRowsの数、与えられました。

パスカルの三角形では、各番号は、その左上と右上の数です。

例:

入力:5
出力

     [ [1]、
    [1,1]、
   [1,2,1]、
  [1,3,3,1]、
 [1,4,6,4,1]
]

ベクトルの異なる大きさに各行の方法は、使い慣れた二次元ベクトル

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

 

公開された84元の記事 ウォン称賛7 ビュー10000 +

おすすめ

転載: blog.csdn.net/weixin_43569916/article/details/104225652