[Leetcode] 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,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
因为需要O(k)的空间,因此需要对同一个数组进行重写,可以从后往前进行覆盖,则没有问题
需要注意的一个问题是 Arrays.asList()操作的不能使基本类型数组
1 public class Solution { 2 public List<Integer> getRow(int rowIndex) { 3 Integer [] row = new Integer[rowIndex+1]; 4 row[0]=1; 5 if(rowIndex==0)return Arrays.asList(row); 6 row[1]=1; 7 if(rowIndex==1) return Arrays.asList(row); 8 for(int i = 2; i<=rowIndex;i++){ 9 row[i]=1; 10 for(int j=i-1;j>=1;j--){ 11 row[j]=row[j]+row[j-1]; 12 } 13 } 14 return Arrays.asList(row); 15 } 16 }
转载于:https://www.cnblogs.com/deepblueme/p/4736091.html
[Leetcode] Pascal's Triangle II相关推荐
- LeetCode 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: 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
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/43562603 题目 Given an ind ...
- LeetCode 119. Pascal’s Triangle II
119. Pascal's Triangle II My Submissions QuestionEditorial Solution Total Accepted: 72147 Total Subm ...
- LeetCode - Easy - 119. Pascal‘s Triangle II
Topic Array Description https://leetcode.com/problems/pascals-triangle-ii/ Given an integer rowIndex ...
- leetcode python3 简单题119. Pascal's Triangle II
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十九题 (1)题目 英文: Given a non-negative ind ...
- C#LeetCode刷题之#119-杨辉三角 II(Pascal‘s Triangle II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3690 访问. 给定一个非负索引 k,其中 k ≤ 33,返回杨辉 ...
- LeetCode Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 119. Pascal's Triangle II (Graph; WFS)
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- [Array]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 ...
最新文章
- 51单片机怎么学啊?有推荐的网课和书籍么?
- excel如何分类汇总_Excel教程:一个排序后分类汇总他弄了一天 Excel神技能!
- 解决ubuntu打开windows中txt文件出现乱码现象
- 使用NFS 实现两台服务器间的数据备份. -- NFS 设置部分.
- 【9018:1956】线段树1
- offsetTop、offsetLeft、offsetWidth、offsetHeight、style中的样式
- SUSE11 搭建iscsi target 配置
- 看过这五条,再离职!
- 1.9 编程基础之顺序查找 01 查找特定的值 python AC
- linux下编程输入密码不回显
- linux su,sudo命令
- java皮卡丘代码_Picachu代码源代码,皮卡丘,源码
- noip2013 day1
- Git廖雪峰 常用命令总结
- [笔记分享] [Camera] 相机的flash led功能小结
- 飞鸽传书2007绿色版还需要遵循些基本的原则
- html编辑器的回车换行问题解决方案
- R语言基础-Rstudio基本使用
- VUE—下载安装并使用mint ui(图文详情)
- java+ElementUI前后端分离旅游项目第七天 权限管理和图形报表