LeetCode——Pascal's Triangle
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's Triangle的更多相关文章
- LeetCode——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 ...
- leetcode - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- [LeetCode]Pascal's Triangle II
题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...
- 【Leetcode】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 ...
- 【leetcode】118. Pascal's Triangle
@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...
- leetcode笔记:Pascal's Triangle
一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- leetcode:Pascal's Triangle
一. 题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二. 分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...
- Pascal's Triangle I,II
题目来自于Leetcode https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numR ...
- 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 ...
随机推荐
- 解决eclipse project前出现红色感叹号 但没有提示错误出在什么地方build path jar包也没报错
在problems窗体能够看到错误信息 菜单条 window-->show view-->problems 或alt+shift+q 然后按x 又一次选一下包就攻克了
- mongodb常用操作语句
mongodb常用操作语句 A:创建数据表 db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean&g ...
- 再探vim经常使用命令
最開始学习过vim,见 http://blog.csdn.net/u011848617/article/details/12837873 之后以前不了了之,当再次学习后,发现经常使用命令的掌握还 ...
- spring的长处 ioc aop
spring 的长处? 1.减少了组件之间的耦合性 ,实现了软件各层之间的解耦 2.能够使用easy提供的众多服务.如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它非 ...
- hdu 4771 Stealing Harry Potter's Precious
题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...
- Android中Dialog的使用
上一篇博文讲到对话框popWindow的使用,这篇博文主要解说Dialog的使用. 1.什么是Dialog? Dialog就是对话框的一种方式! 在Android开发中.我们常常会须要在Android ...
- 由于空间,注定的结果——第五届山东省ACM编程比赛总结
应该是,这是一个很失败的结果.目前省赛玩具.作为志愿者说,,铁匠是一个很丢人的事. 作为队长.全然没有想到会是这种一次旅程.尽管由于去baidu的实习和各种offer的申请,对acm抱着能水就水绝不深 ...
- ios01
http://code4app.com/ 0.全部Swift http://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/ ...
- ALSA安装编程指南
ALSA全指南 一.什么是ALSA ALSA是Advanced Linux Sound Architecture,高级Linux声音架构的简称,它在Linux操作系统上提供了音频和MIDI(Mu ...
- [转载] RaspberryPi B+ WiringPi 引脚对应图
Pin Numbering - Raspberry Pi Model B+ Numbering Scheme Expansion Header J8 Pinout (40-pin Header) Ad ...