[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] ] 从顶部至底部的最 ...
随机推荐
- android 插件化 模块化开发
http://blog.csdn.net/o1587790525/article/details/11891997 Android 插件化架构设计 http://www.iqiyi.com/w_19 ...
- SQLServer 2008以上误操作数据库恢复方法——日志尾部备份(转)
问题: 经常看到有人误删数据,或者误操作,特别是update和delete的时候没有加where,然后就喊爹喊娘了.人非圣贤孰能无过,做错可以理解,但不能纵容,这个以后再说,现在先来解决问题. 遇到这 ...
- SQL Server 2005中的分区表(六):将已分区表转换成普通表(转)
我的俄罗斯名叫作“不折腾不舒服斯基”,所以,不将分区表好好折腾一下,我就是不舒服. 在前面,我们介绍过怎么样直接创建一个分区表,也介绍过怎么将一个普通表转换成一个分区表.那么,这两种方式创建的表有什么 ...
- Eclipse Android HH and theme
一. 汉化方法: 1.Eclipse版本查询:安装目录readme,查版本号;参照查代号如下表: 代号 平台版本 项目 主要版本发行日期 SR1发行日期 SR2发行日期 N/A 3.0 [1] N/A ...
- Network of Schools(强连通分量缩点(邻接表&矩阵))
Description A number of schools are connected to a computer network. Agreements have been developed ...
- BC之The mook jong
Problem Description ZJiaQ want to become a strong man, so he decided to play the mook jong.ZJiaQ wan ...
- android 资源文件
系统文档:http://developer.android.com/guide/topics/resources/available-resources.html 1. 系统下资源文件夹的名字是固定的 ...
- NFS挂载启动
NFS挂载启动参数: 1.服务器IP.目录(虚拟机IP和 NFS目录) 2.开发的IP 如下我的开发板设置 ipaddr=192.168.1.17 ① 开发板IP serverip ...
- android 基础控件 EditText
EditText 简介: EditText 控件继承 TextView ,它有TextView的所有属性和方法,并且自身是可编辑的: extends TextView java.lang.Object ...
- SDUT 3346 数据结构实验之二叉树七:叶子问题
数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...