题目

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

思考

  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. 禁止点击、禁止button触发【c#】

    bts.Attributes["onclick"] = "return false; ";

  2. Flink(九)【Flink的重启策略】

    目录 1.Flink的重启策略 2.重启策略 2.1未开启checkpoint 2.2开启checkpoint 1)不设置重启策略 2)不重启 3)固定延迟重启(默认) 4)失败率重启 3.重启效果演 ...

  3. 【leetcode】563. Binary Tree Tilt

    Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node i ...

  4. MySQL压力测试工具

    一.MySQL自带的压力测试工具--Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新 ...

  5. Linux基础命令---mysql

    mysql mysql是一个简单的sql shell,它可以用来管理mysql数据库. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法      m ...

  6. Spring.DM web开发环境搭建

    作为一个初学者来说,搭建好Spring.DM 的web开发环境还是有些麻烦的.我就遇到了N多麻烦,走了很多弯路.本文介绍了2种比较简单的搭建Spring.DM OSGi web开发环境的搭建.   第 ...

  7. 特定场景下的PLC 远程控制和数据读取

    最近有位博友提出了一种应用场景,根据工作中实际遇到的类似的产品应用场景,记录下自己的解决方案. 场景: 需要在云端控制和采集各个站点的PLC数据.各个站点是分散的,每个站点有公网访问能力,但是分散站点 ...

  8. Jsp/Servlet文件的上传和下载

    文件上传的入门 文件上传的步骤:       总结实现思路: 1.创建核心上传类ServletFileUpload,这个时候需要一个工厂类 2.创建磁盘工厂类对象DiskFileItemFactory ...

  9. 车载以太网第二弹 | 测试之实锤-物理层PMA测试实践

    前言 本期先从物理层"PMA测试"开始,下图1为"PMA测试"的测试结果汇总图.其中,为了验证以太网通信对线缆的敏感度,特选取两组不同特性线缆进行测试对比,果然 ...

  10. PHP数组函数总结与使用

    array_change_key_case(数组,CASE_LOWER/CASE_UPPER) 数组键值转化为小写CASE_LOWER/大写CASE_UPPER   array_chunk(数组,分割 ...