leetcode-118. Pascal's Triangle

来源:互联网 发布:游戏作弊神器软件 编辑:程序博客网 时间:2024/05/16 08:30

Given numRows, generate the first numRows of Pascal’s triangle.

For example, given numRows = 5,
Return

这里写图片描述

思路:两个for循环,注意提炼出它内在的公式

class Solution {public:    vector<vector<int>> generate(int numRows) {        vector<vector<int>> result(numRows);        for(int i=0;i<numRows;i++)        {            result[i].resize(i+1);            result[i][0] = result[i][i] = 1;            for(int j=1;j<i;j++)            {                result[i][j] = result[i-1][j-1] + result[i-1][j];            }        }        return result;    }};
0 0
原创粉丝点击