题目是:

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. 【HDOJ】4704 Sum

    数学题.f(n) = 2^(n-1) mod (1e9+7). #include <cstdio> #define MAXN 100005 char buf[MAXN]; __int64 ...

  2. 基于Node.js的强大爬虫 能直接发布抓取的文章哦

    基于Node.js的强大爬虫 能直接发布抓取的文章哦 基于Node.js的强大爬虫能直接发布抓取的文章哦!本爬虫源码基于WTFPL协议,感兴趣的小伙伴们可以参考一下 一.环境配置 1)搞一台服务器,什 ...

  3. ZKW费用流修正

    #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #inc ...

  4. (转载)HTML标签<br><br/>的区别在哪里?

    (转载)http://zhidao.baidu.com/question/259205863.html HTML标签<br><br/>的区别在哪里? 如果一样为什么还要分2个标 ...

  5. 【动态规划】【归并】Vijos P1412 多人背包

    题目链接: https://vijos.org/p/1412 题目大意: 求01背包的前K优解,要求必须装满(1<=K<=50 0<=V<=5000 1<=N<=2 ...

  6. Linked List Cycle——LeetCode

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  7. iOS 沙盒路径操作:新建/删除文件和文件夹

    http://blog.csdn.net/totogo2010/article/details/7671144

  8. ArrayList 、Vector、 LinkList

    public class TestList {     public static void init(List list)     {         if(list!=null)          ...

  9. [Audio processing] 常见语音特征 —— LPC

    共振峰产生的原理及其在音质上的体现,共振峰的分布位置是建立在声音产生媒介的共鸣物理结构基础上的(Resonant Physical Structure).   无论是人声还是乐器,它们的声音特性都源自 ...

  10. js中正则表达式的使用

    1,作用:匹配一个字符串中的一些内容2,声明和使用: 1),构造函数 var reg=new RegExp(/表达式/) 2),字面量 var reg=/表达式/ 推荐使用 eg: var reg=/ ...