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 ...
随机推荐
- win10 ctrl + 空格 热键取消
关键:添加English,并上移到第一,即开机默认即可.Win8+不用修改注册表的,只有win7里才可能需要修改注册表. http://superuser.com/questions/327479/c ...
- 戴文的Linux内核专题:04安全
转自Linux中国 Linux内核是所有Linux系统的核心.如果有任何恶意代码控制或破害了内核的任何一部分,那么系统会严重受损,文件可能被删除或损坏,私人信息可能被盗等等.很明显,保持内核安全涉及到 ...
- CodeForces 546B C(Contest #1)
Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...
- C++ offsetof
这是一个宏,用于计算类中某个成员的地址相对于类实例的偏移量 在C++11中,要求这个类standard_layout 基本用法是这样子的: #include <stdio.h> /* pr ...
- leetcode 229 Majority Element II
这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...
- javascript splice
//arrayObject.splice(index,howmany,element1,.....,elementX)//index 必需.规定从何处添加/删除元素.(0)//howmany 必需.规 ...
- 考古备份:a.out文件ELF文件头中魔数的由来
来源: <程序员的自我修养>3.4节. 补充: http://wiki.osdev.org/ELF http://www.linux-mag.com/id/116/ http://en.w ...
- 当前页面js代码
<script type="text/javascript"> var url = location.href; $(function(){ var url = loc ...
- JLOI2010 冠军调查 最小割
var b,f:..] of longint; s,t,i,j,n,m,x,y:longint; l,h:..]of longint; a:..,..]of longint; procedure bf ...
- CODEVS1380 没有上司的舞会 (树形DP)
f[i,0] 表示 第i个人不参加舞会 f[i,1] 表示 第i个人参加舞会 f[i,1]=sigma(f[j,0])+v[i] j 为 i 的孩子 f[i,1]=sigma(max(f[j,0] ...