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. Hibernate中的一级缓存、二级缓存和懒加载

    1.为什么使用缓存 hibernate使用缓存减少对数据库的访问次数,从而提升hibernate的执行效率.hibernate中有两种类型的缓存:一级缓存和二级缓存. 2.一级缓存 Hibenate中 ...

  2. K2 BPM+Microsoft Dynamics CRM,妥妥的~

    啊~~~~七夕 ▼ 你比巴西少一xi 你比山西多四xi 对有情人来说今天就是情人节,对单身汪来说,今天就是个星期四. but,软件也是要秀恩爱的! ♥ 晒晒我家亲爱的CRM,它的全名叫Microsof ...

  3. Hibernate对象映射类型

    Hibernate understands both the Java and JDBC representations of application data. The ability to rea ...

  4. python3控制路由器--使用requests重启极路由.py

    代码写了相应的注释,以后再写成可以方便调用的模块. 用fiddler抓包可以看到很多HTTP头,经过尝试发现不是都必须的. 'Upgrade-Insecure-Requests':1,#必要项,值为1 ...

  5. JS 原型继承的几种方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Mysql 基本操作连接数据库读取信息内容

    <?php header("content-type:text/html; charset=utf-8"); // 数据库配置信息 define("DB_HOST& ...

  7. SAP 中如何寻找增强

    http://blog.csdn.net/edifierliu/article/details/5978824 查找SAP标准事务代码中使用的BADI: 在SE24中,查看类对象CL_EXITHAND ...

  8. IOS使用Asyncsocket进行socket编程

    iphone的标准推荐CFNetwork C库编程.但是编程比较烦躁.在其它OS往往用类来封装的对Socket函数的处理.比如MFC的CAsysncSocket.在iphone也有类似于开源项目.co ...

  9. java语法学习问题总结

    No.1:EnumTest No.2:Addition 在此程序中,学习了将文本框调用出来,文本框输入的数据都是String类型,所以用于计算时需要先进行转型,然后计算. No.3:TestDoubl ...

  10. win下隐藏任务栏

    C# 隐藏任务栏开始按钮 关闭shell 分类: .NET C# 2011-07-14 15:26 789人阅读 评论(1) 收藏 举报 shell任务c#stringnulluser 一.隐藏任务栏 ...