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

原题链接:https://oj.leetcode.com/problems/pascals-triangle/

题目 :给定n,生成n行的帕斯卡三角形。

思路:帕斯卡三角形 也就是 杨辉三角形,依据数学知识,知当中每一行的数字代表的是 (a+b)^n
的系数。于是此题能够转化为求组合数 C(n,k)。把每一行的系数算出来就可以。

	public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if (numRows < 1)
return list;
for (int i = 0; i < numRows; i++) {
List<Integer> li = new ArrayList<Integer>();
for (int j = 0; j <= i; j++) {
li.add(Integer.valueOf(cnk(i, j) + ""));
}
list.add(li);
}
return list;
} //求组合数
public static BigInteger cnk(int n, int k) {
BigInteger fenzi = new BigInteger("1");
BigInteger fenmu = new BigInteger("1");
for (int i = n - k + 1; i <= n; i++) {
String s = Integer.toString(i);
BigInteger stobig = new BigInteger(s);
fenzi = fenzi.multiply(stobig);
}
for (int j = 1; j <= k; j++) {
String ss = Integer.toString(j);
BigInteger stobig2 = new BigInteger(ss);
fenmu = fenmu.multiply(stobig2);
}
BigInteger result = fenzi.divide(fenmu);
return result;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode——Pascal&#39;s Triangle的更多相关文章

  1. LeetCode——Pascal&#39;s Triangle II

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

  2. leetcode - Pascal&#39;s Triangle

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

  3. [LeetCode]Pascal&#39;s Triangle II

    题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...

  4. 【Leetcode】Pascal&#39;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&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

  6. leetcode笔记:Pascal&#39;s Triangle

    一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...

  7. leetcode:Pascal&#39;s Triangle

    一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...

  8. Pascal&#39;s Triangle I,II

    题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...

  9. Pascal&#39;s Triangle II

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

随机推荐

  1. 类似于qq联系人的tablview能够展开和收缩

    在.h文件中定义三个数组和一个tablview UITableView *listTable;     NSMutableArray *listArray;     NSMutableArray *p ...

  2. JAVA 保留两位小数的四种方法

    import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; publiccl ...

  3. socket用法

    INADDR_ANY是ANY,是绑定地址0.0.0.0上的监听, 能收到任意一块网卡的连接:INADDR_LOOPBACK, 也就是绑定地址LOOPBAC, 往往是127.0.0.1, 只能收到127 ...

  4. ExtJS与JQuery对照

    首先在介绍ExtJS和JQuery,然后进行比较 一个.什么是ExtJS? 1.ExtJS能够用来开发RIA也即富client的AJAX应用,是一个用javascript写的,主要用于创建前端用户界面 ...

  5. java split小结(转)

    2016.03.27下午参加华为机试,简单扫了一眼几个题的标题,选择了一道字符串问题,其实该题非常非常的简单,可以说是简单的不能再简单了,而且有很多种解法,上机时我选择了直接借用java提供的一些函数 ...

  6. 上传App时遇IDFA错误问题

    今天上传App时遇到下图1的情况,很纳闷,又是苹果新规. 通常是第三方的库引起啦,马上想到百度统计了,打开SDK下载页面看看简单介绍,里面有讲到这个问题了. 图2就是这次改动的原因. 更新SDK,之前 ...

  7. Openfire开发配置,Openfire源码配置,OpenFire二次开发配置

    1.下载源码:http://www.igniterealtime.org/downloads/source.jsp 2.把源码解压出的openfire_src目录放至eclipse workplace ...

  8. WPF DataGrid_SelectChanged获取单元内容

    private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)        {          ...

  9. 猫学习IOS(三)UI纯代码UI——图片浏览器

    猫分享.必须精品 看看效果 主要实现相似看新闻的一个界面,不用拖拽,纯代码手工写. 首先分析app能够非常easy知道他这里有两个UILabel一个UIImageView还有两个UIButton 定义 ...

  10. MVC模型与FishiGUI应用层MVC型号

    MVC概要: MVC (Modal View Controler)M是指数据模型,V是指用户界面,C则是控制器. 使用MVC的目的是将M和V的实现代码分离,从而使同一个程序能够使用不同的表现形式.比方 ...