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 ...
随机推荐
- hdu4714(树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意:给你一棵树,,其中每去掉一条边或加一条边的代价均为1,让你求出将其变成一个圆的最小代价. ...
- Cocos2D & SpriteBuilder Developer Guide
https://www.makegameswith.us/docs/#!/cocos2d/1.0/overview
- 详解CMS垃圾回收机制
原创不易,未经允许,不得转载~~~ 什么是CMS? Concurrent Mark Sweep. 看名字就知道,CMS是一款并发.使用标记-清除算法的gc. CMS是针对老年代进行回收的GC. CMS ...
- Android点滴---ViewHolder通用,优雅写法
近期在做项目时,又要写 ViewHolder. 突然想到网上看看有没什么好的写法! 不知道你是不是也烦透了写那些没有技术含量的ViewHolder 看看这些.也许会有收获! 然后就找到了以下两篇文章( ...
- 不root的情况下 查看App的数据表
一直以来查看sqlite的数据库都须要root才干查看,可是公司的好多測试机root起来比較麻烦~~~ 近期刚好项目上线,略闲, 于是决定写一个libraryproject便于以后调试代码 关键代码例 ...
- .net Work Flow 4.0
工作流 依据 WfMC 的定义.工作流(WorkFlow)就是自己主动运作的业务过程部分或总体.表现为參与者对文件.信息或任务依照规程採取行动,并令其在參与者之间传递.官方的总是非常抽象,抽象是为了可 ...
- 【设计优化】-使用缓冲(Buffer)提高程序性能
缓冲区是一块特定的内存区域.开辟缓冲区的目的是通过缓解应用程序上下层之间的性能差异,提高系统性能. 缓冲能够协调上层组件和下层组件的性能差异.当上层组件性能因为下层组件时,能够有效地降低上层组件对下层 ...
- java reflect反思总结
--------------TestBean package lh.demo.bean; public class TestBean { private String userName; priv ...
- Enhancing the Application: Advanced JDBC Features(转)
Enhancing the Application: Advanced JDBC Features This chapter describes additional functionality th ...
- PocketSphinx语音识别系统语言模型的训练和声学模型的改进
PocketSphinx语音识别系统语言模型的训练和声学模型的改进 zouxy09@qq.com http://blog.csdn.net/zouxy09 关于语音识别的基础知识和sphinx的知识, ...