leetcode 118 Pascal's Triangle ----- 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]
]
用的比价暴力的方法, 也是最快的。
public class Solution {
List list = new ArrayList<List<Integer>>();
public List<List<Integer>> generate(int numRows) {
for( int i = 0;i<numRows;i++)
help(i+1);
return list;
}
public void help(int num){
List ans = new ArrayList<Integer>();
if( num == 1){
ans.add(1);
list.add(ans);
return ;
}else if( num == 2 ){
ans.add(1);
ans.add(1);
list.add(ans);
return ;
}
ArrayList<Integer> last = (ArrayList<Integer>) list.get(num-2);
ans.add(1);
int a = last.get(0);
int b = last.get(1);
for( int i = 2;i<last.size();i++){
ans.add(a+b);
a = b;
b = last.get(i);
}
ans.add(a+b);
ans.add(1);
list.add(ans);
}
}
leetcode 118 Pascal's Triangle ----- java的更多相关文章
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- 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 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- Java for LeetCode 118 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- Java [Leetcode 118]Pascal's Triangle
题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 118. Pascal's Triangle (java)
问题描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- Leetcode 118 Pascal's Triangle 数论递推
杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...
随机推荐
- EF Code-First数据迁移的尝试
Code-First的方式虽然省去了大量的sql代码,但增加了迁移的操作.尝试如下: 1.首先要在“扩展管理器”里搜索并安装NuGet“库程序包管理器”,否则所有命令都不能识别,会报CommandNo ...
- java程序(一)----HashMap同时获取键值
快速会用: HashMap<Integer,String> maps=new HashMap<Integer,String>(); maps.put(1,"xiaom ...
- 五大要求让BPM与企业对接
BPM(即业务流程管理)在中国已经有多年的发展历史,但人们经常提到的还是企业对流程的迫切需要,鲜有人讨论什么样的企业才能实施BPM,或者换句话说BPM的本身对企业有什么要求.不是所有的工作都适合BPM ...
- android 原生dialog对话框
http://www.cnblogs.com/xiaoluo501395377/p/3419398.html
- PDF2SWF转换只有一页的PDF文档,在FlexPaper不显示解决方法
问题:PDF2SWF转换只有一页的PDF文档,在FlexPaper不显示! FlexPaper 与 PDF2SWF 结合是解决在线阅读PDF格式文件的问题的,多页的PDF文件转换可以正常显示,只有一页 ...
- 弹框工作区(dialog)
弹出窗口分为普通弹出窗口和模态弹出窗口,普通弹出窗口可以铜鼓taskBar组件进行最小化等操作.弹出的窗口的DOM结构会放入主页面的body中,结构如下: <div class="bj ...
- STM32之延时秒,毫秒,微秒
#include "delay.h" #include "stdint.h" #include "stm32f10x.h" ; //us延时 ...
- UIViewController添加子控制器(addChildViewController)
// // TaskHallViewController.m // yybjproject // // Created by bingjun on 15/10/27. // Copyright ...
- 2016年3月AV评测
- java获取获得Timestamp类型的当前系统时间。
java获取获得Timestamp类型的当前系统时间. java获取取得Timestamp类型的当前系统时间 格式:2010-11-04 16:19:42 方法1: Timestamp d = n ...