题目:题目本身不存在问题,生成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的更多相关文章

  1. [leetcode]_Pascal's Triangle II

    题目:Pascal三角的变形,要求只用O(K)的额外空间. 思路:由于Pascal三角中,tri[n][i] = tri[n - 1][i] + tri[n-1][i-1],(通常情况下) 如果已经获 ...

  2. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  3. 【LeetCode OJ】Triangle

    Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...

  4. leetcode面试准备:Triangle

    leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...

  5. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  6. [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, ...

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

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

  8. LeetCode——Pascal's Triangle

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

  9. [LeetCode 120] - 三角形(Triangle)

    问题 给出一个三角形,找出从顶部至底部的最小路径和.每一步你只能移动到下一行的邻接数字. 例如,给出如下三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 从顶部至底部的最 ...

随机推荐

  1. codeforce--Vasya and Petya's Game

    网址:http://codeforces.com/contest/576/problem/A A. Vasya and Petya's Game time limit per test 1 secon ...

  2. sqlite支持的数据库类型

    http://www.sqlite.org/datatype3.html 默认只支持:NULL,INTEGER,REAL,TEXT,BLOB

  3. css required,focus,valid和invalid介绍

    本文章来给大家介绍在css3定义required,focus,valid和invalid样式的方法,此方法目前只支持ie9+及ff,gg浏览器哦.css3 提示只适用于高级浏览器:ChromeFire ...

  4. Integer cache

    View.findViewById采用深度遍历,找到第一个匹配的控件 Integer Cache public static void testIntegerCache() { Class cache ...

  5. Sqoop2入门之导入关系型数据库数据到HDFS上

    需求:将hive数据库中的TBLS表导出到HDFS之上: $SQOOP2_HOME/bin/sqoop.sh client sqoop:> set server --host hadoop000 ...

  6. spark-sql启动后在监控页面中显示的Application Name为SparkSQL::xxxx的疑问

    启动spark-sql执行sql时,在监控页面中看到该Application的Name是SparkSQL:hadoop000(其中hadoop000是测试机器的hostname),就有个想法,修改下该 ...

  7. Hadoop JobHistory

    hadoop jobhistory记录下已运行完的MapReduce作业信息并存放在指定的HDFS目录下,默认情况下是没有启动的,需要配置完后手工启动服务. mapred-site.xml添加如下配置 ...

  8. fw:学好Python必读的几篇文章

    学好Python必读的几篇文章 from:http://blog.csdn.net/hzxhan/article/details/8555602 分类: python2013-01-30 11:52  ...

  9. 测试一个域名DNS查询时间的shell脚本

    脚本内容: #!/bin/bash #目标域名 site=${site:-www.ptesting.com} for((i=1;i<=10000;i++)) do     #COUNTER='e ...

  10. DOJO官方API翻译或解读-dojo/store (自定制存储器)

    dojo/store 是对已存数据的访问和存储的统一接口,dojo/store意图以一个简单.易于使用和扩展的API来,替代.集合和改善 dojo/data 和dojox/storage .基于HTM ...