Java for LeetCode 118 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].

解题思路:

注意,本题的k相当于上题的k+1,其他照搬即可,JAVA实现如下:

    public List<Integer> getRow(int rowIndex) {
        List<Integer> alist=new ArrayList<Integer>();
        rowIndex++;
    	if(rowIndex<=0)
			return alist;
    	alist.add(1);
    	for(int i=2;i<=rowIndex;i++){
			List<Integer> alist2=new ArrayList<Integer>();
			alist2.add(1);
			for(int j=1;j<i-1;j++){
				alist2.add(alist.get(j-1)+alist.get(j));
			}
			alist2.add(1);
			alist=alist2;
		}
    	return alist;
    }

Java for LeetCode 118 Pascal's Triangle II

时间: 2024-08-04 11:54:48

Java for LeetCode 118 Pascal's Triangle II的相关文章

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 &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:Pascal‘s Triangle II - 帕斯卡三角形2

1.题目名称 Pascal's Triangle II(帕斯卡三角形2) 2.题目地址 https://leetcode.com/problems/pascals-triangle-ii/ 3.题目内容 英文:Given an index k, return the kth row of the Pascal's triangle. 中文:给出行数k,返回帕斯卡三角形的第k行 例如,k=3时,返回[1,3,3,1] 4.解题方法1 帕斯卡三角形也叫杨辉三角形,在LeetCode第118题(Pas

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

【Leetcode】Pascal&#39;s Triangle II

题目链接:https://leetcode.com/problems/pascals-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

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