LeetCode 119. Pascal'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?



题目标签:Array

  这道题目与之前那题不同的地方在于,之前是给我们一个行数n,让我们把这几行的全部写出来,这样就可以在每写新的一行的时候根据之前的那一行来得出。这一题给了我们一个k,让我们直接得出第3行的数字(这里从0开始,所以3代表第四行)。我们可以先设一个list size为 k + 1。然后把k + 1 的0加入list。 接着设第一个位置为1,之后遍历剩下的数字,对于每一个数字,把它设为1,并且遍历这个数字的前一个数字,一直回到最开始的第二个数字,对于每一个数字,把它和它前面一个相加。(这一步就等于把之前的每一行给重现出来)。我们来看一个例子:

当k = 4,

0  0  0  0  0  先设5个0

1  0  0  0  0  第一个设为1

1  1  0  0  0  第二个设为1

1  1  1  0  0  第三个数字设为1的时候,就需要开始遍历前面一个数字了,因为第二个数字1 position 为1 > 0

1  2  1  0  0    遍历完之后的结果

1  2  1  1  0  第四个数字设为1的时候,需要开始遍历从前面一个数字,一直回到第二个数字

1  2  3  1  0    遍历中

1  3  3  1  0    遍历结束

1  3  3  1  1  第五个数字设为1, 从第四个遍历回第二个

1  3  3  4  1    遍历中

1  3  6  4  1    遍历中

1  4  6  4  1    遍历结束,得到答案

Java Solution:

Runtime beats 51.88%

完成日期:04/06/2017

关键词:Array

关键点:设k+1个0,设第一个为1,遍历之后的数字,把每一个数字设为1的同时,从前一个数字遍历回第二个数字,生成新一行的数组

 1 public class Solution
 2 {
 3    public ArrayList<Integer> getRow(int rowIndex)
 4    {
 5       // define arraylist with size = rowIndex + 1.
 6         ArrayList<Integer> result = new ArrayList<Integer>(rowIndex + 1);
 7
 8         // first, add all 0s for each spot.
 9         for(int i = 0; i <= rowIndex; i++)
10             result.add(0);
11
12         // set the first number to 1.
13         result.set(0, 1);
14
15         // iterate from second number to end
16         for(int i = 1; i <= rowIndex; i++)
17         {
18             // set the number to 1 first.
19             result.set(i, 1);
20             // iterate from the prior number to 2nd number (backward).
21             for(int j = i - 1; j > 0; j--)
22                 result.set(j, result.get(j) + result.get(j - 1));// update the number with sum of itself and prior one.
23
24         }
25
26
27         return result;
28     }
29 }

参考资料:

http://www.cnblogs.com/springfor/p/3887913.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 119. Pascal's Triangle II (杨辉三角之二)

时间: 2024-08-02 07:00:17

LeetCode 119. Pascal's Triangle II (杨辉三角之二)的相关文章

[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)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? Subscribe to see which companies asked this question 解题分析: 此处有空间的限制,因此不能正

Pascal&#39;s Triangle II 杨辉三角

1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 vector<vector<int>> tri; 5 if(rowIndex==0) 6 { 7 vector<int> c; 8 c.push_back(1); 9 return c; 10 } 11 vector<int> b; 12 b.push_back(0); 13 b.push_back(1); 14

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

Pascal&#39;s Triangle 2 杨辉三角之二

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 1 1 1 2 1 1 3 3 1 1 4 6 4 1 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 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 119. Pascal&#39;s Triangle II 数组

119. Pascal'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? 代码如下:(使用双数组处理,未优化版) class Solution { public: