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 [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
(注意:这里要求空间为O(k))
一个满足条件的答案如下:
public class Solution {
public IList<int> GetRow(int rowIndex) {
List<int> res = new List<int>();
for(int i=;i<rowIndex+;i++)
res.Add(); //赋初值
for(int i=;i<rowIndex;i++)
{
for(int j=i;j>;j--)
res[j] += res[j-] ;
}
return res;
}
}
总结:
1 首先生成一个list列表,并且将所有的值都赋初值,初值为1
2 每执行一次外部的for循环,则更新一行的信息。例如,当执行完i=1这一次循环后,将list列表更新为 原来帕斯卡三角形的 第三行的数据。当执行完i=2这一次循环后,将list列表更新为 原来帕斯卡三角形的 第四行的数据。
3 内部的for循环的初值必须为j=i,也就是说不能从j=1开始。因为,每一行的元素的值都是由上一行的元素的值确定,其中第i行的res[j] 就是第i-1行的res[j] += res[j-1] 的结果,如果从j=1开始循环,则list列表的值从左到右开始更新,当第一个元素更新后,就不能用它产生第二个元素(因为第二个元素的产生和第一个元素的值有关系,现在第一个元素的值丢失了)。但如果从j=i开始循环,就是list列表的值从右到左开始更新,当倒数第一个值更新后,并不影响倒数第二个值的更新,因为倒数第二个值得更新仅仅和倒数第二个值本身以及倒数第三个值有关,我们倒数第一个值的更新并没有更改这两个元素,所以没有元素丢失,可以顺利更新完一行。
C#解leetcode:119. Pascal's Triangle II的更多相关文章
- [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, ...
- [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 ...
- 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, ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
- 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, ...
- 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 ...
- 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 ...
- 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: ...
- 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- ...
随机推荐
- 如何执行一个mysql的sql脚本文件
sql脚本是包含一到多个sql命令的sql语句,我们可以将这些sql脚本放在一个文本文件中(我们称之为“sql脚本文件”),然后通过相关的命令执行这个sql脚本文件.基本步骤如下:一.创建包含sql命 ...
- android 开源项目学习
1.Android团队提供的示例项目 如果不是从学习Android SDK中提供的那些样例代码开始,可能没有更好的方法来掌握在Android这个框架上开发.由Android的核心开发团队提供了15个优 ...
- java-web-j2e学习建议路线
JAVA学习之路(2) 首先要明白Java体系设计到得三个方面:J2SE,J2EE,J2ME(KJAVA).J2SE,Java 2 Platform Standard Edition,我们经常说 ...
- WPF感悟(1)
原文地址:http://liutiemeng.blog.51cto.com/120361/91632 1.UI层与逻辑层要尽可能地剥离(解耦). 2.Routed Event和Command比Even ...
- SCVMM更换数据库,如何搞?
因为SCVMM和SQL不是集成在同一台机器上的. 所以,当SQL换机器或是换名字后,SCVMM就不能启动了. 并且MS没提供直观的更改数据库连接的工具,只是在安装的时候有选项. 网上找了方法,修改注册 ...
- MySQL基本查询语句练习
努力很久只为获得别人尊重的眼光. ——我是,董宏宇,我为自己代言. 技术交流QQ:1358506549(请注明你的来意) use xsx; CREATE TABLE Course( Cno char( ...
- JavaScript 函数和对象
在javascirpt 世界中,所有的函数都是对象,并且还可以被用来创建对象. function make_person(firstname, lastname, age) { person = {} ...
- 更改Visual Studio 2010/2012/2008的主题设置
一.更改主题: 主题网站:http://studiostyl.es/ Visual Studio 2010发布也已经有一段时间了,不过安装后默认的白底的主题长时间看代码可能会感觉眼睛酸痛,况且时间长了 ...
- 【Express】请求和响应对象
浏览器发送的信息 app.get('/headers', function(req, res){ res.set('Content-Type', 'text/plain'); var s = ''; ...
- Partition List ——LeetCode
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...