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 ...
随机推荐
- python3 替换字符串中指定位置字符
大家都知道字符串在python中是不可变数据类型,那么我们如何替换字符串中指定位置的字符呢? 字符串转换列表替换并转换解决: def replace_char(string,char,index): ...
- php 将图片转成base64
/** * 获取图片的Base64编码(不支持url) * @date 2017-02-20 19:41:22 * * @param $img_file 传入本地图片地址 * * @return st ...
- 如何查找MySQL中查询慢的SQL语句(转载)
转载自https://www.cnblogs.com/qmfsun/p/4844472.html 如何在mysql查找效率慢的SQL语句呢?这可能是困然很多人的一个问题,MySQL通过慢查询日志定位那 ...
- 重写Object类里equals方法
package com.fff; public class Pet { private String name; private int age; public Pet(String nume,int ...
- java课程之团队开发冲刺阶段1.2
一.总结昨天进度 1.三个任务都已经实现 2.使用时间:四个小时左右 二.遇到的困难 1.对Android原生的侧拉任务栏不了解,导致使用的时候出现了一部分问题 三.今天任务规划 1.对之前的程序重新 ...
- ivew ui
render操作: render:(h, params) => { return h('div', [ h('Button',{ on:{ click:()=>{ this.edit(pa ...
- SQL语句基本
基础 创建数据库 CREATE DATABASE database-name 1 删除数据库 drop database dbname 1 备份sql server 创建 备份数据的 device U ...
- vs2017 调试时 浏览器关闭不想中断调试
解决方案 工具—>选项—>项目和解决方案—>web项目-->去点“浏览器窗口关闭时停止调试”前面的勾去掉>>>
- FFmpeg 结构体学习(五): AVCodec 分析
在上文FFmpeg 结构体学习(四): AVFrame 分析我们学习了AVFrame结构体的相关内容.本文,我们将讲述一下AVCodec. AVCodec是存储编解码器信息的结构体.下面我们来分析一下 ...
- [Swift]LeetCode52. N皇后 II | N-Queens II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...