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的更多相关文章

  1. [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, ...

  2. LeetCode 120. Triangle (三角形)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. [LeetCode] Valid Triangle Number 合法的三角形个数

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  4. [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 ...

  5. LeetCode Valid Triangle Number

    原题链接在这里:https://leetcode.com/problems/valid-triangle-number/description/ 题目: Given an array consists ...

  6. [Leetcode Week8]Triangle

    Triangle 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/triangle/description/ Description Given a t ...

  7. 【leetcode】Triangle (#120)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. LeetCode - 120. Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  10. 【leetcode】triangle(easy)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

随机推荐

  1. 什么是布局?Android中的布局是怎样的?

    布局管理器(通常被称为是布局)是对ViewGroup类的扩展,是用来控制子控件在UI中的位置. Android SDK包含了许多布局类,在为视图.Fragment和Activity创建UI时,可以使用 ...

  2. input框中修改placeholder的样式

    有时间input标签的placeholder属性会出现问题,下面是修改placeholder的样式demo input::-webkit-input-placeholder{ color:red; f ...

  3. h5软键盘挡住输入框问题解决(android)

    问题 移动端浏览器中的表单在部分android机型上测试,点击靠下的输入框时会遇到弹出的软键盘挡住输入框问题 ios可自身弹起(ios自身的调整偶尔也会出问题,例如第三方键盘会遮挡,原因是第三方输入法 ...

  4. web 12

    调用一个地图(百度地图)API(定位) 到网站: 1.调用API的js : <script type="text/javascript" src="https:// ...

  5. Java for Android 第二周课上实验一

    (一)命令行下程序开发 (二)IDEA下程序开发调试 Mac OS系统下使用的IDEA为 Netbeans (三)测试题我的学号后两位为10 使用简单的PHP小程序得我的题目为2:实现简单四则运算(能 ...

  6. kibana 创建index pattern 索引模式时过慢导致无法创建成功 以及解决方案

    下面我具体描述一下我遇到的问题. 在kibana上面创建索引点击创建时,一直显示下面的页面 就看到不停的在那转,始终创建不成功. 查看后台日志,看到状态码为403,报了如下的错误 由于我用的是es6版 ...

  7. cadence焊盘及元件封装制作

    前面学习了元件封装的制作,由于琐碎事情的耽误,加上学习python,没有及时的总结这部分内容,现在做一个补充!

  8. Conda命令指标

    一.Conda相关指令 # 查看当前环境下已安装的包 conda list # 查看某个指定环境的已安装包 conda list -n tensorflow # 查找package信息 conda s ...

  9. Hadoop源码分析:Hadoop编程思想

    60页的ppt讲述Hadoop的编程思想 下载地址 http://download.csdn.net/detail/popsuper1982/9544904

  10. 十几分钟让你学会MySQL布尔和延迟盲注手工操作

    作者:Max老白Gān丶链接:http://www.lofter.com/lpost/1fefbc76_12d25dc31来源:LOFTER 注入常用到的几个函数   1 mid(str,1,3) 字 ...