题目是:

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. CentOS下命令行和桌面模式的切换方法(转载)

    桌面模式和命令行模式的切换方法 用编辑器打开 /etc/inittab 文件(这里用的是vi,你可以选择你喜欢的): #vi /etc/inittab 打开效果图如下: 桌面模式    : 把光标所在 ...

  2. bzoj2395: [Balkan 2011]Timeismoney

    Description      有n个城市(编号从0..n-1),m条公路(双向的),从中选择n-1条边,使得任意的两个城市能够连通,一条边需要的c的费用和t的时间,定义一个方案的权值v=n-1条边 ...

  3. ios的Ping++支付接入步骤-b

    1. Client 发送支付要素给 Server 2. Server 发送支付请求并将返回的支付凭据传给 Client 3. Client 调起支付控件完成支付 4. 渠道同步返回支付结果给 Clie ...

  4. C++关于构造函数的深拷贝与浅拷贝

    首先拷贝构造函数的声明方法: 类名::类名(const 类名&) 1.浅拷贝的实现程序: #include "stdafx.h" #include <iostream ...

  5. 最新game

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h&g ...

  6. Unity3d开发IOS游戏 基础

    Unity3d开发IOS游戏 基础 @阿龙 -  649998群 1.先说明两个问题,我在WIN7下面的U3D里面,用了雅黑字体,但是导出为ios后,字体就看不见了,这是为什么呢?这是需要在MAC下找 ...

  7. SSH2中实例化不了Action的一个原因

    <!-- 流程管理 --> <action name="flow_*" class="workFlowAction" method=" ...

  8. 设置Git提交时不用输入用户名和密码

    在用git提交时代码至github上时每次都要输入用户名和密码,当提交操作较为频繁时非常不方便,可以按下文中的介绍,设置成提交时不用输入用户名和密码: 1.在当前库下,已经运行过 git remote ...

  9. Javascript之return

    做表单验证的时候,除了错误提示之外,还要做的一点就是避免表单提交. 如果避免表单提交呢? 有一个方法很简单,就是return 我们来看一下代码: $(".make_sure").c ...

  10. [LeetCode#82]Remove Duplicates from Sorted Array II

    Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...