输出杨辉三角

pascals-triangle

http://www.nowcoder.com/questionTerminal/f3f8f0f058b347ba8245cc90d0049d92

利用对称性简化:

//
// Created by jt on 2020/9/2.
//
#include <vector>
using namespace std;

class Solution {
public:
    /**
     *
     * @param numRows int整型
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > generate(int numRows) {
        // write code here
        vector<vector<int> > res;
        for (int i = 1; i <= numRows; ++i) {
            vector<int> vec(i, 1);
            for (int j = (i-1) / 2; j > 0; --j) {
                vec[j] = res[i-2][j] + res[i-2][j-1];
                vec[(i-1)-j] = vec[j];
            }
            res.push_back(vec);
        }
        return res;
    }
};
刷遍天下无敌手 文章被收录于专栏

秋招刷题历程

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务