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

思路:动态规划,前面的构建出来了,后面根据某种关系也就可以构建出来。

这个要根据前一行来构建下一行。这里的for循环是直接遍历当前行的个数,往里面添加多少个。这里主要运用当前行的规律

 public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result=new ArrayList<List<Integer>>();
if(numRows==0) return result;
List<Integer> row,pre=null;//row代表当前行,pre代表前一行
for(int i=1;i<=numRows;i++){//第几行,第几行就有几个元素
row=new ArrayList<>();
      /*这里for循环中的步骤很巧妙,遍历当前行,若为开头或结尾,加1,其他情况再从前一行中计算,这样一开始就不会从前一行计算,所以不会出问题。
        
      */
for(int j=0;j<i;j++){//当前行的元素下标
if(j==0||j==i-1)
row.add(1);
else
row.add(pre.get(j-1)+pre.get(j));//一开始第一行第二行这里不会执行,
}
pre=row;
result.add(row);
} return result; }

还有一种更巧妙的,直接在一个list上操作。见代码。注意最后添加的时候new一个list,不然只能添加最终结果

class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(numRows==0) return res;
List<Integer> list=new LinkedList<>();
for(int i=1;i<=numRows;i++){
list.add(1);
for(int j=i-2;j>=1;j--){
list.set(j,list.get(j)+list.get(j-1));
}
//这里要新new一个list加进去,不然如果直接添加list,出现的情况是添加了最终结果(第一层for循环结束时的list)。
res.add(new LinkedList<Integer>(list));
}
return res;
}
}

Pascal's Triangle(杨辉三角)的更多相关文章

  1. [LeetCode] Pascal's Triangle 杨辉三角

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

  2. [leetcode-118]Pascal's triangle 杨辉三角

    Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  3. LeetCode118. Pascal's Triangle 杨辉三角

    题目 给定行数,生成对应的杨辉三角 思考 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2 对于第偶数行,个数也是偶数,对于奇数行,个 ...

  4. 【LeetCode每天一题】Pascal's Triangle(杨辉三角)

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  5. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. 20190105-打印字母C,H,N,口等图像和杨辉三角

    1. 打印字母C ****** * * * * ****** def print_c(n): print('*' * n) for i in range(n): print('* ') print(' ...

  7. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

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

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

  9. [Swift]LeetCode118. 杨辉三角 | Pascal's Triangle

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

随机推荐

  1. Qt中事件分发源代码剖析

    Qt中事件分发源代码剖析 Qt中事件传递顺序: 在一个应该程序中,会进入一个事件循环,接受系统产生的事件,并且进行分发,这些都是在exec中进行的. 下面举例说明: 1)首先看看下面一段示例代码: i ...

  2. FFmpeg API 变更记录

    最近一两年内FFmpeg项目发展的速度很快,本来是一件好事.但是随之而来的问题就是其API(接口函数)一直在发生变动.这么一来基于旧一点版本的FFmpeg的程序的代码在最新的类库上可能就跑不通了. 例 ...

  3. const引用

    在C++中可以声明const引用 const Type& name = var: const引用让变量拥有只读属性 const int &a = b const int &a ...

  4. (八十七)AutoLayout的简介与实例

    AutoLayout是继AutoResizing之后的一种自动布局方法,解决了AutoResizing无法处理控件间相互关系的问题. AutoLayout在storyboard中通过底部工具条设置,底 ...

  5. mysql中 REPLACE INTO 和 INSERT INTO 的区别

    mysql中 REPLACE INTO 和 INSERT INTO 的区别 REPLACE INTO 和 INSERT INTO 功能类似,都是像表中插入数据,不同点在于:REPLACE INTO 首 ...

  6. 【翻译】如何创建Ext JS暗黑主题之二

    原文:How to Create a Dark Ext JS Theme– Part 2 我已经展示了如何去开发一个精致的暗黑主题,看起来就像Spotify.在本文的第一部分,了解了Fashion.S ...

  7. 【leetcode】经典算法题-Counting Bits

    题目描述: 给定一个数字n,统计0-n之间的数字二进制的1的个数,并用数组输出 例子: For num = 5 you should return [0,1,1,2,1,2]. 要求: 算法复杂复o( ...

  8. 使用spine骨骼动画制作的libgdx游戏

    (官网:www.libgdx.cn) Super Spineboy是一个使用Spine和libgdx开发的跨平台游戏(Windows,Mac,Linux),Spine是一个2D游戏动画工具.Super ...

  9. 详解ebs接口之客户配置文件导入(一)

    DECLARE l_rec_profile_t hz_customer_profile_v2pub.customer_profile_rec_type; l_rec_profile hz_custom ...

  10. spring揭秘 读书笔记 六 bean的一生

    我们知道,Spring容器具有对象的BeanDefinition来保存该对象实例化时需要的数据. 对象通过container.getBean()方法是才会初始化该对象. BeanFactory 我们知 ...