leetcode 118. Pascal's Triangle && leetcode 119. Pascal'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     {
 3         vector<vector<int> > pascal;
 4         vector<int> line;
 5
 6         if (numRows <= 0)
 7             return pascal;
 8
 9         line.push_back(1);
10         pascal.push_back(line);
11         if (numRows == 1)
12             return pascal;
13
14         for (int i = 1; i < numRows; i++)
15         {
16             vector<int> nextLine;
17
18             nextLine.push_back(1);
19             for (int j = 0; j < i - 1; j++)
20                 nextLine.push_back(line[j] + line[j + 1]);
21             nextLine.push_back(1);
22             pascal.push_back(nextLine);
23             line = nextLine;
24         }
25
26         return pascal;
27     }

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 vector<int> getRow(int rowIndex)
 2     {
 3         vector<int> line;
 4         vector<int> nextline;
 5         int numRows = rowIndex + 1;
 6
 7         if (numRows <= 0)
 8             return line;
 9
10         line.push_back(1);
11         if (numRows == 1)
12             return line;
13
14         for (int i = 1; i < numRows; i++)
15         {
16             nextline.push_back(1);
17             for (int j = 0; j < i - 1; j++)
18                 nextline.push_back(line[j] + line[j + 1]);
19             nextline.push_back(1);
20             line = nextline;
21             nextline.clear();
22         }
23
24         return line;
25     }

leetcode 118. Pascal's Triangle && leetcode 119. Pascal's Triangle II

时间: 2024-12-22 10:39:28

leetcode 118. Pascal's Triangle && leetcode 119. Pascal's Triangle II的相关文章

leetcode 生成杨辉三角形, 118 119 Pascal&#39;s Triangle 1,2

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<vector<int>> generate(int numRows) { vector<vector<int>> res = {};

LeetCode开心刷题五十一天——118. Pascal&#39;s Triangle 接触跳转表概念,不知用处 lamda逗号导致表达式加法奇怪不理解119. Pascal&#39;s Triangle II

118. Pascal's Triangle Easy 87984FavoriteShare Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1],

LeetCode 119 Pascal&#39;s Triangle II(帕斯卡三角形II)(vector、数学公式)(*)

翻译 给定一个索引K,返回帕斯卡三角形的第K行. 例如,给定K=3, 返回[1,3,3,1]. 注释: 你可以改进你的算法只用O(k)的额外空间吗? 原文 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 sp

[LeetCode] 119. Pascal&#39;s Triangle II 杨辉三角 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? 118. Pascal's Triangle 的拓展,给一个索引k,返回杨辉三角的第k行. 解法:题目要求优化到 O(k) 的空间复杂,那么就不能把每

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

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 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 119 Pascal&#39;s Triangle II ----- java

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? 上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度. 也是直接相加就可以了. public class Solution { pub

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