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

SOLUTION 1:很easy的题。注意记得把List加到ret中。比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标。2. 中间的是f[i][j] = f[i - 1][j] + f[i - 1][j - 1]不断复用上一行的值即可。

 1 public class Solution {
 2     public List<List<Integer>> generate(int numRows) {
 3         List<List<Integer>> ret = new ArrayList<List<Integer>>();
 4
 5         for (int i = 0; i < numRows; i++) {
 6             List<Integer> list = new ArrayList<Integer>();
 7             for (int j = 0; j <= i; j++) {
 8                 if (j == 0 || i == j) {
 9                     list.add(1);
10                 } else {
11                     int sum = ret.get(i - 1).get(j - 1) + ret.get(i - 1).get(j);
12                     list.add(sum);
13                 }
14             }
15
16             // BUG 1: forget this statement.
17             ret.add(list);
18         }
19
20         return ret;
21     }
22 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/Generate.java

LeetCode: Pascal's Triangle 解题报告

时间: 2024-10-05 04:25:58

LeetCode: Pascal's Triangle 解题报告的相关文章

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)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] ] Subscribe to see which companies asked this question 解题分析: 题目的这个帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉

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

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

LeetCode——Pascal&#39;s Triangle

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] ] public class Solution { public List<List<Integer>> generate(int numRows) { List

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]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

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

[题目] 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] ] [题意] 给定整数numRows, 要求生成杨辉三角的前numRows行 [思路] 杨辉三角有以下特点: 1. 第n行有n个元素 2. 每行的收尾都是1, 其余元素A[n][i]=A[n-