ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

LeetCode118:Pascals Triangle

来源:网络整理     时间:2015-12-18     关键词:

本篇文章主要介绍了"LeetCode118:Pascals Triangle",主要涉及到方面的内容,对于其他编程jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: Given numRows, generate the first numRows of Pascal's triangle.For example, give...

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

For example, given numRows = 5,
Return

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

Subscribe to see which companies asked this question

//解题思路:利用一个中间vector来保存每层的数
class Solution {
public:
    vector<> > generate(int numRows) {
        vector<>> ans;
        for(int i = 0;i < numRows;i++)
        {
            vector cur;
            if(i == 0)
                cur.push_back(1);
            else
            {
                for(int j = 0;j <= i;j++)
                {
                    if(j == 0 || j == i) cur.push_back(1);
                    else cur.push_back(ans[i - 1][j] + ans[i - 1][j - 1]);
                }
            }
            ans.push_back(cur);
        }

        return ans;
    }
};

LeetCode118:Pascals Triangle

以上就介绍了LeetCode118:Pascals Triangle,包括了方面的内容,希望对其他编程jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_250844.html

相关图片

相关文章