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. MVC4 Task.Factory.StartNew 异步调用

    MVC4也添加了一些异步的东西,不过一枝都没有研究过. 工作上遇到了发出一个调用,但是不去管调用结果如何的情况,在谢平师傅的指导下, 写成如下异步方式 Task.Factory.StartNew(() ...

  2. Android性能优化第(三)篇---MAT比Menmery Monitor更强大

    作者 LooperJing 2016.11.17 16:42* 字数 1687 阅读 1603评论 3喜欢 21 在Android性能优化第(一)篇---基本概念中讲了JAVA的四大引用,讲了一下GC ...

  3. VS2010 MFC中 单独添加ODBC数据库记录集类(CRecordset)方法

    基于VS2010 MFC的项目是之前建好的,后来需要添加数据库. 方法分享于此. 1.  打开自己的项目,项目->添加类. 2. 选MFC ODBC使用者,点右下角的添加. 3. 点数据源. / ...

  4. ylb:SQL 视图(View)基础

    ylbtech-SQL Server: SQL Server-SQL 视图(View)基础 SQL 视图(View)基础. 1,ylb:视图(View)基础返回顶部 -- ============== ...

  5. Android开发之布局文件里实现OnClick事件关联处理方法

    一般监听OnClickListener事件,我们都是通过Button button = (Button)findViewById(....); button.setOClickLisener....这 ...

  6. 移动端开发者福利-免费收费api收藏

    一 .api 1.https://www.juhe.cn/ 跟百度api集市差不多,超级赞,做好认证就行了,我有20+认证能用的免费api 2.http://apistore.baidu.com/as ...

  7. 网络电台(WIZ550io)

    网络电台是用WIZ550io(内嵌MAC地址)和ATMEGA1284(Flash 128K,EEPROM4K)制作的.用户可注冊多达80个无线电广播. 无线电广播的注冊可在内嵌网页中进行. 网络电台的 ...

  8. Windows下Tomcat+nginx配置证书实现登录页https访问

    最近公司出于安全考虑,需要将登录页做成https访问,其他页面仍采用http访问,环境是Linux平台,web服务器采用Tomcat + Nginx.之前没接触过nginx,这两天网上查资料,试了好多 ...

  9. php计算两个经纬度地点之间的距离(转)

    php计算两个指定的经纬度地点之间的距离,这个在做计算给定某个地点的经纬度,计算其附近的商业区,以及给定地点与附近各商业区之间的距离的时候,还是用的到的.下面是具体的函数代码以及用法示例. 关于如何获 ...

  10. 也谈SQL Server 2008 处理隐式数据类型转换在运行计划中的增强 (续)

    在上一篇文章也谈SQL Server 2008 处理隐式数据类型转换在运行计划中的增强中,我提到了隐式数据类型转换添加对于数据分布非常不平均的表.评估的数据行数与实际值有非常大出入的问题,进一步測试之 ...