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, ...
随机推荐
- First Steps: Command-line
This brief tutorial will teach how to get up and running with the Flyway Command-line tool. It will ...
- (转)干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码)
干货|这篇TensorFlow实例教程文章告诉你GANs为何引爆机器学习?(附源码) 该博客来源自:https://mp.weixin.qq.com/s?__biz=MzA4NzE1NzYyMw==& ...
- mybatis配置文件resultMap标签的使用
本文为博主原创,未经允许不得转载: resultMap标签是为了映射select查询出来结果的集合,其主要作用是将实体类中的字段与 数据库表中的字段进行关联映射. 注意:当实体类中的字段与数据库表中的 ...
- java web项目在linux部署、启动,查看系统配置常用的linux命令总结
本文为博主原创,未经允许不得转载: 以下为在工作中常用的linux命令进行了总结,主要在项目安装及启动,及编辑部署文件时应用较多1.gz文件是一种压缩文件. 以·tar.gz为扩展名的是一种压缩文件, ...
- Mysql视图、触发器、事务、储存过程、函数
一.视图 什么是视图 视图是有一张表或多张表的查询结果构成的一张虚拟表 为什么使用视图 当我们在使用多表查询时 我们的sql语句可能会非常的复杂,如果每次都编写一遍sql'的话无疑是一件麻烦的事情,这 ...
- Codeforces 781D Axel and Marston in Bitland
题目链接:http://codeforces.com/contest/781/problem/D ${F[i][j][k][0,1]}$表示是否存在从${i-->j}$的路径走了${2^{k}} ...
- module.exports小程序模块化,require
小程序模块化 可以将一些公共的代码抽离成为一个单独的 js 文件,作为一个模块.模块只有通过 module.exports 或者 exports 才能对外暴露接口. tips:exports 是 mo ...
- JCF
点我 补两个红黑树原理:https://www.cnblogs.com/yyxt/p/4983967.html https://www.cnblogs.com/skywang12345/p/324 ...
- 使用openpyxl的styles,实现写入值时加背景色
所用文件.数据和上一节代码中用的一致 本次直接贴代码 from openpyxl.styles import fills from openpyxl import load_workbook clas ...
- 基于虹软人证核验 2.0 Android SDK开发集成入门
一.功能介绍虹软人证核验 2.0 SDK(以下简称SDK)包含人脸检测.人脸跟踪.人证核验等能力,主要实现人证的1:1比对.其中暴露对外的功能方法有:active 引擎激活init 引擎初始化inpu ...