帕斯卡三角形
给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
示例:
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
 
思路:利用三角形性质 1*(n-1)*(n-2)/2 *(n-3)/3  * (n-4)/4    和杨辉三角形n行含有n个数字的数学性质。
本题可以通过数学性质来获得求解。代码如下:   注意1*(n-1)*(n-2)/2 *(n-3)/3  * (n-4)/4中Int型数组导致运算结果为0的情况(如2/5=0,1/2=0)
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
for(int i=1;i<=numRows;i++){
List<Integer> list=new ArrayList();
list.add(1);
double temp=1;
for(int j=1;j<i;j++){
temp=(temp*(i-j))/j; //这个地方不能写成temp*=(i-j)/j 因为是Int型 (i-j)/j 会变成0.
list.add((int)temp);
} res.add(list);
}
return res;
}
}

另一种解法根据数组的性质,每一个数字都是上一组的前一个数与后一个数之和。

class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> pascal = new ArrayList<List<Integer>>();
ArrayList<Integer> row = new ArrayList<Integer>();
for (int i = 0; i < numRows; i++) {
row.add(0, 1); //(0,1)中的0是索引,1是值。 每一组首先添加1进行运算
for (int j = 1; j < row.size() - 1; j++)
row.set(j, row.get(j) + row.get(j + 1)); //上一组前两个数字的和
pascal.add(new ArrayList<Integer>(row));
}
return pascal;
}
}
class Solution {
    public List<List<Integer>> generate(int numRows) {
       List<List<Integer>> pascal = new ArrayList<List<Integer>>();
        ArrayList<Integer> row = new ArrayList<Integer>();
        for (int i = 0; i < numRows; i++) {
            row.add(0, 1);     //(0,1)中的0是索引,1是值。 每一组首先添加1进行运算
            for (int j = 1; j < row.size() - 1; j++)
                row.set(j, row.get(j) + row.get(j + 1));    //上一组前两个数字的和
            pascal.add(new ArrayList<Integer>(row));
        }
        return pascal;
    }
}

leetcode-帕斯卡三角形的更多相关文章

  1. 119 Pascal's Triangle II 帕斯卡三角形 II Pascal's Triangle II

    给定一个索引 k,返回帕斯卡三角形(杨辉三角)的第 k 行.例如,给定 k = 3,则返回 [1, 3, 3, 1].注:你可以优化你的算法到 O(k) 的空间复杂度吗?详见:https://leet ...

  2. 118 Pascal's Triangle 帕斯卡三角形 杨辉三角形

    给定 numRows, 生成帕斯卡三角形的前 numRows 行.例如, 给定 numRows = 5,返回[     [1],    [1,1],   [1,2,1],  [1,3,3,1], [1 ...

  3. 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 ...

  4. leetcode-118-Pascal's Triangle(生成具有n行的帕斯卡三角形)

    题目描述: Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. Example ...

  5. 【LeetCode-面试算法经典-Java实现】【118-Pascal&#39;s Triangle(帕斯卡三角形)】

    [118-Pascal's Triangle(帕斯卡三角形(杨辉三角))] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given numRows, generate ...

  6. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. [LeetCode 120] - 三角形(Triangle)

    问题 给出一个三角形,找出从顶部至底部的最小路径和.每一步你只能移动到下一行的邻接数字. 例如,给出如下三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 从顶部至底部的最 ...

  8. LeetCode 976. 三角形的最大周长(Largest Perimeter Triangle) 33

    976. 三角形的最大周长 976. Largest Perimeter Triangle 题目描述 给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的.面积不为零的三角形的最大周长. ...

  9. leetcode 有效三角形的个数

    题目描述: 平明伞兵解法: 既然要求满足三角形要求的三边,简单来说,就是最短两边之和大于第三边,所以,第一步Arrays.sort().先排序,然后直接伞兵暴力法,三重循环.当然最后肯定是能跑出来的, ...

  10. LeetCode Triangle 三角形(最短路)

    题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...

随机推荐

  1. VC MFC工具栏(CToolBar)控件(转)

    工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来完成,事实上任何一种控件,都可以用代码创建, ...

  2. careercup-扩展性和存储限制10.4

    题目 有一个数组,里面的数在1到N之间,N最大为32000.数组中可能有重复的元素(即有的元素 存在2份),你并不知道N是多少.给你4KB的内存,你怎么把数组中重复的元素打印出来. 解答 我们有4KB ...

  3. oracle11g的安装与卸载

    1.首先确认电脑已安装 .NET Framework3.5 2.本地计算机的安装: 参考:https://www.cnblogs.com/hoobey/p/6010804.html 3.服务器的安装: ...

  4. SpringBoot使用maven插件打包時報:[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException的處理方案

    SpringBoot使用maven插件打包時報:[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExec ...

  5. java 计算器算法脚本

    import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public String Count(Strin ...

  6. Spark在实际项目中分配更多资源

    Spark在实际项目中分配更多资源 Spark在实际项目中分配更多资源 性能调优概述 分配更多资源 性能调优问题 解决思路 为什么调节了资源以后,性能可以提升? 性能调优概述 分配更多资源 性能调优的 ...

  7. 【C】数据类型和打印(print)

    char -128 ~ 127 (1 Byte) unsigned char 0 ~ 255 (1 Byte) short -32768 ~  32767 (2 Bytes) unsigned sho ...

  8. C++函数调用之——值传递、指针传递、引用传递

    1.简介 1.值传递:形参时实参的拷贝,改变函数形参并不影响函数外部的实参,这是最常用的一种传递方式,也是最简单的一种传递方式.只需要传递参数,返回值是return考虑的:使用值传递这种方式,调用函数 ...

  9. Zabbix 3.4.11监控 apache服务,ftp服务的配置

    一 zabbix 的安装部署 略 二监控 apache服务的配置 首先在本机下载模板:https://github.com/rdvn/zabbix-templates/archive/m aster. ...

  10. c语言:矩阵相乘-矩阵相加 新手练习1

    #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> voi ...