题目

给定行数,生成对应的杨辉三角

思考

  1. 同一行是对称的,最大的下标为(行数+1)/2;1,1,2,3,6;下标从0开始,则对应分别为0.0.1.1.2.2
  2. 对于第偶数行,个数也是偶数,对于奇数行,个数也是奇数
  3. 而且对于非第一列的数,d[i][j]=d[i-1][j-1]+d[i-1][j]

后面通过编程实现,发现自己第一个条件是完全没有用的,可以不需要用到对称性,直接由上一行计算下一行(即第三条)。

而且忘记说最后一列了。

所以新的思考点是:

  1. 对于非第一列/最后一列的数,d[i][j]=d[i-1][j-1]+d[i-1][j]
  2. 第一列和最后一列初始化为1即可

出现过的错误

粗心

由于粗心把当前行数i用总行数numRows代替,所以出现了下标出界的情况Line 18: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

没想清楚

还有没想清楚关于对称点和i、j的关系,所以总是出界。最后考虑取消这个点,从总体情况考虑。

局部变量带来的问题

lineInts声明在函数域中,结果放入总的嵌套List中出了问题。然后在for的局部域中声明这个变量,问题就解决了。

代码

    public List<List<Integer>> generate(int numRows) {
List<List<Integer>> results=new ArrayList<List<Integer>>(numRows); for(int i=0;i<numRows;i++){//i代表当前行数-1
ArrayList<Integer> lineInts=new ArrayList<Integer>();//这一行的数字
lineInts.add(1);//每行第一个是1
int j=1;
for(;j<i;j++){
int sum=results.get(i-1).get(j-1)+results.get(i-1).get(j);
lineInts.add(sum);
}
if(i>0)
lineInts.add(1);//最后加一个1 results.add(lineInts);
}
return results;
}

总结

成绩是32%,应该还有优化的地方,回头再考虑一下吧。

LeetCode118. Pascal's Triangle 杨辉三角的更多相关文章

  1. [leetcode-118]Pascal's triangle 杨辉三角

    Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...

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

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

  3. Pascal's Triangle(杨辉三角)

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

  4. 【LeetCode每天一题】Pascal's Triangle(杨辉三角)

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

  5. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  6. 20190105-打印字母C,H,N,口等图像和杨辉三角

    1. 打印字母C ****** * * * * ****** def print_c(n): print('*' * n) for i in range(n): print('* ') print(' ...

  7. [Swift]LeetCode118. 杨辉三角 | Pascal's Triangle

    Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...

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

  9. LeetCode 118. Pascal's Triangle (杨辉三角)

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

随机推荐

  1. Python序列化,json&pickle&shelve模块

    1. 序列化说明 序列化可将非字符串的数据类型的数据进行存档,如字典.列表甚至是函数等等 反序列化,将通过序列化保存的文件内容反序列化即可得到数据原本的样子,可直接使用 2. Python中常用的序列 ...

  2. Oracle-一张表中增加计算某列值重复的次数列,并且把表中其他列也显示出来,或者在显示过程中做一些过滤

    总结: 1.计算某列值(数值or字符串)重复的次数 select 列1,count( 列1 or *) count1  from table1 group by 列1 输出的表为:第一列是保留唯一值的 ...

  3. mongodb数据库简单类

    <?php/*** Mongodb类** examples: * $mongo = new HMongodb("127.0.0.1:11223"); * $mongo-> ...

  4. hbase调优

    @ 目录 一.phoenix调优 1.建立索引超时,查询超时 2.预分区 hbase shell预分区 phoenix预分区 3.在创建表的时候指定salting. 4.二级索引 建立行键与列值的映射 ...

  5. 学习Java的第三天

    一.今日收获 1.今天家里有白事,忙了一整天,也没有看更多的资料 二.今日问题 无 三.明日目标 补全今天耽误的功课,继续学习java!

  6. MapReduce08 数据清洗(ETL)和压缩

    目录 数据清洗(ETL) ETL清洗案例 需求 需求分析 实现代码 编写WebLogMapper类 编写WebLogDriver类 打包到集群运行 压缩 概念 MR支持的压缩编码 压缩算法对比 压缩性 ...

  7. abort, about

    abort 变变变: abortion:堕胎 abortionist:(非法)做堕胎手术的,不是所有的ist都是scientist, "All that glitters is not go ...

  8. Java SSLSocket

    Java SSLSocket JSSE(Java Security Socket Extension)是Sun公司为了解决互联网信息安全传输提出的一个解决方案,它实现了SSL和TSL协议,包含了数据加 ...

  9. 时光网内地影视票房Top100爬取

    为了和艺恩网的数据作比较,让结果更精确,在昨天又写了一个时光网信息的爬取,这次的难度比艺恩网的大不少,话不多说,先放代码 # -*- coding:utf-8 -*-from __future__ i ...

  10. Spring Cloud中使用Eureka

    一.创建00-eurekaserver-8000 (1)创建工程 创建一个Spring Initializr工程,命名为00-eurekaserver-8000,仅导入Eureka Server依赖即 ...