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的更多相关文章

  1. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  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- ...

  3. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  4. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  5. Java for LeetCode 118 Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  6. Java [Leetcode 118]Pascal's Triangle

    题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  7. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  8. 118. Pascal's Triangle (java)

    问题描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  9. Leetcode 118 Pascal's Triangle 数论递推

    杨辉三角,即组合数 递推 class Solution { vector<vector<int>> v; public: Solution() { ; i < ; ++i ...

随机推荐

  1. HDU 4085 斯坦纳树

    题目大意: 给定无向图,让前k个点都能到达后k个点(保护地)中的一个,而且前k个点每个需要占据后k个中的一个,相互不冲突 找到实现这个条件达到的选择边的最小总权值 这里很容易看出,最后选到的边不保证整 ...

  2. 开发基于Handoff的App(Swift)

            iOS8推出一个新特性,叫做Handoff.Handoff中文含义为换手(把接力棒传给下一个人),可以在一台Mac和iOS设备上开始工作,中途将工作交换到另一个Mac或iOS设备中进行 ...

  3. 调整label中text显示的行间距

    调整label中text显示的行间距最近再做一个项目时,发现UILabel中text的系统默认行间距不能满足要求,于是在网上找到了调整行间距的代码.跟大家分享一下,希望能对你有所帮助.悦德财富:htt ...

  4. java.lang包的分类

    提供利用 Java 编程语言进行程序设计的基础类. 1>  最重要的类是 Object(它是类层次结构的根)和 Class(它的实例表示正在运行的应用程序中的类).   2>  把基本类型 ...

  5. rcc

    一.在STM32中,有五个时钟源,为HSI.HSE.LSI.LSE.PLL.   全名: high  speed  external ①HSI是高速内部时钟,RC振荡器,频率为8MHz. ②HSE是高 ...

  6. struts2+hibernate整合开发步骤

    百度的各种代码,步骤,自己整合了一下 1,创建数据库 常用mysql   creat table..... 2,在WebContent下的bin中添加相应的包 http://pan.baidu.com ...

  7. HttpWebRequest与HttpWebResponse使用例子(转)

    转自:http://www.jb51.net/article/28401.htm 在每个系统出写入报告错误代码(找个合理的理由,比如系统免费升级) -> 自家服务器接收并处理错误报告 -> ...

  8. jQuery之load、unload、onunload和onbeforeunload

    1.load:jQuery load() 方法是简单但强大的 AJAX 方法.load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法:$(selector).load(URL,dat ...

  9. PHP面向对象中常用的关键字和魔术方法

    PHP面向对象中常用的关键字 final        1.final不能修饰成员属性(类中常量不是用这个关键字)        2.final只能修饰类和方法 作用:            使用fi ...

  10. 【LeetCode OJ】Sum Root to Leaf Numbers

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...