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,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的更多相关文章
- [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, ...
- 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 ...
- 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 ...
- 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, ...
- [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 ...
- 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 ...
- 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 ...
- 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: ...
- 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, ...
随机推荐
- ubuntu安装vim
1.安装 sudo apt-get install vim-gtk 2.安装完成之后,在命令行敲入vi,按“tab”键,可以看到,已经有vim命令的存在,安装成功. 3.配置 sudo vim /et ...
- UITextView添加一个placeholder功能
控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UITextView可以多行 ...
- 基于百度定位及天气获取的DEMO
demo基于百度定位APIv4.0版.新浪天气(不用查询城市代码). 需求: 1.button实现触发定位监听和天气捕获 2.两个textview 分别显示详细地址.天气. 界面很简陋,侧重功能实现. ...
- kali linux karmetasploit配置
原理分析:http://www.freebuf.com/articles/77055.html 转官方说明:https://www.offensive-security.com/metasploit- ...
- 用Ogre实现《天龙八部》场景中水面(TerrainLiquid)详解
本文主要讲的是<天龙八部>游戏中水面(TerrainLiquid)的具体实现,使用C++,Ogre1.6. 天龙的水面做的比较简单,虽然没有倒影,但动态纹理+深度图做出的效果还行,看着不是 ...
- jsp弹出Please check the location and try again!对话框
关闭它的jsp图形模式.myeclipse10中打开jsp文件时,右键open with 选MyEclipse JSP Editor,不选MyEclipse Visual JSP Editor模式.
- LeetCode----Word Ladder 2
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- iOS:Size Classes的使用
iOS 8在应用界面的可视化设计上添加了一个新的特性-Size Classes,对于任何设备来说,界面的宽度和高度都只分为两种描述:正常和紧凑.这样开发者便可以无视设备具体的尺寸,而是对这两类和它们的 ...
- Object-C 基础笔记1--杂识
一,常量 1, 常量字符串永远不会release; 2,使用常量字符串初始化另一个字符串,这两个字符串相等. 3,相同内容的常量地址值相同. 二, @class 声明一个类,一般是在.h文件中使用,不 ...
- Redis - hash类型操作
hash 类型操作设置操作:hset: hset key filed value 创建指定key的filed-value名值对 hsetnx: hsetnx key file ...