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, ...
随机推荐
- Java与.NET DES加密解密互转
上代码: Java代码: import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKe ...
- windows下安装openssh服务并实现远程登录
需要准备的工具: winscp 点击下载 openssh 点击下载 步骤: 在远程计算机安装 1.首先安装openssh,双击并安装 2.指定用户的home directory为C:\ ...
- InterruptedException 线程异常
InterruptedException 这个异常一般发生在线程中,当一个正在执行的线程被中断时就会出现这个异常-! 简单的说就是:假如有两个线程,第一个线程正在运行,第二个没有运行,这时第二个线程启 ...
- centos7的网络配置以及设置主机名和绑定IP的问题
CentOS 7.0系统是一个很新的版本哦,很多朋友都不知道CentOS 7.0系统是怎么去安装配置的哦,因为centos7.0与以前版本是有很大的改进哦. 说明:截止目前CentOS 7.x最新版本 ...
- AutoReleasePool 和 ARC 以及Garbage Collection
AutoReleasePool autoreleasepool并不是总是被auto 创建,然后自动维护应用创建的对象. 自动创建的情况如下: 1. 使用NSThread的detachNewThread ...
- JS获取客户端Mac和IP
JS获取硬件信息是通过ActiveX进行获取的,因此只能IE浏览器支持,火狐不支持 而且必须降低浏览器安全级别,因此不到万不得以一般不会采用这种方式 <html> <head> ...
- Ajax的post方法,模拟 从后台读取数据小demo
$(document).ready(function() { //定义一个函数 function timer() { $.post("1.json", function(data, ...
- 2016 - 1 -19 初学HTML5 第一天
1.HTML COMMANDS MHTL commands called elements.Usually, an element has a start tag and an end tag e.g ...
- Windows下adb push 总是提示Failed to copy "XX.apk" to 'system/app':Read-only file system
一般情况看到这种提示我们会想到需要root权限,然后敲上adb remount,但是当我们执行过adb remount后,提示成功,但执行push命令依旧无法完成push. 那么此时我们的做法应该是重 ...
- 13年省赛-B题-连通分量
题意:求从1到N是否存在一条路,可以遍历每个节点. 思路:求任意两点之间是否通畅即可: 疑惑:完全暴力,bfs但是TLE,问题在于求连通分量(PS:不会)贴别人代码,先保存着. #include &l ...