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?

解题思路1(实现巧妙,但时间复杂度高):

空间复杂度O(k),时间复杂度O(k^2)

Pascal's Triangle类似,每一行数列,从后往前找规律:f(k)(n) = f(k-1)(n) + f(k-1)(n-1)

为了只使用O(k)空间,下一层数列可以在旧层数列基础上,从后往前更新。新一层已经更新的数字,不会影响到新一层中,还未更新的数字的计算。

代码:

 class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> row(rowIndex + ); row[] = ;
for(int i = ; i <= rowIndex; i++)
for(int j = i; j >= ; j--)
if (j == i)
row[j] = row[j-];
else if (j == )
row[j] = row[j];
else
row[j] = row[j-] + row[j]; return row;
}
};

解题思路2(寻找公式,直接得出):

空间复杂度O(k),时间复杂度O(k)

根据数字规律,给出一个k,可以直接得到对应数列,不必通过前一个数列求值。

公式:

f(0) = 1;

f(n) = f(n-1)(k-n+1) / i      n∈[1,k]

记录1(int越界出错):

当k=30时,求出f(14),要求f(15)时,由于要先乘(k-n+1)再除 i,因此做乘法时数值超出了int的最大范围+2147483647;

代码(未通过):

 class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> row(rowIndex + );
row[] = ; for (int i=; i<=rowIndex; ++i) {
row[i] = row[i-] * (rowIndex-i+) / i;
} return row;
} };

修改(AC):

将运算中的值暂时double,结果再转回int(12行)

 class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> row(rowIndex + );
row[] = ; for (int i=; i<=rowIndex; ++i) {
row[i] = int(double(row[i-]) * double(rowIndex-i+) / i);
} return row;
} };

附录:

杨辉三角与计算机

【Leetcode】【Easy】Pascal's Triangle II的更多相关文章

  1. LeetCode Array Easy 119. Pascal's Triangle II

    Description Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's tria ...

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

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

  3. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  4. 【LEETCODE】34、119题,Pascal's Triangle II

    package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...

  5. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  6. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  7. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. LeetCode OJ 119. 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] Pascal's Triangle II,剑指Offer 题4

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

  10. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

随机推荐

  1. 队列 102 Binary Tree Level Order Traversal

    队列的基本应用 - 广度优先遍历 1)树 : 层序遍历: 2)图:无权图的最短路径. 使用队列来实现二叉树的层序遍历,需要多关注一个层数的信息 /** * Definition for a binar ...

  2. zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)

    题目传送门 题目大意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai,现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走 ...

  3. UVA - 10635 LIS LCS转换

    白书例题,元素互不相同通过哈希转换为LIS求LCS #include<iostream> #include<algorithm> #include<cstdio> ...

  4. mysql远程连接详细配置

    2018-11-06 CentOS 配置MySQL允许远程登录 Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下 ...

  5. UML-3-案例研究

    没有什么比恰当的举例更难的了.-----马克吐温

  6. Java变量类型,实例变量 与局部变量 静态变量

    实例变量: 实例变量在类中声明,但在方法的外面,构造函数或任何块. 当空间分配给某个对象在堆中,插槽为每个实例变量创建值. 当一个对象与使用关键字 “new” 来创建,在对象被销毁销毁创建的实例变量. ...

  7. ubuntu系统在安装好mysql后,出现ERROR 2002(HY000: Can't to local MySQL server through socket '/var/run/mysqld/mysqld.sock')(2)(图文详解)

    不多说,直接上干货! 问题详情 我在写此博客之前,看了网上各种资料,写的太冗余和繁琐杂乱.最简单的解决方法莫过于我这篇博客.直接如下. 这是liux套接字网络的特性,win平台不会有这个问题. 解决方 ...

  8. zookeeper 编程框架 curator

    Curator框架提供了一套高级的API, 简化了ZooKeeper的操作. 它增加了很多使用ZooKeeper开发的特性,可以处理ZooKeeper集群复杂的连接管理和重试机制. 这些特性包括: 自 ...

  9. TOJ 1883 Domino Effect

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  10. MYSQ系列-MYSQL基础增强(Mysql基本语句)

    MYSQL基础增强 库操作 创建一个使用UTF-8字符集的数据库: create database mydb character set UTF8; 创建一个带校对集的数据库 create datab ...