leetcode 118

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

输出Pascal三角形的前n行;每次利用前面已经生成的行来生成下一行。

代码如下:
 1 class Solution {
 2 public:
 3     vector<vector<int>> generate(int numRows) {
 4         vector<vector<int>> pascal;
 5         vector<int> ss;
 6         if(numRows == 0)
 7         {
 8             return pascal;
 9         }
10         if(numRows == 1)
11         {
12             ss.push_back(1);
13             pascal.push_back(ss);
14             return pascal;
15         }
16         pascal.push_back({1});
17         for(int i = 1; i <numRows; i++)
18         {
19             for(int j = 0; j <= i; j++)
20             {
21                 if(j == 0 || j == i)
22                 {
23                     ss.push_back(1);
24                     continue;
25                 }
26                 int n = pascal[i-1][j-1] + pascal[i-1][j];
27                 ss.push_back(n);
28             }
29             pascal.push_back(ss);
30             ss.clear();
31         }
32         return pascal;
33     }
34 };
 
时间: 2024-08-04 11:54:48

leetcode 118的相关文章

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 (杨辉三角)

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,中间的都是上一行的两边

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

leetcode || 118、Pascal&#39;s Triangle

problem: 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] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值

Java for LeetCode 118 Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. 解题思路: 注意,本题的k相当于上题的k+1,其他照搬即可,JAVA实现如下: public List<Integer> getRow(int rowIndex) { List<Integer> alist=new ArrayList<Integer>();

[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] ] 杨辉三角形,又称贾宪三角形.帕斯卡三角形.海亚姆三角形.巴斯卡三角形,是二项式系数在的一种写法,形似三角形,在中国首现于南宋杨辉的<详解九章算术>得名,书中杨辉说明是引自贾宪的<释锁算术>

2017-3-6 leetcode 118 169 189

今天什么都没发生 ================================================= leetcode118 https://leetcode.com/problems/pascals-triangle/?tab=Description leetcode169 https://leetcode.com/problems/majority-element/?tab=Description leetcode189 https://leetcode.com/proble

[leetcode]118,119PascalsTriangle,杨辉三角1,2

杨辉三角1Given 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 List<List<Integer>> generate(int numRows) { List<List<Integer