Pascal's Triangle Pascal's Triangle||

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]
]

Pascal‘s Triangle||:

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‘s triangle.

public class Solution {
	public List<List<Integer>> generate(int numRows) {
	    if(numRows == 0){
			return new ArrayList<List<Integer>>();
		}
		List<List<Integer>> ans = new ArrayList<List<Integer>>();
		ans.add(new ArrayList<Integer>(Arrays.asList(1)));
		for(int i=1;i<numRows;i++){
			List<Integer> list = new ArrayList<Integer>();
			for(int j=0;j<=i;j++){
				if(j==0||j==i){
					list.add(1);
					continue;
				}
				list.add(ans.get(i-1).get(j-1)+ans.get(i-1).get(j));
			}
			ans.add(list);
		}
		return ans;
	}
}

Pascal‘s Triangle||:

public class Solution {
    	public List<Integer> getRow(int rowIndex) {
		List<Integer> ans = new ArrayList<Integer>();
		for(int i=0;i<=rowIndex;i++){
			List<Integer> temp = new ArrayList<Integer>();
			for(int j=0;j<=i;j++){
				if(j==0||j==i){
					temp.add(1);
					continue;
				}
				temp.add(ans.get(j)+ans.get(j-1));
			}
			ans = new ArrayList<Integer>(temp);
		}
		return ans;
	}
}

其实这些基本处理就是学习C语言时的杨辉三角。

Pascal's Triangle Pascal's Triangle||

时间: 2024-12-13 14:11:08

Pascal's Triangle Pascal's Triangle||的相关文章

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

28. Triangle &amp;&amp; Pascal&#39;s Triangle &amp;&amp; Pascal&#39;s Triangle II

Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom

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

leetcode 【 Pascal&#39;s Triangle 】python 实现

题目: 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] ] 代码:oj测试通过 Runtime: 46 ms 1 class Solution: 2 # @return a list of lists of integers 3 def generat

Pascal Hexagrammum Mysticum 的深度探索

    PASCAL . Hexagrammum Mysticum . (六角迷魂图) . 的深度探索 . 英中对比.英文蓝色,译文黑色,译者补充说明用紫红色 (已校完,但尚未定稿,想再整理并补充内容) keyword:hexagon(六角形.六边形),  6点15边,  45对边交点(opposite edge meet), Hexagrammum  Mysticum(六角迷魂图),  射影几何,   Pascal定理, Pascal线,  Steiner点,   Plucker线,   Ki

(leetcode题解)Pascal&#39;s Triangle

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] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

Lab 1: Write a java program for the triangle problem and test the program with Junit.

Tasks: 1. Install Junit(4.12), Hamcrest(1.3) with Eclipse 将两个jar包添加到工程中 2. Install Eclemma with Eclipse 3. Write a java program for the triangle problem and test the program with Junit. [Description of triangle problem]Function triangle takes three i

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

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

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] ] 分析 第k层有k个元素,每层第一个及最后一个元素值为1,对于(k>2)层,第n(n>1 && n < k)个元素A[k][n] = A[k-1][n-1]+A[k-1][n];