LeetCode 118. 杨辉三角
118. 杨辉三角
给定一个非负整数numRows,生成杨辉三角的前numRows行。

在杨辉三角中,每个数是它左上方和右上方的数的和。
示例
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
Java 实现
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
for(int i=1;i<=numRows;i++){
List<Integer> list = new ArrayList<Integer>();
for(int j=1;j<=i;j++){
if (j==1||j==i) { // 当列为 1,或者 行==列 的时候都为 1
list.add(1);
}else{
list.add(result.get(i-2).get(j-1)+result.get(i-2).get(j-2));
}
}
result.add(list);
}
return result;
}
}
测试代码
import java.util.ArrayList;
import java.util.List;
public class Solution {
public List<List> generate(int numRows) {
List<List> result = new ArrayList<List>();
for(int i=1;i<=numRows;i++){
List list = new ArrayList();
for(int j=1;j<=i;j++){
if (j1||ji) { // 当列为 1,或者 行==列 的时候都为 1
list.add(1);
}else{
list.add(result.get(i-2).get(j-1)+result.get(i-2).get(j-2));
}
}
result.add(list);
// 测试代码
System.out.println(list);
}
return result;
}
// 测试主程序
public static void main(String[] args) {
Solution solution = new Solution();
solution.generate(5);
}
}
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
</div>
**参考资料**
* [https://leetcode.com/problems/pascals-triangle/discuss/38141/My-concise-solution-in-Java](https://leetcode.com/problems/pascals-triangle/discuss/38141/My-concise-solution-in-Java)
* [https://leetcode-cn.com/problems/pascals-triangle/description/](https://leetcode-cn.com/problems/pascals-triangle/description/)
* [https://leetcode.com/problems/pascals-triangle/discuss/?orderBy=most_votes](https://leetcode.com/problems/pascals-triangle/discuss/?orderBy=most_votes)
LeetCode 118. 杨辉三角的更多相关文章
- Java实现 LeetCode 118 杨辉三角
118. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], ...
- leetcode 118. 杨辉三角(python)
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...
- LeetCode:杨辉三角【118】
LeetCode:杨辉三角[118] 题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: ...
- LeetCode(119. 杨辉三角 II)
问题描述 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: 你可以优化你的 ...
- C语言118. 杨辉三角
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5输出:[ [1], [1,1], [1,2,1], [1, ...
- Java实现 LeetCode 119 杨辉三角 II
119. 杨辉三角 II 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...
- Leecode刷题之旅-C语言/python-118杨辉三角
/* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
随机推荐
- python学习(十一)测试和调试
最近学习了python的错误处理和几种测试方法 1 try except 可以通过try except方式捕捉异常 try: print('try...') r = 10/0 print('resul ...
- python学习笔记(六) 函数式编程
一 函数对象 函数同样可以作为对象复制给一个变量,如下: f = abs; print(f(-10)) f = 'abs'; print(f) def add(a,b,f): return f(a) ...
- 微信小程序,页面分享
onShareAppMessage: function () { return { title: '微信小程序联盟', desc: '最具人气的小程序开发联盟!', path: '/page/user ...
- [实战篇]Tomcat发布项目-虚拟目录
在二阶段学习的过程中,我一直使用MyEclipse的方式把工作空间的项目发布到webapps目录下,这种方式自我感觉在实际开发中应用能在70%左右,但是如何涉及到一些上传操作等操作, 从新发布项目之后 ...
- 使用git上传项目到GitHub上
之前的博客有<使用git拉取GitHub上的项目>的文章,那么现在说一下,如何上传项目到GitHub上. 1. Git的.gitignore 文档配置 因为项目中可能有很多的图片还有nod ...
- NOIP模拟赛16
NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第一轮Day2 期望得分:100+100+ =200+ 实际得分:100+40+70=210 T1天天寄快递 直接模拟,代码丢了...... T2天天和不 ...
- 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 ...
- node、npm及node_modules中依赖的版本更新
好久没用node了,想重新拾起来发现node还有相关模块的版本都太低了,使用npm install全是报版本低的警告. 这里记录一下,版本管理和node_modules更新的方法. 我用的是Windo ...
- 牛客网习题剑指offer之数值的整数次方
分析: 要考虑到exponent为0和负数的情况. 如果base是0并且exponent是负数的时候呢?那就发生除0的情况了. AC代码: public class Solution { public ...
- vc6列表框多选时,获取哪些项被选中
//vc6列表框多选时,获取哪些项被选中...... void CWebcyzDlg::OnButton2() { int n = m_mylist1.GetSelCount();//首先获取一共有多 ...