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 ...
随机推荐
- python 操作json
认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Dece ...
- Cisco IOS Security command Guide
copy system:running-config nvram:startup-config : to save your configuration changes to the startup ...
- 多功能节点连线绘图控件Nevron Diagram for .NET使用方法及下载地址
Nevron Diagram for .NET是一个功能强大,世界上顶级的.NET图表控件.可扩展的图形报表构架,可以帮您创建功能丰富的Winforms及Webforms图表解决方案.这个产品构建于N ...
- 支持多人协作的在线免费作图工具:ProcessOn
之前朋友给我推荐一款作图工具ProcessOn,出于好奇我就研究了一下它,今天我就给大家简单介绍一下这款免费的在线作图工具:ProcessOn 首先使用ProcessOn我们需要有一个帐号,这样每次操 ...
- [网络技术][转]PPTP协议解析
PPTP协议大体上可以分为两部分:控制层连接和隧道,下面简要介绍两部分的功能.如果要详细了解PPTP协议请阅读RFC文档. 一. Control Connection Protol 控制层连接是基于T ...
- python解无忧公主数学题107.py
python解无忧公主数学题107.py """ python解无忧公主数学题107.py http://mp.weixin.qq.com/s?__biz=MzI5ODE ...
- Centos 6 安装 epel yum库
1.获得epel库安装rpm包 wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm 2.安装获得的r ...
- Ubuntu 14.10 下安装java反编译工具 jd-gui
系统环境,Ubuntu 14.10 ,64位 1 下载JD-GUI,网址http://221.3.153.126/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/jd.benow.ca/ ...
- C# 调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配
在dllimport中加入CallingConvention参数就行了,[DllImport(PCAP_DLL, CharSet = CharSet.Auto, CallingConvention = ...
- 转载 javascript中的正则表达式总结 二
学习正则表达式 今年的第一篇javascript文章就是这个正则表达式了,之前的文章是转载别人的,不算自己的东西,可以忽略不计,最近突然想把转载别人的东西 统统删掉,因为转载过的文章,我根本没有从中获 ...