本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

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

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

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

  4. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  5. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  6. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  7. Triangle - Delaunay Triangulator

    Triangle - Delaunay Triangulator  eryar@163.com Abstract. Triangle is a 2D quality mesh generator an ...

  8. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  9. 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 ...

随机推荐

  1. python if判断语句&计算

    python对缩进要求严格,代码块里的缩进必须一样,可以常用 tab键  表示4个空格 if 条件: 代码块 else: if判断语句如下: 1 print("吃饭,喝水,回家") ...

  2. Docker快速配置指南

    下面是一个跟 Docker 网络相关的命令列表. 其中有些命令选项只有在 Docker 服务启动的时候才能配置,而且不能马上生效. -b BRIDGE or --bridge=BRIDGE --指定容 ...

  3. 导出和导入Docker容器

    导出容器 如果要导出本地某个容器,可以使用 docker export 命令. $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATU ...

  4. OpenCV RGB2LAB执行效率测试

    代码 #include <iostream> #include <vector> #include <opencv2/opencv.hpp> #define ERR ...

  5. 给定 n×n 的实数矩阵,每行和每列都是递增的,求这 n^2 个数的中位数。

    #define COL 4 #define ROW 4 int findMedian(int matrix[][COL], int row, int col) { int* arr = new int ...

  6. Spring声明式事务总结

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...

  7. R语言:安装及使用

    http://blog.csdn.net/pipisorry/article/details/53640638 ubuntu下安装 sudo apt-get install -y r-base源码安装 ...

  8. Bootstrap3 代码-内联代码

    通过 <code> 标签包裹内联样式的代码片段. For example, <section> should be wrapped as inline. For example ...

  9. Android简易实战教程--第四十一话《vitamio网络收音机》

    在Android初级教程专栏里面,介绍了Android原生的VideoView和vitamio框架Android视频媒体相关,VideoView和开源框架vitamio.并演示了播放网络视频的对应的D ...

  10. Scheme N皇后

    (define (range n) (define (recur n) () '() (cons n (recur (- n ))))) (recur (- n ))) (define (flatte ...