[leetcode]_Pascal's Triangle
题目:题目本身不存在问题,生成Pascal三角。
注意:
ArrayList的使用:
1、ArrayList申请二维数组。
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
2、操作:
ArrayList<Integer> one = new ArrayList<Integer>();
one.add(1);
result.add(one); //在二维List中添加一个List
result.get(0).get(0); //访问第1个List的第1个元素
3、疑问,为什么使用a.clear()和注释掉的两句,res里的之前add进去的内容会有所改变?
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
res.add(a); a.clear(); //ArrayList<Integer> b = new ArrayList<Integer>();
//a = b;
答:使用a.clear(),a指向的那块内存区域的值被洗掉,因此res中add进去的内容同步被洗掉。使用注释掉的两个语句,是将a指向了b指向的内存区域,原来指向的内存区域的那块值并没有改变,这也是为什么a的值被清空了,res中add进去的内容不会改变。
代码:
public ArrayList<ArrayList<Integer>> generate(int numRows) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(numRows == 0) return result;
ArrayList<Integer> pre = new ArrayList<Integer>();
pre.add(1);
result.add(pre);
if (numRows == 1) return result;
for(int index = 2 ; index <= numRows ; index++){
ArrayList<Integer> help = new ArrayList<Integer>();
help.add(1);
for(int i = 0 ; i < pre.size() - 1 ; i++){
help.add(pre.get(i) + pre.get(i + 1));
}
help.add(1);
result.add(help);
pre = help; //只是改变了pre指向的内容,因此add进result的内容不会改变
}
return result;
}
[leetcode]_Pascal's Triangle的更多相关文章
- [leetcode]_Pascal's Triangle II
题目:Pascal三角的变形,要求只用O(K)的额外空间. 思路:由于Pascal三角中,tri[n][i] = tri[n - 1][i] + tri[n-1][i-1],(通常情况下) 如果已经获 ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- 【LeetCode OJ】Triangle
Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...
- leetcode面试准备:Triangle
leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- [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, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [LeetCode 120] - 三角形(Triangle)
问题 给出一个三角形,找出从顶部至底部的最小路径和.每一步你只能移动到下一行的邻接数字. 例如,给出如下三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 从顶部至底部的最 ...
随机推荐
- Intellisense in Visual Studio for Microsoft Dynamics CRM 2016
Intellisense in Visual Studio for Microsoft Dynamics CRM 2016 posted by dynamicsnick on may 18, 2016 ...
- 页面中插入flash,并且给flash添加单击事件控制播放,以及获取相关参数.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100%" hei ...
- ExtGrid
刷新表中数据 Ext.getCmp('SystemManage_role_ContainPresonnel_grid').store.reload(); store.load({ url: '/dat ...
- ArcGIS Engine 下投影坐标和经纬度坐标的相互转换
ArcGIS Engine 下投影坐标和经纬度坐标的相互转换 投影转经纬度 ); pPoint.Project(pSRF.CreateGeographicCoordinateSystem((int)e ...
- JS实例
JS实例 1.跑马灯 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- Sqoop导入mysql数据到Hbase
sqoop import --driver com.mysql.jdbc.Driver --connect "jdbc:mysql://11.143.18.29:3306/db_1" ...
- (转)C#调用非托管Win 32 DLL
转载学习收藏,原文地址http://www.cnblogs.com/mywebname/articles/2291876.html 背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使 ...
- 解决删除/升级Python导致Ubuuntu无法进入桌面的问题
找到问题的原因后于是换个思路,想大概修复了python,Ubuntu进入桌面应该也就没啥问题了.于是重新安装Python发现还是无济于事.也通过/usr/bin/python: can't find ...
- 解决oralce 11g dg搭建报错:ORA-16664、ORA-16714、ORA-16810问题--转
下面不是小编错误报告只是转了网络一篇,同时也解决了我的问题所以复制过来给各位参考. 最近在弄11g的dg时,遇到如下问题,记录下.首先在主上查看报如下错误: DGMGRL> show confi ...
- EF连接mysql数据库生成实体模型
声明:本人也是第一次用EF连接mysql生成实体模型 经过试验: mysql-connector-net-6.6.6 可以支持VS2012 mysql-connector-net-6.3.9 可以支持 ...