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]
] Thoughts:
第一种:直观的想法就是,我们计算第n+1行的值,它的头尾都是1,中间的值T[n+1][i] = T[n][i-1]+T[i];根据这个想法得到如下的java代码:
class Solution {
public List<List<Integer>> generate(int numRnows){
List<List<Integer>> result = new ArrayList();
if(numRnows<=0){
return new ArrayList(){{}};
}
for(int i = 1;i<=numRnows;i++){
List lists = new ArrayList(); if(i == 1){
lists.add(1);
}else{
lists.add(1);
for(int j = 1; j<i-1;j++){
lists.add(result.get(i-2).get(j-1)+result.get(i-2).get(j));
}
lists.add(1);
} result.add(lists);
}
return result;
} }

第二种想法:第i行有i个数;每次往最前面插入一个1,那么除了头尾以外,中间的值T[n][i] = T[n][i]+T[n][i+1], 根据这个想法得到如下的代码:

package easy.array;

import java.util.ArrayList;
import java.util.List; public class GeneratePascal {
public List<List<Integer>> generate(int numRnows){
List<List<Integer>> allrows = new ArrayList<List<Integer>>();
ArrayList<Integer> row = new ArrayList<Integer>();
for(int i=0;i<numRnows;i++)
{
row.add(0, 1);
for(int j=1;j<row.size()-1;j++)
row.set(j, row.get(j)+row.get(j+1));
allrows.add(new ArrayList<Integer>(row));
}
return allrows;
} public static void main(String[] args){
int numRnows = 10;
GeneratePascal ge = new GeneratePascal();
List<List<Integer>> f = ge.generate(numRnows);
for(int i= 0; i<numRnows;i++){
System.out.println(f.get(i));
}
}
}

Pascal Triangle的更多相关文章

  1. leetcode—pascal triangle

    1.题目描述 Given numRows, generate the first numRows of Pascal's triangle.   For example, given numRows ...

  2. 杨辉三角(Pascal Triangle)的几种C语言实现及其复杂度分析

    说明 本文给出杨辉三角的几种C语言实现,并简要分析典型方法的复杂度. 本文假定读者具备二项式定理.排列组合.求和等方面的数学知识. 一  基本概念 杨辉三角,又称贾宪三角.帕斯卡三角,是二项式系数在三 ...

  3. HDU 4237 The Rascal Triangle

    The Rascal Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. Pescal Triangle Two

    Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...

  5. leetcode-pascal triangle I&&II

    对于第2个pascal triangle,通过观察可以发现,其实只需要2个额外的变量来记录,于是就设了个tmp数组. 整体有点DP问题中的滚动数组的感觉. #include <vector> ...

  6. Must practice programming questions in all languages

    To master any programming languages, you need to definitely solve/practice the below-listed problems ...

  7. 算法之杨辉三角形(Java语言)

    杨辉三角形, 又称贾宪三角形.帕斯卡三角形. 前9层写出来如下: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 ...

  8. leetcode 0217

    目录 ✅ 682. 棒球比赛 描述 解答 cpp py ✅ 999. 车的可用捕获量 描述 解答 c other java todo py ✅ 118. 杨辉三角 描述 解答 cpp py ✅ 258 ...

  9. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

随机推荐

  1. 海量数据挖掘MMDS week5: 聚类clustering

    http://blog.csdn.net/pipisorry/article/details/49427989 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  2. (NO.00002)iOS游戏精灵战争雏形(五)

    完成了精灵自己移动之后,我们开始着手实现按住精灵拖动的功能. 要想处理触摸事件,我们需要做2件事: 1.在类的初始化中打开触摸接收属性 2.实现触摸处理回调方法 我们依次来做这2件事. 首先要搞清楚在 ...

  3. ISLR系列:(4.1)模型选择 Subset Selection

    Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...

  4. Java 与 C++ 不一样的地方(持续更新中...)

    本文仅以记录 Java 与 C++ 不同之处,以备随时查询. Java 程序运行机制 Java 是一门编译解释型的语言,即它在运行的过程中既需要编译也需要解释.如下图表示的是 Java 程序运行机制: ...

  5. Mahout系列之----共轭梯度预处理

    对于大型矩阵,预处理是很重要的.常用的预处理方法有: (1) 雅克比预处理 (2)块状雅克比预处理 (3)半LU 分解 (4)超松弛法

  6. Cocos2D的平台检查宏

    为了避免在非iOS平台包含UIKit.h文件,需要在Prefix.pch文件中添加一个条件判断: #if __CC_PLATFORM_IOS #import <UIKit/UIKit.h> ...

  7. Leetcode_114_Flatten Binary Tree to Linked List

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42744919 Given a binary tree, f ...

  8. React Native开发必备的10个插件包

    Sublime Text 具有漂亮的用户界面和强大的功能,例如代码缩略图,多重选择,快捷命令等.Sublime Text 更妙的是它的可扩展性.所以,这里挑选了全栈开发必备的10款 Sublime T ...

  9. Java学习笔记(一)网格袋布局

    网格袋布局类似于Win8的Metro布局,用于将组件按大小比例放在不同位置的网格内,各组件的实际大小会随着窗口的改变而改变,但相对位置不变,能够很好的适应屏幕. 通过阅读<21天学通Java&g ...

  10. 测试access函数

    测试程序: 测试结果: chown root access.out 将用户ID改为root chmod u+s access.out 打开 set-user-ID位