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. Qt随笔 - QSettings

    QSettings类提供了持久的跨平台应用程序设置. 嗯,一句话概括QSettings-- 创建 来看一下原型: QSettings::QSettings(const QString &org ...

  2. SQL数据库查询一张表新建一个排序字段并根据某列的排序存储排序值

    现在有一张表如下Id Name Age Classify Score1 张一 18 一班 122 张二 17 二班 19 3 张三 19 三班 30 我跟据他们的分数进行排名 再去新建一个列存储排序值 ...

  3. jQuery常用的方法

    each() 以每一个匹配的元素作为上下文来执行一个函数. size()  jQuery 对象中元素的个数.

  4. 前后端分离和restful开发规范

    一.web开发的两种模式 1.前后端不分离 在前后端不分离的应用模式中,前端页面看到的效果都是由后端控制,由后端渲染页面或重定向,也就是后端需要控制前端的展示,前端与后端的耦合度很高. 这种应用模式比 ...

  5. Git merge 和 rebase 进一步比较

    但是 假如 我不想看到 分支转折点呢 合并的分支始终会存在一个交叉点 Microsoft Windows [版本 10.0.17134.345] (c) Microsoft Corporation.保 ...

  6. java中的线程(1):如何正确停止线程Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?

    转自 : http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html 1.Why is Th ...

  7. 设计模式学习总结(八)策略模式(Strategy)

    策略模式,主要是针对不同的情况采用不同的处理方式.如商场的打折季,不同种类的商品的打折幅度不一,所以针对不同的商品我们就要采用不同的计算方式即策略来进行处理. 一.示例展示: 以下例子主要通过对手机和 ...

  8. 发送请求时params和data的区别

    在使用axios时,注意到配置选项中包含params和data两者,以为他们是相同的,实则不然. 因为params是添加到url的请求字符串中的,用于get请求. 而data是添加到请求体(body) ...

  9. Apache Beam中的函数式编程理念

    不多说,直接上干货! Apache Beam中的函数式编程理念 Apache Beam的编程范式借鉴了函数式编程的概念,从工程和实现角度向命令式妥协. 编程的领域里有三大流派:函数式.命令式.逻辑式. ...

  10. TOJ 2452 Ultra-QuickSort

    描述 In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a se ...