Pascal's Triangle II

Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution
Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

解:
题目里要求O(k),与上一题 杨辉三角 类似,但是我们稍微改一下,只需要存上一行的结果就行了。
这样就不需要消耗太多内存。

更厉害的是:Inplace也可以,只要你每次从后往前扫描就行了。一个array也能搞定:

 public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> ret = new ArrayList<Integer>(); for (int i = 0; i <= rowIndex; i++) {
for (int j = i; j >= 0; j--) {
if (j == i) {
ret.add(1);
} else if (j != 0) {
// ERROR: use add instead of set
//ret.add(ret.get(j) + ret.get(j - 1));
ret.set(j, ret.get(j) + ret.get(j - 1));
}
}
} return ret;
}
}

GitHub代码链接

LeetCode: Pascal's Triangle II 解题报告的更多相关文章

  1. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  2. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  3. [leetcode]Pascal's Triangle II @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...

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

  5. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  6. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  7. LeetCode - Pascal's Triangle II

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

  8. LeetCode——Pascal's Triangle II

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

  9. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

随机推荐

  1. mysql--SQL编程(基础知识) 学习笔记1

    1.数据库应用类型分类: 一般来说,可将数据库的应用类型分为OLTP(OnLine TransactionProcessing ,联机事务处理)和OLAP(OnLine Analysis Proces ...

  2. TCP连接建立与释放

    tcp建立连接 tcp连接的建立需要经历”三次握手“的过程.过程如下 client发送SYN包(值为j)以及SEQ包到server端,此时client进入SYN_SEND状态.此为第一次握手. ser ...

  3. linux运维常见英文报错中文翻译(菜鸟必知)

    linux常见英文报错中文翻译(菜鸟必知) 1.command not found  命令没有找到 2.No such file or directory  没有这个文件或目录 3.Permissio ...

  4. Ant压缩与解压缩

    package com.test.utils; import java.io.File; import java.io.FileOutputStream; import java.io.InputSt ...

  5. ASP.NET 5 RC 1:UrlRouting 设置(不包含MVC6的UrlRouting设置)

    转自:http://habrahabr.ru/company/microsoft/blog/268037/?mobile=no 1.project.json { "version" ...

  6. Linux伙伴算法

    Linux内存管理伙伴算法 伙伴算法 Linux内核内存管理的任务包括: 遵从CPU的MMU(Memory Management Unit)机制 合理.有效.快速地管理内存 实现内存保护机制 实现虚拟 ...

  7. c2java select algorithm

    对于非常多应用来说,随机算法是最简单的或者最快的.既简单又快的有没有呢? 那须要深刻的洞察力或者革命性的突破. 什么是随机算法 随机算法与确定算法差别是:它还接收输入随机比特流来做随机决策. 对于同一 ...

  8. SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script

    In this post I will introduce a way how to run a script for backing up SharePoint data which could b ...

  9. nginx last 和break redirect 和 permanent

    一.last & break (1)last 和 break 当出现在location 之外时,两者的作用是一致的没有任何差异. 注意一点就是,他们会跳过所有的在他们之后的rewrite 模块 ...

  10. 大批量导入数据的SqlBulkCopy类

     SqlBulkCopy 这个类用于数据库大批量的数据传递,通常用于新旧数据库之间的更新.关键的一点是,即使表结构不同,也可以通过表字段或者字段位置建立映射关系,将所需的数据导入到目标数据库. 下面代 ...