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. Linux多核并行编程关键技术

    多核并行编程的背景 在摩尔定律失效之前,提升处理器性能通过主频提升.硬件超线程等技术就能满足应用需要.随着主频提升慢慢接近撞上光速这道墙,摩尔定律开始逐渐失效,多核集成为处理器性能提升的主流手段.现在 ...

  2. opencv imwrite保存图片花屏的问题

    问题:在项目中用opencv的imwrite保存图片出现花屏的问题,如下图: 思路:1.  因为项目中的图像数据(float类型,0-255)是在GPU中,保存的话:可以用CPU保存图片,也可以用GP ...

  3. PHP使用CURL抓取网页

    CURL是一个非常强大的开源库,支持很多协议,包括HTTP.FTP.TELNET等,我们使用它来发送HTTP请求.它给我 们带来的好处是可以通过灵活的选项设置不同的HTTP协议参数,并且支持HTTPS ...

  4. centos查看apache用的是哪个httpd.conf

    httpd -V得到类似如下结果: -D HTTPD_ROOT="/etc/httpd" -D SERVER_CONFIG_FILE="conf/httpd.conf&q ...

  5. 调用获取学生信息的接口,保存到excel里面

    # 2.http: // doc.nnzhp.cn / index.php?s = / 6 & page_id = 14# 调用获取学生信息的接口,保存到excel里面 import requ ...

  6. 锐捷交换机配置DHCP SERVER给固定的MAC地址分配静态IP

    今天突发奇想,想给自己的手机分配固定地址,使得接入公司无线网络时每次都取到同一ip地址,这样可以排除认证登录问题. 上网溜达一下,记录下锐捷官方的[常见问题]如下,经验证可行. 需求: 给MAC地址为 ...

  7. 按模板批量修改Excel文件内容

    Sub 按模板修改Excel文件() Dim MoBanWorkBook As Workbook Set MoBanWorkBook = Application.ActiveWorkbook Dim ...

  8. vue table-tree 组件

    最近接到一个需要使用table-tree开发 百度的一圈.什么的都有.感觉不怎么靠谱.最后找到一个感觉挺 huo shi 先附上demo和代码地址: 代码地址:https://github.com/s ...

  9. window10 Docker仓库访问

    window10 Docker仓库访问 docer官网 docker仓库 windown10 安装docker可以参考 window10安装docker 配置了加速器以后还访问不了,点击托盘处dock ...

  10. 小测D

    就是二分查找就够了,找到符合条件的那个最小值 不会二分可以去学一下,可以看看这个:https://www.cnblogs.com/wzl19981116/p/9354012.html #include ...