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 ... 
随机推荐
- 勾勾街——一个专注于免越狱免签名的苹果ios APP打包生成的网站
			自涛舅舅研发的“苹果ios APP自助生成系统”上线以来,每天都有大量的用户注册和生成免越狱app,为什么? 因为我们有明显的技术优势,APP不需要上架appstore, 生成APP又不需要企业签名证 ... 
- JDBC连接Oracle错误ORA-00922: 选项缺失或无效
			以下错误: ORA-00922: 选项缺失或无效 ORA-00922: missing or invalid option 是由于: execute(sql)语句多了个分号 ; 你没看错!!! 在sq ... 
- mysql_Navicat数据库破解
			Navicat Premium 12.1.16.0安装与激活 Navicat Premium 12是一套数据库开发管理工具,支持连接 MySQL.Oracle等多种数据库,可以快速轻松地创建.管理和维 ... 
- vue简单指令笔记
			v-once 执行一次性插值,数据改变插值处内容不会更新 <span v-once>这个将不会改变: {{ msg }}</span> v-text 插入文本 <!--两 ... 
- 关于 js中replace 特殊符号 ‘.’ 的问题
			问题: 字符串转数组,但是分割点在‘.’ ,需要保留每个后缀的 ‘.’ + 类型 解决: let videoType = '.avi.rmvb.rm.asf.divx.mpg.mpeg.mpe.wmv ... 
- Python爬虫(2):urllib库
			爬虫常用库urllib 注:运行环境为PyCharm urllib是Python3内置的HTTP请求库 urllib.request:请求模块 urllib.error:异常处理模块 urllib.p ... 
- 最新手机号正则表达式 java 、javascript版正则表达式验证是否为11位有效手机号码
			最近在做注册登陆页面,都要涉及到验证11位有效手机号码,这里贴出代码,希望能帮到有这个开发需求的朋友. function isPoneAvailable($poneInput) { var myreg ... 
- 【RL-TCPnet网络教程】第32章    RL-TCPnet之Telnet服务器
			第32章 RL-TCPnet之Telnet服务器 本章节为大家讲解RL-TCPnet的Telnet应用,学习本章节前,务必要优先学习第31章的Telnet基础知识.有了这些基础知识之后,再搞 ... 
- [Swift]LeetCode336. 回文对 | Palindrome Pairs
			Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ... 
- BUGKU-逆向(reverse)-writeup
			目录 入门逆向 Easy_vb Easy_Re 游戏过关 Timer(阿里CTF) 逆向入门 love LoopAndLoop(阿里CTF) easy-100(LCTF) SafeBox(NJCTF) ... 
