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

解题思路:

观察法,JAVA实现如下:

    public List<List<Integer>> generate(int numRows) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if(numRows<=0)
return list;
List<Integer> alist=new ArrayList<Integer>();
alist.add(1);
list.add(new ArrayList<Integer>(alist));
for(int i=2;i<=numRows;i++){
List<Integer> alist2=new ArrayList<Integer>();
alist2.add(1);
for(int j=1;j<i-1;j++)
alist2.add(alist.get(j-1)+alist.get(j));
alist2.add(1);
alist=alist2;
list.add(new ArrayList<Integer>(alist));
}
return list;
}

Java for LeetCode 118 Pascal's Triangle的更多相关文章

  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. leetcode 118 Pascal's Triangle ----- java

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

  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. Java for LeetCode 119 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 ...

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

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

随机推荐

  1. android的多次点击事件的实现(有源码)

    三次点击事件的原理图:数组的复制(android源码的调用): 下面就是第一步: 创建long数组,里面的数字代表点击的次数. 下面是主要代码实现: system.arraycopy();里面的参数描 ...

  2. If Value Exists Then Query Else Allow Create New in Oracle Forms An Example

    An example given below for Oracle Forms, when a value exists then execute query for that value to di ...

  3. mac 下mysql常用命令

    是那种单独安装的mysql 启动: /usr/local/mysql/bin/mysql -u root -p

  4. 实现页面切换(动画效果实现,不用ViewPager)

    源代码地址 http://download.csdn.net/detail/u013210620/8791687 先看主页面布局activity_main <?xml version=" ...

  5. 转: Linux下使用java -jar运行可执行jar包的正确方式

    from:  http://codepub.cn/2016/05/11/The-correct-way-to-use-java-jar-run-an-executable-jar-package-un ...

  6. iframe 实现网页本页显示

    <el-dialog title="" :visible.sync="dialogVisible"> <iframe src="ht ...

  7. asp.net模拟请求

    在asp.net模拟请求,微软在控件状态有安全性控制. __VIEWSTATE.__EVENTVALIDATION要与服务端页面(.aspx)中元素信息保存一致.

  8. SQL存在一个表而不在还有一个表中的数据

    select a.id,a.oacode,a.custid,a.custname,a.xsz,a.salename,a.communicationtheme,a.communicationproper ...

  9. Linux环境变量PS1配置

    1. 说明: 在Shell下,我们能够拥有更加色慘斑斓的提示行信息.这能够通过改变bash的$PS1环境变量还设置,如以下就是提示行的一种: user@host$ root用户的提示是这种: user ...

  10. Nginx在windows2003下的使用 PHP

    nginx真正能够发挥其良好的负载能力的,是在linux下. 我们在windows下搭建好环境,测试开发使用.强烈建议在linux下开发的. ---- 下载nginx 访问www.nginx.org, ...