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 ...
随机推荐
- php无缝连接滚动
最近用到了,仿照别人的写了一个 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- unity3d基础01
Unity3d 五大视图: 1 Scene:存放hierarchy中创建的游戏对象,但实际只能看到一部分 *Scene浏览: ①右键进入“飞行模式”,方便查看整个场景 ②选中摄像机,按ALT进入浏览的 ...
- (spring-第7回【IoC基础篇】)BeanDefinition的载入与解析&&spring.schemas、spring.handlers的使用
报错信息:Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http: ...
- 一次蜿蜒曲折的RFID破解之路
前言 早一段时间看到一篇看雪论坛关于逻辑嗅探破解接触式IC卡口令的文章,激起鄙人对rfid的兴趣.遂准备拿学校的卡一展身手. 0×00 前期准备 经过初步了解,学校的rfid卡片分为两种.校园卡采用M ...
- 过期邮件替换SQL
- 安装VMware Tools找不到内核头文件
http://blog.csdn.net/bobbat/article/details/38568885 安装VMware Tools,解决无法找到kernel header path的问题 安装 V ...
- MATLAB做主成分分析(PCA)
简单的主成分分析.第一次见识PCA,我的认识是,尽量用更少的维度来描述数据,以达到理想(虽不是最好,但是''性价比''最高)的效果. %% 主成分分析降维 clear; % 参数初始化 inputfi ...
- Java Abstract Class
在Baths-stomp里面的每个Fluent Interface Interactor Impl,都继承了MarketDataAccessor,which is an abstract class. ...
- iOS App 转移
此文章只是为了记录一个Apple ID下的APP,转移到另外一个Apple ID 账户下.为了说的清楚下面用A账户(有App,要转出去)B账户(接收A账户App,接收者),来说明. 1. 登 ...
- Bad Hair Day_单调栈
Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow ...