LeetCode——Pascal's Triangle II
Description:
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3, Return [1,3,3,1].
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
for(int i=0; i<=rowIndex; i++) {
List<Integer> tList = new ArrayList<Integer>();
if(i == 0) {
tList.add(1);
}
else {
for(int j=0; j<=i; j++) {
if(j == 0 || j == i) {
tList.add(1);
}
else {
tList.add(list.get(i-1).get(j) + list.get(i-1).get(j-1));
}
}
}
list.add(tList);
}
return list.get(rowIndex);
}
}
LeetCode——Pascal's Triangle II的更多相关文章
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...
- LeetCode - Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- LeetCode Pascal's Triangle II (杨辉三角)
题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 0ms的版本: class Solution { p ...
- leetcode:Pascal's Triangle II【Python版】
1.将tri初始化为[1],当rowIndex=0时,return的结果是:1,而题目要求应该是:[1],故将tri初始化为[[1]],返回结果设置为tri[0]即可满足要求: 2.最开始第二层循环是 ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
随机推荐
- MySQL死锁原因分析
行级锁有三种模式: innodb 行级锁 record-level lock大致有三种:record lock, gap lock and Next-KeyLocks. record lock 锁住 ...
- Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/instal ...
- Linux下DedeCMS详细安全设置
经常会听到使用dedecms的站长抱怨,网站又被挂马了,dedecms真的很不安全.dedecms可能存在某些漏洞这不假,但主要责任真的是dedecms吗?我们知道,一个黑客想上传木马,首先得可以找到 ...
- C++中static_cast, dynamic_cast使用方法
前言 Android的Framework层源代码中有非常多强制类型转换函数的调用.写惯了C代码的人一般都习惯以下这样的强制转换方式: double a = 89; int b = (int)a; 可是 ...
- e581. Animating an Array of Images in an Application
This is the simplest application to animate an array of images. import java.awt.*; import javax.swin ...
- e649. 处理焦点改变事件
component.addFocusListener(new MyFocusListener()); public class MyFocusListener extends FocusAdapter ...
- 转载:【原译】Erlang性能的八个误区(Efficiency Guide)
转自:http://www.cnblogs.com/futuredo/archive/2012/10/16/2725770.html The Eight Myths of Erlang Perform ...
- CentOS查看操作系统信息(重要)
1.查看物理CPU的个数 [root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc ...
- Maven-使用及常见问题
一.Maven是什么? 管理jar包用的,以前jar包是自己下载,然后放在lib下,然后add build Path,Maven环境下,只需要添加坐标(实际是xml片段)便会根据坐标自行下载. 二.M ...
- 怎么让Word编辑公式又快又好
现在很多办公学习都是在电脑中进行的.很多文件论文都是在Word中编写定稿以后再打印成册或者去投稿.毫无疑问,在Word中编辑各种各样的文字与符号是一项现在社会中非常必要的技能,而这其中一项就是对公式的 ...