Leetcode#118Pascal's Triangle

Pascal‘s Triangle

Total Accepted: 43914 Total Submissions: 145531My Submissions

Question Solution

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

分析,数据特点是,当前行可由上一行计算求得,计算方式,A[n][m]=A[n-1][m]+A[n-1][m-1]

public class Solution {

public List<List<Integer>> generate(int numRows) {

List<List<Integer>> x=new ArrayList<List<Integer>>();

if(numRows<1)

return x;

else if(numRows==1)

{

List<Integer> y=new ArrayList<Integer>();

y.add(1);

x.add(y);

return x;

}

else if(numRows==2)

{

List<Integer> y=new ArrayList<Integer>();

y.add(1);

x.add(y);

y=new ArrayList<Integer>();

y.add(1);

y.add(1);

x.add(y);

return x;

}

else

{

List<Integer> y=new ArrayList<Integer>();

y.add(1);

x.add(y);

y=new ArrayList<Integer>();

y.add(1);

y.add(1);

x.add(y);

int v=3;

int listsize=2;

while(v<=numRows)

{

y=new ArrayList<Integer>();

int size=x.get(listsize-1).size();

//int[] z=new int[size+1];

//z[0]=1;

y.add(1);

for(int i=1;i<size;i++)

y.add(x.get(listsize-1).get(i-1)+x.get(listsize-1).get(i));

y.add(1);

x.add(y);

/*

y=new ArrayList<Integer>();

int[] z=new int[v/2+1];

for(int i=0;i<(v+1)/2;i++)

{

z[i]=1+i*(v-2);

y.add(z[i]);

}

if(v%2==0)

{

for(int i=(v+1)/2-1;i>=0;i--)

y.add(z[i]);

}

else

{

for(int i=(v+1)/2-2;i>=0;i--)

y.add(z[i]);

}

x.add(y);

*/

listsize++;

v++;

}

return x;

}

}

}

Leetcode#118Pascal's Triangle

时间: 2024-10-14 09:00:27

Leetcode#118Pascal's Triangle的相关文章

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: 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-面试算法经典-Java实现】【118-Pascal&#39;s Triangle(帕斯卡三角形)】

[118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-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] ] 题目大意 给定一个正整数n,求n层帕斯卡三角形

(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#119Pascal&#39;s Triangle II

分析,与118题很相似,118题需要求出整个的金字塔list结合,本题只需要给出某一层的结果 public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> x=new ArrayList<List<Integer>>(); if(rowIndex<0) { List<Integer> y=new ArrayList&