题目:

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. Swift基础之设计折线坐标图

    最近添加了折线视图的样式,所以在这里用Swift语言重新再使用设计一下 首先设置纵坐标的数值是:体重 //体重        let weightLabel = UILabel.init(frame: ...

  2. Libgdx教程目录

    Libgdx教程 Note:本教程用的Libgdx 1.9.2版本,在教程更新完毕之前应该不会更新版本.之前的博客中也发表过Libgdx的内容,不过当时都从别人那里拷贝的,因此现在想做一个Libgdx ...

  3. 【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态

    一. Objective-C 方法详解 1. 方法属性 (1) OC 方法传参机制 Object-C 方法传参机制 : OC 中得参数传递都是值传递, 传入参数的是参数的副本; -- 基本类型 (值传 ...

  4. Android View架构总结

    View和Activity的区别 android的四大组件,Activity是四大组件中唯一一个用来和用户进行交互的组件.可以说Activity就是android的视图层. 如果再细化,Activit ...

  5. android6.0SDK 删除HttpClient的相关类的解决方法

    本文转载自博客:http://blog.csdn.net/yangqingqo/article/details/48214865 android6.0SDK中删除HttpClient的相关类的解决方法 ...

  6. 百度的android面试总结分析

    今天就是今天上午10点,我接到了百度的电话面试,当然提前和我说了,我的拖延症是有多强烈,以至于我没怎么准备,当然我也想着看看自己的真实水平,在此检讨一下!!!!!!!!!!!!!!!!!!!!!!!! ...

  7. cocos2dx 3.3 + QT5.3制作游戏编辑器

    欢迎转载,但请注明本blog地址,谢谢_(:зゝ∠)_ http://www.cnblogs.com/marisa/p/4141862.html 主要参考: http://blog.csdn.net/ ...

  8. Java Web 高性能开发,第 2 部分: 前端的高性能

    Web 发展的速度让许多人叹为观止,层出不穷的组件.技术,只需要合理的组合.恰当的设置,就可以让 Web 程序性能不断飞跃.Web 的思想是通用的,它们也可以运用到 Java Web.这一系列的文章, ...

  9. Socket编程实践(8) --Select-I/O复用

    五种I/O模型介绍 (1)阻塞I/O[默认] 当上层应用App调用recv系统调用时,如果对等方没有发送数据(Linux内核缓冲区中没有数据),上层应用Application1将阻塞;当对等方发送了数 ...

  10. 【一天一道LeetCode】#62. Unique Paths

    一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...