Pascal's Triangle Leetcode Java and C++
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1] ]
public class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> result = new ArrayList<>();if (numRows <= 0) {return result;}for (int i = 0; i < numRows; i++) {List<Integer> array = new ArrayList<>();array.add(1);if (i > 0) {List<Integer> pre = result.get(i - 1);int size = pre.size();for (int j = 0; j < size; j++) {if (j == size - 1) {array.add(pre.get(j));} else {array.add(pre.get(j) + pre.get(j + 1));}}}result.add(array);}return result;}
}
看了top solution后的改进版本,减少了条件判断:
public class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> result = new ArrayList<>();if (numRows <= 0) {return result;}for (int i = 0; i < numRows; i++) {List<Integer> array = new ArrayList<>();for (int j = 0; j < i + 1; j++) {if (j == 0 || j == i) {array.add(1);} else {List<Integer> pre = result.get(i - 1);array.add(pre.get(j - 1) + pre.get(j));}}result.add(array);}return result;}
}
但看了另一个top solution还是觉得可能自己就是写不好代码了。。。= =?
但也许我写的比较快一点吧?并不好判断。。。
public class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> result = new ArrayList<>();List<Integer> array = new ArrayList<>();if (numRows <= 0) {return result;}for (int i = 0; i < numRows; i++) {array.add(0, 1);for (int j = 1; j < array.size() - 1; j++) {array.set(j, array.get(j) + array.get(j + 1));}result.add(new ArrayList<>(array));}return result;}
}
附上c++的解法,和第一种解法的改进版是一样的:
class Solution {
public:vector<vector<int>> generate(int numRows) {vector<vector<int>> r(numRows);for (int i = 0; i < numRows; i++) {r[i].resize(i + 1);r[i][0] = 1, r[i][i] = 1;for (int j = 1; j < i; j++) {r[i][j] = r[i - 1][j - 1] + r[i - 1][j];}}return r;}
};
这么一看c++还真的挺简洁的。
转载于:https://www.cnblogs.com/aprilyang/p/6943158.html
Pascal's Triangle Leetcode Java and C++相关推荐
- Pascal's Triangle (LeetCode) 帕斯卡三角或者叫杨辉三角----动态规划和memoization
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- Pascal's Triangle 2(leetcode java)
问题描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- [LeetCode] 118. Pascal's Triangle Java
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- LeetCode - Easy - 119. Pascal‘s Triangle II
Topic Array Description https://leetcode.com/problems/pascals-triangle-ii/ Given an integer rowIndex ...
- LeetCode - Easy - 118. Pascal‘s Triangle
Topic Array Description https://leetcode.com/problems/pascals-triangle/ Given a non-negative integer ...
- LeetCode 118 Pascal's Triangle(帕斯卡三角形)(vector)
翻译 给定一个行数字,生成它的帕斯卡三角形.例如,给定numRows = 5, 返回: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1] ] 原文 Given numR ...
- Leetcode 118:Pascal's Triangle 杨辉三角
118:Pascal's Triangle 杨辉三角 Given a non-negative integer numRows, generate the first numRows of Pasca ...
- [LeetCode]119.Pascal's Triangle II
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/43562603 题目 Given an ind ...
- leetcode python3 简单题119. Pascal's Triangle II
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十九题 (1)题目 英文: Given a non-negative ind ...
最新文章
- 干货丨2017年AI与深度学习要点大全
- ubuntu下python+tornado+supervisor+nginx部署
- JavaScript 编程精解 中文第三版 十一、异步编程
- 机器学习 | 梯度下降原理及Python实现
- java性能分析工具_java性能分析工具
- 13.函数式编程:匿名函数、高阶函数、装饰器
- 中国科学院计算机网络信息中心领导,中国科学院计算机网络信息中心
- python命名时可以使用中文吗_Python命名约定
- An internal error occurred during: Launching web on MyEclipse Tomcat
- #define和inline 的区别
- C1WebChart 图形化处理。
- 刘知远:在深度学习时代用HowNet搞事情
- Qt Qlineedit右击自带的菜单默认英文转中文
- NLP | 自然语言处理经典seq2seq网络BERT详解及代码
- python中matplotlib绘图中文显示问题
- H5公众号-canvas海报分享图+生成二维码
- 苹果电池ti测试软件,iPhone真实电池寿命快速检测,比苹果官方测的还准!
- C:exit(0),_exit(0),exit(1),exit(-1)作用与区别
- 简单认识向上转型和向下转型
- android系统日志如何查看,Android如何查看系统recovery日志,从而找到系统程序、刷机异常…...
热门文章
- openstack+essex+quantum成功show
- C++ Primer笔记 容器和算法(2)
- Android Studio打包APK时出现 is not translated in en (English) [MissingTranslation]
- vmware智能资源调整
- Android 返回桌面
- listview刷新,延迟加载,用单行刷新取代notifyDataSetChanged
- android AtomicBoolean类的使用
- android 密码加密
- Android开发常用轮子
- 解决 Successfully created project '' on GitHub, but initial push failed: Could not read from remote re