leetcode — pascals-triangle
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/pascals-triangle/
*
*
* 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 PascalTiangle {
/**
* 生成杨辉三角(帕斯卡三角形)
*
* @param n
* @return
*/
public List<List<Integer>> generate (int n) {
List<List<Integer>> result = new ArrayList<List<Integer>>(n);
if (n == 0) {
return result;
}
result.add(Arrays.asList(new Integer[]{1}));
for (int i = 2; i <= n; i++) {
List<Integer> list = new ArrayList<Integer>(i);
list.add(1);
for (int j = 1; j < i-1; j++) {
list.add(result.get(i-2).get(j-1) + result.get(i-2).get(j));
}
list.add(1);
result.add(list);
}
return result;
}
public void print (List<List<Integer>> list) {
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j <= list.size() - i; j++) {
System.out.print(" ");
}
System.out.println(Arrays.toString(list.get(i).toArray(new Integer[list.get(i).size()])));
}
}
public static void main(String[] args) {
PascalTiangle pascalTiangle = new PascalTiangle();
pascalTiangle.print(pascalTiangle.generate(5));
}
}
leetcode — pascals-triangle的更多相关文章
- [Leetcode] pascals triangle ii 帕斯卡三角
Given an index k, return the k th row of the Pascal's triangle. For example, given k = 3,Return[1,3, ...
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- [LeetCode] Largest Triangle Area 最大的三角区域
You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...
- LeetCode Valid Triangle Number
原题链接在这里:https://leetcode.com/problems/valid-triangle-number/description/ 题目: Given an array consists ...
- [Leetcode Week8]Triangle
Triangle 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/triangle/description/ Description Given a t ...
- 【leetcode】Triangle (#120)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode][Java]Triangle@LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【leetcode】triangle(easy)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- 使用ftp读取文件夹中的多个文件,并删除
public class FTPUtils { private static final Logger LOG = LoggerFactory.getLogger(FTPUtils.class); / ...
- SDN网络虚拟化中有效协调的映射算法
来自论文An efficient and coordinated mapping algorithm in virtualized SDN networks,来自期刊<信息与电子工程前沿> ...
- go 可变参数
go 在参数列表中使用 ... 表示可变参数,例如: import "fmt" func Minimum(first int, others ...int) int { min : ...
- MFC实现红黑砖块
MFC实现红黑砖块 题目 老题目了,给定w,h长宽的图,上面有颜色不同的瓷砖,黑和红,问从给的起点出发,只能走黑色瓷砖,能走多少块,可视化输出过程 思路 咋一看搜索水题,但是要用可视化,要用模板类,, ...
- wpa_cli 关联无线网络
fq关联无线网络,不同的无线网络认证方式不同设置: 1)open(开放式认证方式,分为): wpa_cli -iwlan0 set_network 0 ssid '"w ...
- 转 Singleton clr via c#3
/// <summary> /// 注意 只有在构造器没有副作用的时候才能使用这个技术 /// </summary> class Singleton { private sta ...
- vue图片上传到七牛云
代码: <template> <div class="upload-info"> <div> <el-upload class=" ...
- Web版需求征集系统所得1,servlet中获取checkbox复选框的值
servlet中获取checkbox复选框的值 </tr> <tr> <td align="right">研究类型</td> < ...
- wordpress安装插件和主题
一.建立ftp服务器: 安装:sudo apt-get install vsftpd 配置:sudo nano /etc/vsftpd.conf 本地写入的注释去掉,可以写入的注释去掉 重启服务: s ...
- win 10 亮度调节不能使用了
我的解决办法的前提:装过teamviewer ,然后每次系统推送大升级似乎都会无法调节亮度,如果不是这个前提自己找别的办法吧 teamviewer 就是一个流氓软件. 每次更新之后都末名奇妙的不能调节 ...