LeetCode 118. Pascal's Triangle

来源:互联网 发布:windows live mail pst 编辑:程序博客网 时间:2024/05/29 05:56

描述

按照题目规则,输出三角形

解决


class Solution {public:    vector<vector<int>> generate(int numRows) {        vector<vector<int>> res;        for (int i = 1; i <= numRows; ++i)        {            vector<int> tmp(i);            int k = int(i / 2.0 + 0.5);            for (int j = 1; j <= k; ++j)            {                if (j == 1)                    tmp[j - 1] = tmp[i - 1] = 1;                else                {                    //cout << res[0].size() << endl;                    cout << res[i - 2][j - 1] << ' ' << res[i - 2][j] << endl;                    tmp[j - 1] = tmp[i - j] = res[i - 2][j - 2] + res[i - 2][j - 1];                }            }            res.push_back(tmp);        }        return res;    }};
0 0
原创粉丝点击