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 ...
随机推荐
- 黑马程序员——C语言基础 scanf函数 基本运算 三目运算符
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (一下内容是对黑马苹果入学视频的个人知识点总结) (一)scanf函数 1> 简单介绍一下scanf函数 这是在 ...
- 【转】 JAR、WAR、EAR的使用和区别
Jar.war.EAR.在文件结构上,三者并没有什么不同,它们都采用zip或jar档案文件压缩格式.但是它们的使用目的有所区别: Jar文件(扩展名为. Jar,Java Application Ar ...
- MySQL数据库远程连接
12.00 MySQL数据库远程连接 参考: http://www.jb51.net/article/24508.htm http://www.linuxdiyf.com/viewarticle.ph ...
- xcode开发的6个小技巧
Xcode是iPhone和iPad开发者用来编码或者开发iOS app的IDE.Xcode有很多小巧但很有用的功能,很多时候我们可能没有注意到它们,也或者我们没有在合适的水平使用这些功能简化我们的iO ...
- static inline
今天看到了这样一段代码, static inline BOOL IsEmpty(id thing) { return thing == nil || [thing isEqual:[NSNull nu ...
- Android中判断当前网络是否可用
转载原文地址:http://www.cnblogs.com/renqingping/archive/2012/10/18/Net.html 当前有可用网络,如下图: 当前没有可用网络,如下图: 实现步 ...
- <转>thinkphp的各种内部函数 D()、F()、S()、C()、L()、A()、I()详解
D.F.S.C.L.A.I 他们都在functions.php这个文件家下面我分别说明一下他们的功能 D() 加载Model类M() 加载Model类 A() 加载Action类L() 获取语言定义C ...
- 理解NSTextContainer
Apple的Doc对这个类的描述是这样的: The NSTextContainer class defines a region where text is laid out. An NSLayout ...
- PAT 06-1 简单计算器
想看一般简单计算器实现的看客不好意思了,这不是你想要点东西,此处题设为“只能进行加减乘除”.“都是整数”.”优先级相同“和"从左到右".此题来自PAT(http://www.pat ...
- C杂记
printf主要是为了便于取地址,默认将char,short变成了int,float变成了double:但scanf的时候,因为数据宽度的问题,必须区分%f和%lf. memset 是字符串操作函数: ...