LeetCode118 Pascal's Triangle

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

For example, given numRows = 5, (Easy)
Return

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

分析:

求解杨辉三角形,按照其定义以此求解即可,注意优化写法使其能够更简洁(比如对于二维数组每一行的个数其实是已知的,所以可以先push_back一个长度的数组,后面直接用下标访问),即第二种写法的代码。

代码:

 1 class Solution {
 2 public:
 3     vector<vector<int>> generate(int numRows) {
 4         vector<vector<int>> result;
 5         if (numRows == 0) {
 6             return result;
 7         }
 8         vector<int> oldVec{1};
 9         result.push_back(oldVec);
10         for (int i = 2; i <= numRows; ++i) {
11             vector<int> newVec;
12             for (int j = 0; j < i; ++j) {
13                 int temp;
14                 if (j > 0 && j < oldVec.size()) {
15                     temp = oldVec[j] + oldVec[j - 1];
16                 }
17                 else if (j > 0){
18                     temp = oldVec[j - 1];
19                 }
20                 else {
21                     temp = oldVec[0];
22                 }
23                 newVec.push_back(temp);
24             }
25             result.push_back(newVec);
26             oldVec = newVec;
27         }
28         return result;
29     }
30 };
 1 class Solution {
 2 public:
 3     vector<vector<int>> generate(int numRows) {
 4         vector<vector<int>> result;
 5         if (numRows == 0) {
 6             return result;
 7         }
 8         vector<int> init{1};
 9         result.push_back(init);
10         for (int i = 2; i <= numRows; ++i) {
11             result.push_back(vector<int>(i,1));
12             for (int j = 1; j < i - 1; ++j) {
13                 result[i - 1][j] = result[i - 2][j] + result[i - 2][j - 1];
14             }
15         }
16         return result;
17     }
18 };

LeetCode118 Pascal's Triangle

时间: 2024-10-10 03:34:28

LeetCode118 Pascal's Triangle的相关文章

LeetCode118——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] ] 题目大意 给定numRows,生成帕斯卡三角的前numRows行. 难度系数:容易 实现 vector<vector<int> > generate(int rowIndex) { v

每天一道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] ] solution: ackage cn.magicdu; import java.util.ArrayList; import java.util.List; public class _118_Pa

LeetCode118:Pascal&amp;#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] ] Subscribe to see which companies asked this question //解题思路:利用一个中间vector来保存每层的数 class Solution { pu

(leetcode题解)Pascal&#39;s Triangle

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] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

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

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

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

119. 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]. Note:Could you optimize your algorithm to use only O(k) extra space? 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 5 vect

Java [Leetcode 119]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]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r

LeetCode OJ: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] ] 帕斯卡三角,很简单的问题,见代码: 1 class Solution { 2 public: 3 vector<vector<int>> generate(int numRows) {