题目是:

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的更多相关文章

  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

    Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...

  5. 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, ...

  6. 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 ...

  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. WPF会重写Windows GUI的历史吗?

    原文地址:http://tech.it168.com/zx/2007-09-15/200709141320653.shtml 你可能对微软的.NET框架3.0版本的最近的一次更新感到有点奇怪.主版本指 ...

  2. sql restore mode

    refer : https://msdn.microsoft.com/en-us/library/ms189272.aspx SELECT name, recovery_model_desc FROM ...

  3. 再来,LVS+KEEPALIVED

    记得常规组合哟. 一般同时实现HA+LB. 如果只需要实现一个,那还不如UCARP?双机绑定一个IP作热备. CENTOS6:PACEMAKER+COROSYNC+HAPROXY. OTHER:HEA ...

  4. IAR FOR ARM 各版本,需要的大家可以收藏了

    原创,原帖地址是在阿莫论坛:http://www.amobbs.com/thread-5400051-1-1.html,这里也在博客贴上来供大家参考. 用过Keil和IAR,个人感觉是IAR还是很不错 ...

  5. 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩

    使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...

  6. 【JS】壹零零壹

    function f1() { } var f2 = function() { } var O = {} O.f1 = f1 O.f2 = f2 console.log(O)

  7. 从JavaScript的移位运算看数字在计算机内部的编码——补码

    偶然看到一个JavaScript的题目: js中13>>2=?      -13>>2=? 在浏览器中很容易测试出答案分别是 3 和 -4. 13>>2 = 3 很 ...

  8. DIV+CSS布局问题:一个宽度不确定的DIV里面放三个水平对齐的DIV,左右两个DIV宽度固定为150px,中间那个DIV充满剩余的宽度

    一个入门的DIV+CSS布局问题:一个宽度不确定的DIV里面放三个水平对齐的DIV,左右两个DIV宽度固定为150px,中间那个DIV充满剩余的宽度. 说明:代码非真实情况下使用,所以直接简单. 没耐 ...

  9. 高效算法——J 中途相遇法,求和

    ---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

  10. Java 中 StringBuilder 在高性能用法总结

    关于StringBuilder,一般同学只简单记住了,字符串拼接要用StringBuilder,不要用+,也不要用StringBuffer,然后性能就是最好的了,真的吗吗吗吗? 还有些同学,还听过三句 ...