No.118 Pascal's Triangle

No.118 Pascal‘s Triangle

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]
]

解法:其实很简单,想清楚就好,找好规律!!

 1 #include "stdafx.h"
 2 #include <string>
 3 #include <vector>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 class Solution
 9 {
10 public:
11     vector<vector<int>> generate(int numRows)
12     {//生成帕斯卡三角形
13      //找规律
14         vector<vector<int>> res;
15         if(numRows<=0)
16             return res;
17         vector<int> front;
18         for(int i=0; i<numRows; i++)
19         {
20             vector<int> tmp;
21             for(int j=0; j<i+1; j++)
22             {
23                 if(j==0 || j==i)
24                     tmp.push_back(1);
25                 else
26                 {
27                     tmp.push_back(front[j]+front[j-1]);//又是i、j傻傻分不清楚!!!
28                 }
29             }
30             front = tmp;
31             res.push_back(tmp);
32         }
33         return res;
34     }
35 };
36
37 int main()
38 {
39     Solution sol;
40     int numRows;
41     vector<vector<int>> res;
42     while(cin >> numRows)
43     {
44         cout << numRows<<" : "<<endl;
45         res = sol.generate(numRows);
46         for(const auto &i : res)
47         {
48             for(const auto &j : i)
49                 cout << j << " ";
50             cout << endl;
51         }
52         cout <<"--------------"<<endl;
53     }
54 }

No.118 Pascal's Triangle

时间: 2024-10-25 13:15:27

No.118 Pascal's Triangle的相关文章

118. Pascal&#39;s Triangle

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] ] 分析 第k层有k个元素,每层第一个及最后一个元素值为1,对于(k>2)层,第n(n>1 && n < k)个元素A[k][n] = A[k-1][n-1]+A[k-1][n];

leetcode 118 Pascal&#39;s Triangle ----- java

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] ] 用的比价暴力的方法, 也是最快的. public class Solution { List list = new ArrayList<List<Integer>>(); public

leetCode 118. Pascal&#39;s Triangle 数组 (杨辉三角)

118. Pascal's Triangle 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] ] 题目大意: 输入行数,输出如上图所示的数组.(杨辉三角) 思路: 用双vector来处理当前行和下一行. 代码如下: cla

【leetcode】118. Pascal&#39;s Triangle

@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) /************************ * @description: simple. * @time_complexity: O(n) * @space_complexity: O(n) ****

[leedcode 118] Pascal&#39;s Triangle

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] ] public class Solution { public List<List<Integer>> generate(int numRows) { //杨辉三角形,把每一行从头对齐,每

118. Pascal&#39;s Triangle (Graph; WFS)

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] ] class Solution { public: vector<vector<int> > generate(int numRows) { vector<vector<in

LeetCode 118. Pascal&#39;s Triangle (杨辉三角)

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] ] 题目标签:Array 题目给了我们一个numRows,让我们写出这个行数的杨辉三角.来观察一下原题例子,5行的话,第一行只有1,第二行,只有1,第三行,除去第一个1和最后一个1,中间的都是上一行的两边

Leet Code OJ 118. Pascal&#39;s Triangle [Difficulty: Easy]

题目: 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] ] 翻译: 给定一个数numRows,产生前numRows行的杨辉三角(即贾宪三角形.帕斯卡三角形). 分析: 除了每行首尾是1以外,其他元素均可由上行推出,本方案采用lastLine保存上行数

leetcode 118. Pascal&#39;s Triangle &amp;&amp; leetcode 119. Pascal&#39;s Triangle II

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] ]nextline[j] = currentline[j] + currentline[j+1] 1 vector<vector<int> > generate(int numRows) 2