LeetCode(30)-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]
]
思路
- 题意是要用数组表示帕斯卡三角形
- 输入一个数值,显示相应行数的【帕斯卡三角形
- 根据下一行和上一行的递推公式来处理
- 设置一个变量fisrt来记录上一行的数据,最新一行的是新建的
-
代码
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> all = new ArrayList<List<Integer>>();
List<Integer> first = new ArrayList<Integer>();
if(numRows == 0){
return all;
}
if(numRows == 1){
List<Integer> a1 = new ArrayList<Integer>();
a1.add(1);
all.add(a1);
return all;
}
if(numRows > 1){
List<Integer> a2 = new ArrayList<Integer>();
a2.add(1);
all.add(a2);
}
for(int i = 1;i < numRows;i++){
List<Integer> next = new ArrayList<Integer>();
next.clear();
next.add(1);
for(int a = 1;a < i;a++){
if(a-1 >= 0 )
next.add(first.get(a-1)+first.get(a));
}
next.add(1);
all.add(next);
first.clear();
if(next != null){
for(int o = 0;o < next.size();o++){
first.add(next.get(o));
}
}
}
return all;
}
}
LeetCode(30)-Pascal's Triangle的更多相关文章
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- LeetCode 119. 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, ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
随机推荐
- shell 参数列表的获取&shell使用的一些总结
最近在修改公司的一些cron,自己也是第一次接触和学习shell.对于一些零散但是常用的知识点,做一点点的总结. 拿出一个方法说说吧,方法如下:(信息量挺大的,请耐心看下面的说明) trans_cou ...
- Android初级教程对大量数据的做分页处理理论知识
有时候要加载的数据上千条时,页面加载数据就会很慢(数据加载也属于耗时操作).因此就要考虑分页甚至分批显示.先介绍一些分页的理论知识.对于具体用在哪里,会在后续博客中更新. 分页信息 1,一共多少条数据 ...
- Xdoclet + Ant自动生成Hibernate配置文件
在使用Hibernate的时候,过多的Hibernate配置文件是一个让人头疼的问题.最近接触了Xdoclet这个工具.它实际上就是一个自动代码生成的工具,Xdoclet不能单独运行,必须搭配其他工具 ...
- (NO.00004)iOS实现打砖块游戏(四):砖块类的实现
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 用Xcode打开之前SpriteBuilder创建的项目,我们现 ...
- Android数据库框架——GreenDao轻量级的对象关系映射框架,永久告别sqlite
Android数据库框架--GreenDao轻量级的对象关系映射框架,永久告别sqlite 前不久,我在写了ORMLite这个框架的博文 Android数据库框架--ORMLite轻量级的对象关系映射 ...
- iOS中 Swift初级入门学习(一)
/ // Copyright (c) 2015年 韩俊强. All rights reserved. // import Foundation // Swift当中的输出函数 // println S ...
- React Native入门教程2 -- 基本组件使用及样式
在上一篇文章中,我们学会了如何搭建React Native的环境(React Native入门教程(笔记) 1 – 开发环境搭建),不知道你们是否搭建好了呢,如果还没有,那么快动起小手,来体验RN带给 ...
- Linux信号实践(1) --Linux信号编程概述
中断 中断是系统对于异步事件的响应, 进程执行代码的过程中可以随时被打断,然后去执行异常处理程序; 计算机系统的中断场景:中断源发出中断信号 -> CPU判断中断是否屏蔽屏蔽以及保护现场 -&g ...
- Leetcode_83_Remove Duplicates from Sorted List
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41728739 Given a sorted linked ...
- ActivityManagerService启动过程分析
之前讲Android的View的绘制原理和流程的时候,讲到过在Android调用setContentView之后,Android调用了一个prepreTravle的方法,这里面就提到了Activity ...