Problem:

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?

Summary:

返回杨辉三角(帕斯卡三角)的第k行。

Solution:

1. 若以二维数组的形式表示杨辉三角,则可轻易推算出row[i][j] = row[i - 1][j - 1] + row[i - 1][j], 但在这道题中只要求返回一行,消耗空间是完全没有必要的。

我们可知若已知第i行的所有数值,则第i+1行的数值可以由row[j] = row[j - 1] + row[j]计算出,但若正向计算,则计算row[j+1]时所需的row[j]已被覆盖掉,故计算新的一行时从后往前计算即可。

 vector<int> getRow(int rowIndex) {
vector<int> row(rowIndex + );
for (int i = ; i <= rowIndex; i++) {
row[] = row[i] = ;
for (int j = i - ; j > ; j--) {
row[j] = row[j - ] + row[j];
}
} return row;
}

2. 公式计算:r= ri1 ∗ (index − i + 1) / i

参考:http://blog.csdn.net/nomasp/article/details/50568802

 vector<int> getRow(int rowIndex) {
vector<int> row(rowIndex + );
row[] = row[rowIndex] = ;
for (int i = ; i <= (rowIndex + ) / ; i++) {
row[i] = row[rowIndex - i] = (unsigned long)(row[i - ]) * (unsigned long)(rowIndex - i + ) / i;
} return row;
}

LeetCode 119 Pascal's Triangle II的更多相关文章

  1. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

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

  2. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

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

  3. LeetCode 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, ...

  4. leetcode 119 Pascal's Triangle II ----- java

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

  5. Java [Leetcode 119]Pascal's Triangle II

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

  6. C#解leetcode:119. Pascal's Triangle II

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

  7. Java for LeetCode 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 ...

  8. Leetcode 119 Pascal's Triangle II 数论递推

    杨辉三角,这次要输出第rowIndex行 用滚动数组t进行递推 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; class Solution { public: ...

  9. 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. AppBox升级进行时 - Any与All的用法(Entity Framework)

    AppBox 是基于 FineUI 的通用权限管理框架,包括用户管理.职称管理.部门管理.角色管理.角色权限管理等模块. 属于某个角色的用户列表(Any的用法) 使用Subsonic,我们有两种方法获 ...

  2. 【Python】[面向对象高级编程] 多成继承,定制类,使用枚举

    1.多成继承 class SmallDog(Animal,Dog) pass MixIn就是一种常见的设计. 2.定制类类似__slots__这种形如 __xxx__ 的变量或者函数名,在python ...

  3. elipse 从eclipse导入maven项目

    1. 使用Eclipse通过Svn导入项目 2.cmd 在项目目录下执行 mvn eclipse:eclipse 3. 然后在项目上点击右键 configure ->convert to mav ...

  4. outlook 2016 for windows 每次刷新发送接收邮件会弹出登陆界面

    Q: outlook2016 for windows 每次刷新发送接收邮件会弹出登陆界面,office365 ProPlus 都是正常激活了,Word 和Excel都不存在此类问题 A: 排除用户的o ...

  5. 【kAriOJ】离散数学 构造群码 极大似然法解码

    A. 编程题1 构造群码 时间限制 1000 ms 内存限制 65536 KB 题目描述 针对给定H,计算群码编码函数eH,并计算给定字的码字. 输入格式 第一行输入两个整数m,n:(m < n ...

  6. 更新/替换系统 hosts,轻松访问国外站点

    更新 hosts 下面介绍的操作均可能覆盖现有 hosts ,进行操作前请先确认是否需要备份. 推荐使用本项目的 Host Tools 来自动化 备份/配置 工作. 若更新 hosts 未立即生效,请 ...

  7. AngularJS 的嵌套路由 UI-Router

    AngularJS 的嵌套路由 UI-Router 本篇文章翻译自:https://scotch.io/tutorials/angular-routing-using-ui-router 演示网站请查 ...

  8. <<< Tomcat 部署项目There are no resources that can be added or removed from the server

    错误信息:没有资源可以添加或删除的服务器 解决方式: 方式1.选中项目右键——找到Project Facets——勾选Dynamic Web Project和java 方式2.新建一个同名web项目, ...

  9. Hibernate的关联映射关系

    一:多对一 <many-to-one 1.name:当前类的属性名(关联映射的类) 2.column:属性多对应的类的对应的表的外键(连接条件) 3.class:属性所对应的类的权限定名 4.n ...

  10. 探索Aspnetcore+mysql+efcore

    摘要 之前尝试了,新建asp.net core站点,那么如何和mysql建立连接,如果操作mysql?本篇将尝试使用EntityFrameworkCore进行mysql的操作. 一个例子 首先新建一个 ...