题目:

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]
]

思路

  • 题意是要用数组表示帕斯卡三角形
  • 输入一个数值,显示相应行数的【帕斯卡三角形
  • 根据下一行和上一行的递推公式来处理
  • 设置一个变量fisrt来记录上一行的数据,最新一行的是新建的
  • -

代码

public class Solution {
    public List<List<Integer>> generate(int numRows) {
         List<List<Integer>> all = new ArrayList<List<Integer>>();
        List<Integer> first = new ArrayList<Integer>();
        if(numRows == 0){
            return all;
        }
        if(numRows == 1){
            List<Integer> a1 = new ArrayList<Integer>();
            a1.add(1);
            all.add(a1);
            return all;
        }
        if(numRows > 1){
            List<Integer> a2 = new ArrayList<Integer>();
            a2.add(1);
            all.add(a2);
        }
        for(int i = 1;i < numRows;i++){
            List<Integer> next = new ArrayList<Integer>();
            next.clear();
            next.add(1);
            for(int a = 1;a < i;a++){
                if(a-1 >= 0 )
                next.add(first.get(a-1)+first.get(a));
            }
            next.add(1);
            all.add(next);
            first.clear();
             if(next != null){
                for(int o = 0;o < next.size();o++){
                    first.add(next.get(o));
                }
            }
        }
        return all;
    }
}

LeetCode(30)-Pascal's Triangle的更多相关文章

  1. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  2. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  3. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  4. LeetCode 119. Pascal's Triangle II (杨辉三角之二)

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  5. LeetCode 118. Pascal's Triangle (杨辉三角)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  6. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  7. leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle

    118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...

  8. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  9. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

随机推荐

  1. Eclipse中设置VM参数

    eclipse.ini -Xms256m //设置堆最小值 -Xmx1024m //设置堆最大值 Eclipse 做JVM 的分析时,需要动态设置JVM的参数来进行各种测试, 可以在下图地方进行设置 ...

  2. spring源码系列(一)sring源码编译 spring源码下载 spring源码阅读

    想对spring框架进行深入的学习一下,看看源代码,提升和沉淀下自己,工欲善其事必先利其器,还是先搭建环境吧. 环境搭建 sping源码之前是svn管理,现在已经迁移到了github中了,新版本基于g ...

  3. (NO.00005)iOS实现炸弹人游戏(八):游戏主角(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 最近一直在做另一个RPG游戏,所以本系列迟迟没有更新,上一篇博 ...

  4. MySQL聚簇索引的使用介绍

    MySQL聚簇索引保证关键字的值相近的元组存储的物理位置也相同(所以字符串类型不宜建立聚簇索引,特别是随机字符串,会使得系统进行大量的移动操作),且一个表只能有一个聚簇索引.因为由存储引擎实现索引,所 ...

  5. Docker教程:dokcer machine的概念和安装

    http://blog.csdn.net/pipisorry/article/details/50920982 Docker machine介绍 做为Docker容器集群管理三剑客之一的Docker ...

  6. mac 下终端 操作svn命令 以及出现证书错误的处理方法

    首先,转载地址:http://hi.baidu.com/zhu410289616/item/eaaf160f60eb0dc62f4c6b0e 还有一个地址:http://www.cnblogs.com ...

  7. log4j 日志限制大小 拆分成30个 不按日期分日志 按大小拆分 按日期产生

    先说一下按日期产生,不解释,大家都懂,这种方法的缺点就是很吃硬盘空间 log4j.rootLogger=INFO,logfile,stdout log4j.logger.java.sql=DEBUG, ...

  8. Dynamics CRM 2011/2013 section的隐藏

            代码如下 Xrm.Page.ui.tabs.get("TabName").sections.get("SectionName").setVisi ...

  9. Touch Handling in Cocos2D 3.x(二)

    接受触摸 在Cocos2d 3.0中每一个CCNode和每一个CCNode的子类都可以接收触摸.你只需要开启一个选项.让我们在定制的初始化器里完成它.替换MainScene.m中init方法的代码: ...

  10. git中failed to push some refs to git问题解决及基本使用

    国庆归来准备试用一下git,在提交代码时遇到时遇到一些问题 提交时使用git push origin master 出现failed to push some refs to git 回想一下,创建该 ...