题目

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

思考

  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. 【学相伴】Nginx最新教程通俗易懂-狂神说

    Nginx - 学相伴 分享人:秦疆(遇见狂神说) 公司产品出现瓶颈? 我们公司项目刚刚上线的时候,并发量小,用户使用的少,所以在低并发的情况下,一个jar包启动应用就够了,然后内部tomcat返回内 ...

  2. jmeter+ant输出测试报告

    jmeter自己本身可以输出html测试报告的,不过这种自带的测试报告特别简陋,如下图所示,一般我们是不看这种的. 我们可以使用ant来输出更高效.更直观的测试报告. 首先下载安装ant, 我用的是a ...

  3. 使用Docker编译OpenResty支持国密ssl加密

    编译环境 执行编译操作环境如下 #操作系统 CentOS Linux release 7.4.1708 (Core) #docker版本 Version: 19.03.5 编译过程 Dockerfil ...

  4. MySQL8.0配置文件详解

    mysql8.0配置文件一.关键配置1. 配置文件的位置 MySQL配置文件 /etc/my.cnf 或者 /etc/my.cnf.d/server.cnf几个关键的文件:.pid文件,记录了进程id ...

  5. A Child's History of England.46

    As, one hundred years before, the servile [卑躬屈膝的~serve] followers of the Court had abandoned the Con ...

  6. MapReduce的类型与格式

    MapReduce的类型 默认的MR作业 默认的mapper是Mapper类,它将输入的键和值原封不动地写到输出中 默认的partitioner是HashPartitioner,它对每条记录的键进行哈 ...

  7. c#中实现串口通信的几种方法

    c#中实现串口通信的几种方法 通常,在C#中实现串口通信,我们有四种方法: 第一:通过MSCOMM控件这是最简单的,最方便的方法.可功能上很难做到控制自如,同时这个控件并不是系统本身所带,所以还得注册 ...

  8. Hibernate 总结(转)

    JMX:Java Management Extensions.JCA: J2EE Contector ArchitectureJNDI: Java Namind and Directory Inter ...

  9. Controller返回类的自动识别,WEB-INF,jsp位置

    Controller: @Controller@RequestMapping("/params")public class ParamsController { @RequestM ...

  10. Jenkins视图管理

    目录 一.简介 二.视图维护 创建视图 将项目加入视图中 三.状态图标变绿 四.看板 一.简介 在现在的编程中,公司往往采用的是模块化的编程方式,也就是说将一个项目拆分成许多模块,每个小项目组往往只负 ...