Leetcode#119Pascal's Triangle II

分析,与118题很相似,118题需要求出整个的金字塔list结合,本题只需要给出某一层的结果

public class Solution {

public List<Integer> getRow(int rowIndex) {

List<List<Integer>> x=new ArrayList<List<Integer>>();

if(rowIndex<0)

{

List<Integer> y=new ArrayList<Integer>();

return y;

}

else if(rowIndex==0)

{

List<Integer> y=new ArrayList<Integer>();

y.add(1);

return y;

}

else if(rowIndex==1)

{

List<Integer> y=new ArrayList<Integer>();

y.add(1);

y.add(1);

return y;

}

else

{

List<Integer> y=new ArrayList<Integer>();

y.add(1);

x.add(y);

y=new ArrayList<Integer>();

y.add(1);

y.add(1);

x.add(y);

int v=3;

int listsize=2;

while(v<=rowIndex+1)

{

y=new ArrayList<Integer>();

int size=x.get(listsize-1).size();

//int[] z=new int[size+1];

//z[0]=1;

y.add(1);

for(int i=1;i<size;i++)

y.add(x.get(listsize-1).get(i-1)+x.get(listsize-1).get(i));

y.add(1);

x.add(y);

listsize++;

v++;

}

return x.get(x.size()-1);

}

}

}

Leetcode#119Pascal's Triangle II

时间: 2024-09-29 16:56:56

Leetcode#119Pascal's Triangle II的相关文章

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven 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 us

【LeetCode-面试算法经典-Java实现】【119-Pascal&#39;s Triangle II(帕斯卡三角形(杨辉三角)II)】

[119-Pascal's Triangle II(帕斯卡三角形(杨辉三角)II)] [LeetCode-面试算法经典-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

(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 解题分析: 此处有空间的限制,因此不能正

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

题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if(rowIndex==0) return vector<int>(1,1); //0和1特殊处理 5 if(rowIndex==1) return vector<int>(2,1); 6

LeetCode: Pascal&#39;s Triangle II [119]

[题目] 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, k从0开始,返回该索引指向的杨辉三角的行 要求只能使用O(k)的额外空间 [思路] 申请两个k+1大小的数组,

[LeetCode]Pascal&amp;#39;s Triangle II

题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public List<Integer> getRow(int rowIndex) { if (rowIndex < 0) { return null; } List<List<Integer>> pascalTriangle = new ArrayList<List<I

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 【 Pascal&#39;s Triangle II 】python 实现

题目: 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? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege