Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

  1. [
  2. [1],
  3. [1,1],
  4. [1,2,1],
  5. [1,3,3,1],
  6. [1,4,6,4,1]
  7. ]

用的比价暴力的方法, 也是最快的。

  1. public class Solution {
  2. List list = new ArrayList<List<Integer>>();
  3.  
  4. public List<List<Integer>> generate(int numRows) {
  5.  
  6. for( int i = 0;i<numRows;i++)
  7. help(i+1);
  8.  
  9. return list;
  10. }
  11.  
  12. public void help(int num){
  13.  
  14. List ans = new ArrayList<Integer>();
  15.  
  16. if( num == 1){
  17. ans.add(1);
  18. list.add(ans);
  19. return ;
  20. }else if( num == 2 ){
  21. ans.add(1);
  22. ans.add(1);
  23. list.add(ans);
  24. return ;
  25. }
  26. ArrayList<Integer> last = (ArrayList<Integer>) list.get(num-2);
  27. ans.add(1);
  28. int a = last.get(0);
  29. int b = last.get(1);
  30. for( int i = 2;i<last.size();i++){
  31. ans.add(a+b);
  32. a = b;
  33. b = last.get(i);
  34. }
  35. ans.add(a+b);
  36. ans.add(1);
  37. list.add(ans);
  38. }
  39. }

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. win10 ctrl + 空格 热键取消

    关键:添加English,并上移到第一,即开机默认即可.Win8+不用修改注册表的,只有win7里才可能需要修改注册表. http://superuser.com/questions/327479/c ...

  2. 戴文的Linux内核专题:04安全

    转自Linux中国 Linux内核是所有Linux系统的核心.如果有任何恶意代码控制或破害了内核的任何一部分,那么系统会严重受损,文件可能被删除或损坏,私人信息可能被盗等等.很明显,保持内核安全涉及到 ...

  3. CodeForces 546B C(Contest #1)

    Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...

  4. C++ offsetof

    这是一个宏,用于计算类中某个成员的地址相对于类实例的偏移量 在C++11中,要求这个类standard_layout 基本用法是这样子的: #include <stdio.h> /* pr ...

  5. leetcode 229 Majority Element II

    这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...

  6. javascript splice

    //arrayObject.splice(index,howmany,element1,.....,elementX)//index 必需.规定从何处添加/删除元素.(0)//howmany 必需.规 ...

  7. 考古备份:a.out文件ELF文件头中魔数的由来

    来源: <程序员的自我修养>3.4节. 补充: http://wiki.osdev.org/ELF http://www.linux-mag.com/id/116/ http://en.w ...

  8. 当前页面js代码

    <script type="text/javascript"> var url = location.href; $(function(){ var url = loc ...

  9. JLOI2010 冠军调查 最小割

    var b,f:..] of longint; s,t,i,j,n,m,x,y:longint; l,h:..]of longint; a:..,..]of longint; procedure bf ...

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