题目:

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. 关于Lt分发系统的时序图分析

    我们已经知道,系统共分为两个模块,mather与son 同时系统允许的操作也有三种,向mather提交war包,我某个服务器更新代码,为所有服务器更新代码 我们一个一个来看 先说,向mather提交w ...

  2. [ExtJS5学习笔记]第十九节 Extjs5中通过设置form.Panel的FieldSet集合属性控制多个field集合

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39209533 官方例子:http://docs.sencha.com/extjs/5. ...

  3. Dynamics CRM2013/2015 插件注册工具登录后无法显示assembly列表问题的解决办法二

    本篇接前面的一篇博文:http://blog.csdn.net/vic0228/article/details/47079717,前篇提供了一种解决方案,将本机系统的语言切换成英文即可,今天再来介绍第 ...

  4. Cocos2D实现RPG队伍菜单任意调整角色顺序的效果

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 前一篇我们实现了队伍实现拖尾效果,但是在实际游戏中我们往往需要 ...

  5. Servlet概述-servlet学习之旅(一)

    Servlet概述 servlet是server+applet的缩写.applet是运行于客户端浏览器的java小程序,java诞生的时候,因为applet而闻名于世,但是现在已经没有多少热使用了,而 ...

  6. 操作系统处理内存时内存页为4k

    windows和unix处理内存时,一个内存页的大小都为4k. 测试代码 int main() { while (1) { int *p = (int *)malloc(1024); getchar( ...

  7. 05 Activity知识

    1.Activity          >概念:活动面板   应用程序组件  可以绘制Ui界面  可以和用户进行交互     默认展示全屏  其他情况 界面比其他窗口小  悬浮在其他窗口上方   ...

  8. 【java集合框架源码剖析系列】java源码剖析之HashSet

    注:博主java集合框架源码剖析系列的源码全部基于JDK1.8.0版本.本博客将从源码角度带领大家学习关于HashSet的知识. 一HashSet的定义: public class HashSet&l ...

  9. LCD 常用的客观效果指标和测试方法

    1.DPI--精密度: 评分标准 DPI 评分 DPI<200 50 200≤DPI<250 60 250≤DPI<300 70 300≤DPI<350 80 350≤DPI& ...

  10. Unity UGUI基础之Button

    UGUI Button,可以说是真正的使用最广泛.功能最全面.几乎涵盖任何模块无所不用无所不能的组件,掌握了它的灵巧使用,你就几乎掌握了大半个UGUI! 一.Button组件: Interactabl ...