leetcode 119

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?

输出Pascal三角形的第k行(从0开始);空间复杂度为O(k);

从第0行开始生成,每次利用已经生成的行,在原有空间上生成下一行,直到生成所要求的行。

代码入下:

 1 class Solution {
 2 public:
 3     vector<int> getRow(int rowIndex) {
 4         vector<int>  pascal;
 5         pascal.push_back(1);
 6         int m = 1;
 7         for(int i = 0; i <= rowIndex; i++)
 8         {
 9             if(rowIndex == 0)
10             {
11                 return pascal;
12             }
13             for(int j = 1; j < i+1; j++)
14             {
15                 if(j == i)
16                 {
17                     pascal.push_back(1);
18                     break;
19                 }
20                 int n = pascal[j];
21                 pascal[j] = m + n;
22                 m = n;
23             }
24         }
25        return pascal;
26     }
27 };
时间: 2024-12-06 03:45:07

leetcode 119的相关文章

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:     

LeetCode#119 Pascal&#39;s Triangle II

Problem Definition: 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? Solution: 1 def getRow(rowIndex): 2 pt=[] 3 for rn in

C#解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]. Note:Could you optimize your algorithm to use only O(k) extra space? (注意:这里要求空间为O(k)) 一个满足条件的答案如下: public class Solution { public IList<int

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]. 题目要求计算杨辉三角某一行的元素,这个也是二项式系数的计算问题. class Solution { public: vector<int> getRow(int rowIndex) { vector<int> result; vector<int> tmp;

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 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? 题目标签:Array 这道题目与之前那题不同的地方在于,之前是给我们一个行数n,让我们把这几行的全部写出来,这样就可以在每写新的一行的时候根据之前的那

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&amp;#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]. 题目要求计算杨辉三角某一行的元素.这个也是二项式系数的计算问题. class Solution { public: vector<int> getRow(int rowIndex) { vector<int> result; vector<int> tmp;