Leetcode_118_Pascal's Triangle
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41827325
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
思路:
(1)这道题所说的Pascal's Triangle实质就是杨辉三角,题意是给定整数N,输出杨辉三角中1-N行中包括的所有数字。
(2)大家肯定都挺熟悉杨辉三角,对于三角中每一行:每行数字的个数和行数相等,第一个和最后一个数字是1,中间任意一个数字是该数字对应在该行正上方两数字的和。
(3)首先,创建LinkedList来存放每一行的数字,由于三角中第1行和第2行比较特殊,需单独进行判断和处理。
(4)其次,循环遍历N次,正好对应N行,行数等于1和等于2分别返回对应值;如果行数大于2,则需遍历,如果遍历到该行第一个元素和最后一个元素则将1加入临时链表row中,对于中间的元素,肯定是其前一行对应元素相加(如果在m(m>2)行中,某一元素为k(1<k<m-1),则k的值为m-1行中的第k-1个元素和低k个元素相加),由于已经把前2行数字按顺序存入rows中,所以取到指定元素顺序不会发生变化。
(5)最后,每遍历一行就将得到的临时链表存入rows中,在遍历下一行时就能按照行数获取到上一行的数字,从而得到这一行中间任意数字的值。
(6)这道题还是比较简单的。本文仅提供一种解题思路,对于时间效率和空间效率的优化暂未考虑,希望对你有所帮助,谢谢。
算法代码实现如下所示:
public static List<List<Integer>> getAll(int num) {
List<List<Integer>> rows = new LinkedList<List<Integer>>();
//num太大可能越界
for (int i = 1; i <= num; i++) {
List<Integer> row = new LinkedList<Integer>();
if (i == 1) {
row.add(1);
}
if (i == 2) {
row.add(1);
row.add(1);
}
if (i >= 3) {
for (int j = 0; j < i; j++) {
if (j == 0) {
row.add(1);
}else if (j == i - 1) {
row.add(1);
}else if (j != 0 && j != i - 1) {
List<Integer> list = rows.get(i - 2);
row.add(list.get(j - 1) + list.get(j));
}
}
}
rows.add(row);
}
return rows;
}
当N=10时,运行结果如下所示:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
Leetcode_118_Pascal's Triangle的更多相关文章
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [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 II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- Triangle - Delaunay Triangulator
Triangle - Delaunay Triangulator eryar@163.com Abstract. Triangle is a 2D quality mesh generator an ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
随机推荐
- 在java中如何使用etcd的v2 和v3 api获取配置,并且对配置的变化进行监控
etcd 和zookeeper 很像,都可以用来做配置管理.并且etcd可以在目前流行的Kubernetes中使用. 但是etcd 提供了v2版本合v3的版本的两种api.我们现在分别来介绍一下这两个 ...
- HTML DOM 改变 HTML 内容
HTML DOM 允许 JavaScript 改变 HTML 元素的内容. 改变 HTML 输出流 JavaScript 能够创建动态的 HTML 内容: 今天的日期是: Thu Feb 25 201 ...
- iOS开发基础之设置状态栏和二维码的unspported type found 问题
最近遇到设置状态栏和二维码AVCaptureMetadataOutput setMetadataObjectTypes: unspported type found的错误问题,所以我在这里进行总结一下 ...
- 防止SpringMVC拦截器拦截js等静态资源文件
SpringMVC提供<mvc:resources>来设置静态资源,但是增加该设置如果采用通配符的方式增加拦截器的话仍然会被拦截器拦截,可采用如下方案进行解决: 方案一.拦截器中增加针对静 ...
- Bootstrap3 表格-鼠标悬停
通过添加 .table-hover 类可以让 <tbody> 中的每一行对鼠标悬停状态作出响应. <table class="table table-hover" ...
- JUnit单元测试教程(翻译自Java Code Geeks)
JUnit单元测试教程--终极指南 JUnit单元测试教程终极指南 说明 单元测试简介 1 什么是单元测试 2 测试覆盖 3 Java中的单元测试 JUnit简介 1 使用Eclipse实现简单JUn ...
- 聚沙成塔-linux 常用命令
批量更改文件后缀名 find . -depth -name "*.scss" -exec sh -c 'mv "$1" "${1%.scss}.les ...
- Spring Security安全框架入门篇
一.Spring Security相关概念 1.1..Spring Security介绍: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安 ...
- Dynamics CRM Trigger plugin for N:N relationships
博客原文:https://demystifyingcrm.wordpress.com/2014/12/17/trigger-plugin-for-nn-relationships-in-dynamic ...
- Docker学习笔记4: Docker-Compose—简化复杂容器应用的利器
本文转载自http://www.tuicool.com/articles/AnIVJn. 因Python语言,个人也没学过,不是太熟悉,这篇文章的代码格式排版不准确爆了很多错,让我走了好多坑,不过还是 ...