LeetCode——Pascal'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<List<Integer>> list = new ArrayList<List<Integer>>();
        for(int i=0; i<numRows; i++) {
            List<Integer> tList = new ArrayList<Integer>();
            if(i == 0) {
                tList.add(1);
            }
            else {
                for(int j=0; j<=i; j++) {
                    if(j == 0 || j == i) {
                    tList.add(1);
                }
                else {
                    tList.add(list.get(i-1).get(j) + list.get(i-1).get(j-1));
                }
                }
            }
            list.add(tList);
        }
        return list;
    }
}

LeetCode——Pascal's Triangle

时间: 2024-08-25 14:48:29

LeetCode——Pascal's Triangle的相关文章

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 解题报告

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.

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

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-

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

称号:定行数n,生成n帕斯卡三角行 算法:步骤通过阵列工序 public class Solution { public List<List<Integer>> generate(int numRows) { if (numRows < 0) { return null; } List<List<Integer>> pascalTriangle = new ArrayList<List<Integer>>(); for (int