Pascal's Triangle 2(leetcode 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?
问题分析:
第 rowIndex 行有 rowIndex + 1 个元素,给这 rowIndex + 1个元素赋初值 0,
row的第一个元素赋值为1,此时,row的内容为[1,0,0,0,......]
由第 j 行生成第j + 1行,初始为[1,0,0,0,......],逆序将相邻两个元素相加后,赋值给后者
j == 0 时,生成的是 [1,1,0,0,......]
j == 1 时,生成的是 [1,2,1,0,......]
......
j == rowIndex - 1,生成最终结果
代码:
public List<Integer> getRow(int rowIndex) {
List<Integer> row = new ArrayList<Integer>();
if(rowIndex < 0) return row;
// 第 rowIndex 行有 rowIndex + 1 个元素,给这 rowIndex + 1个元素赋初值 0
for(int i = 0; i <= rowIndex; ++i){
row.add(0);
}
row.set(0, 1);
//生成第rowIndex行数据,最小为第0行
//由第 j 行逆序生成第j + 1行,初始为 1
//j == 0 时,生成的是 1 1
//j == 1 时,生成的是 1 2 1
//......
//j == rowIndex - 1,生成最终结果
for(int j = 0; j < rowIndex; ++j){
for(int k = rowIndex - 1; k > 0; --k){ //从最后一个元素开始,逆序计算
row.set(k, row.get(k) + row.get(k - 1));
}
}
row.set(rowIndex, 1); //给最后一位赋值1
return row;
}
Pascal's Triangle 2(leetcode java)的更多相关文章
- Pascal's Triangle II Leetcode java
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- Pascal's Triangle II —LeetCode
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Pascal's Triangle II leetcode
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- LeetCode算法题-Pascal's Triangle II(Java实现)
这是悦乐书的第171次更新,第173篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第30题(顺位题号是119).给定非负索引k,其中k≤33,返回Pascal三角形的第k ...
- 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 for LeetCode 118 Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 【LeetCode】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...
- [Leetcode][JAVA] Pascal's Triangle I, II
Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...
- 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, ...
随机推荐
- 深入了解JVW
Java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时 ...
- CodeForces 509C Sums of Digits(贪心乱搞)题解
题意:a是严格递增数列,bi是ai每一位的和,告诉你b1~bn,问你怎样搞才能让an最小 思路:让ai刚好大于ai-1弄出来的an最小.所以直接模拟贪心,如果当前位和前一个数的当前位一样并且后面还能生 ...
- Linux下使用wget下载FTP服务器文件
wget -nH -m --ftp-user=your_username --ftp-password=your_password ftp://your_ftp_host/* 使用命令下载ftp上的文 ...
- Python实现机器学习算法:EM算法
''' 数据集:伪造数据集(两个高斯分布混合) 数据集长度:1000 ------------------------------ 运行结果: ---------------------------- ...
- 51nod1057-N的阶乘(大数乘法巧解)
这道大数乘法开始我是想套板子模拟的..然后就发现2/3的例子都wa了.(惊了).然后在思考后发现n2的板子的确过不了这么多的大数.(不看题的下场).所以,我在网上发现了分块求大数的方法.%%% 思路来 ...
- Ubuntu 14.04 安装 boost 1_57_0
参考: How to build boost 1_57_0 Ubuntu platform Ubuntu 14.04 安装 boost 1_57_0 $ sudo mkdir /opt/downloa ...
- Lintcode27-Reverse 3-digit Integer
Reverse a 3-digit integer. Example Example 1: Input: number = 123 Output: 321 Example 2: Input: numb ...
- node.js中的http.response.end方法使用说明
转载自:http://m.jb51.net/article/58468.htm 本文介绍了http.response.end的方法说明.语法.接收参数.使用实例和实现源码,需要的朋友可以参考下 方法说 ...
- 关于Test类中不能使用Autowired注入bean的问题
在测试类中使用AutoWired注解一直不能获取到Bean,调用方法时一直报空指针异常,我有在其他类中使用AutoWired试了下,发现能够生效.问题应该就是处在Test类中,后面找了半天终于找到问题 ...
- PCA-主成分分析(Principal components analysis)
来自:刘建平 主成分分析(Principal components analysis,以下简称PCA)是最重要的降维方法之一. 1. PCA的思想 PCA顾名思义,就是找出数据里最主要的方面,用数据里 ...