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. 如何删除github上的某个文件夹

    在github上只能删除仓库,却无法删除文件夹或文件, 所以只能通过命令来解决 首先进入你的master文件夹下, Git Bash Here ,打开命令窗口 $ git –help 帮助命令 $ g ...

  2. CentOS7终端的分辨率和字体修改

    本文转贴自https://blog.csdn.net/u010566813/article/details/40502819 一.修改分辨率 修改/boot/grub2/grub.cfg 添加如上,具 ...

  3. 刚学的vue.js的单一事件管理组件通信

    第一次在博客园写的技术分享,写的不好的话各位大神多体谅,好啦进入主题 说说思路 首先 第一步,准备一个空的示例对象 var Event=new Vue(); 第二步,准备发送的数据 Event.$em ...

  4. TimesTen数据库表中显示中文乱码的真正原因

    上一篇博客TimesTen中文乱码问题(其实是cmd.exe中文乱码)的内容可能不对,也许只是个巧合?不得而知了.因为我今天重装系统了,把win10换成了win7(64bit).又安装了timeste ...

  5. mac搭建简单的hls推流服务器遇到的问题(待更新)

    实际操作步骤: 输入brew install nginx-full --with-rtmp-module命令出现以下报错: 需要先安装nginx服务器,运行命令brew tap homebrew/ng ...

  6. 问题解决:Spyder不支持OpenCV模块代码提示

    在使用中遇到的问题是,Spyder的代码完成功能不支持某些编译模块(.pyd后缀),如OpenCV的Python模块cv/cv2,在编写脚本文件时,在已存在import cv&import c ...

  7. Vue-router重修01

    ---恢复内容开始--- 1.在vue中获取dom vue中不建议您亲自进行dom操作 vue实例内置ref属性存储或获取相应的dom元素 <div ref="dv"> ...

  8. List集合和JSON互转工具类

    public class JsonListUtil { /** * List<T> 转 json 保存到数据库 */ public static <T> String list ...

  9. Ubunto使用 码云 创建项目

    1.安装 git sudo apt-get install git配置 git 文件 git config --global user.name "你的用户名" git confi ...

  10. IOS - 上APPSTORE为何因IPv6被拒?

    http://blog.csdn.net/wanglixin1999/article/details/52182001