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?

上一道题的延伸版,就是直接求出第k行的数,要求用o(k)的空间复杂度。

也是直接相加就可以了。

public class Solution {
public List<Integer> getRow(int rowIndex) { List ans = new ArrayList<Integer>(); if( rowIndex == 0){
ans.add(1);
return ans;
}
else if( rowIndex == 1){
ans.add(1);
ans.add(1);
return ans;
} int[] result = new int[rowIndex+1]; result[0] = 1;
result[1] = 1;
for( int i = 2;i<rowIndex+1;i++){
int a = result[0];
int b = result[1];
result[i] = 1;
for( int j = 1;j<i;j++){
result[j] = a+b;
a = b;
b = result[j+1]; }
}
for( int i = 0 ;i<rowIndex+1;i++)
ans.add(result[i]); return ans; }
}

leetcode 119 Pascal's Triangle II ----- java的更多相关文章

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

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

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

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

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

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

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

随机推荐

  1. 神奇的NOIP模拟赛 T3 LGTB 玩THD

    LGTB 玩THD LGTB 最近在玩一个类似DOTA 的游戏名叫THD有一天他在守一座塔,对面的N 个小兵排成一列从近到远站在塔前面每个小兵有一定的血量hi,杀死后有一定的金钱gi每一秒,他都可以攻 ...

  2. FR报表 自动缩小的代码

    procedure TfrMemoView.Draw(Canvas: TCanvas); var newdx: Integer; OldScaleX, OldScaleY: Double; fs: i ...

  3. iOS如何生成.a文件

    首先来谈谈为何要使用.a文件 Objective-c语言有.h .m 文件组成.静态库可以将 .m文件封装成一个.a文件,第三方应用程序只需要拿到这个.a文件和代码对应的.h文件即可使用静态库中封装的 ...

  4. JAVA每日一旅3

    1.关于byte byte在内存中占一个字节,范围是-128-127,128作强制类型转换到byte变成-128,因为128的二进制表示:1000 0000,最高位是符号位. 2.关于Hibernat ...

  5. sql游标的使用入门

    游标的理解: 游标其实可以理解成一个定义在特定数据集上的指针,我们可以控制这个指针遍历数据集,或者仅仅是指向特定的行,所以游标是定义在以Select开始的数据集上的 普通的sql语句是面向集合的,游标 ...

  6. matlab调用opencv函数的配置

    环境: VS2010 活动解决方案平台x64 WIN 8.1 Opencv 2.4.3 Matlab 2012a 1.  首先保证vs2010能正确调用opencv函数, 2.  Matlab中选择编 ...

  7. Installing Cygwin and Starting the SSH Daemon

    This chapter explains how to install Cygwin and start the SSH daemon on Microsoft Windows hosts. Thi ...

  8. 《day10》

    //65-面向对象-接口-接口的思想 /* 举例:笔记本电脑. 1,接口的出现对功能实现了扩展. 2,接口的出现定义了规则. 3,接口的出现降低了耦合性.(解耦) 接口的出现完成了解耦,说明有两方,一 ...

  9. mycat启动后,用Navicat Premium 连接报 "2013"

    最近在学习mycat,启动后,用Navicat Premium 连接报 "2013"  Lost Connection During Query ,经过一顿百度也没发现是怎么回事, ...

  10. hdoj-2031

    #include "stdio.h"#include "stdlib.h"int main(){ char a[]={'0','1','2','3','4',' ...