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. ABP项目后台初始化数据库

    设置host为启动项,并修改连接字符串 在程序包管理控制台中,默认项目选中EFCore 执行Update-Database命令

  2. CF248E Piglet's Birthday(概率dp)

    题面 luogu CodeForces 题解 \(orz\) yyb 转移蜜罐其实是吓唬人的...... 转移的蜜罐都是尝试过的,所有只关心当前架子上的蜜罐数 设\(f[i][j]\)表示第i个货架有 ...

  3. 链表 206 Reverse Linked List, 92,86, 328, 2, 445

    表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...

  4. 将M个客服随机分配给N个客户

    class AllocUser { //客户多于客服 public static void Test() { var customers = new List<Customer>() { ...

  5. HTML嵌套php

    1.  <?php echo 'if you want to serve XHTML or XML documents, do it like this'; ?> 2.  <scri ...

  6. eclipse+maven远程(自动)部署web项目到tomcat

    [转自] http://blog.csdn.net/dhmpgt/article/details/11197995 eclipse集成maven后可以用maven命令把web项目自动部署到tomcat ...

  7. Oracle下lag和lead分析函数

    [转自] http://blog.csdn.net/thinkscape/article/details/8290894 Lead和Lag分析函数可以在同一次查询中取出同一字段的前N行的数据(Lag) ...

  8. vim(三)golang代码跳转配

    在golang的代码里跳来跳去.... godef 安装 跳转是通过godef实现,godef的安装目录一般是$GOBIN,只要让godef命令在$PATH下即可 godef 命令安装: go get ...

  9. linux下目录、文件显示颜色的设置生效

    Centos系统 拷贝/etc/DIR_COLORS文件为当前主目录的 .dir_colors 命令:cp /etc/DIR_COLORS ~/.dir_colors 修改~/.dir_colors中 ...

  10. PIE SDK微分锐化

    1.算法功能简介 微分锐化通过微分使图像的边缘或轮廓突出.清晰.导数算子具有突出灰度变化的作用,对图像运用导数算子,灰度变化较大的点处算得的值较高,因此我们将图像的导数算子运算值作为相应的边界强度,所 ...