Problem Pascal's Triangle

Problem Description:

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

Solution:

 1     public List<List<Integer>> generate(int numRows) {
 2 List<List<Integer>> triangle  = new ArrayList<List<Integer>>();
 3         for (int i = 0; i < numRows; i++) {
 4             List<Integer> row = new ArrayList<Integer>();
 5             for (int j = 0; j <= i; j++) {
 6                 if (j == 0 || j == i) {
 7                     row.add(1);
 8                 } else {
 9                     row.add(triangle.get(i-1).get(j-1) + triangle.get(i-1).get(j));
10                 }
11             }
12
13             triangle.add(row);
14         }
15
16         return triangle;
17     }

Problem Pascal's Triangle

时间: 2024-07-30 01:53:39

Problem Pascal's Triangle的相关文章

Problem Pascal&#39;s Triangle II

Problem Description: 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 public List<Integer> getRow(int rowIndex)

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

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:

leetcode || 118、Pascal&#39;s Triangle

problem: 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] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值

leetcode || 119、Pascal&#39;s Triangle II

problem: 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? Hide Tags Array 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间

(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

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

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? 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 5 vect