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 ...
随机推荐
- python基础课程_2学习笔记3:图形用户界面
图形用户界面 丰富的平台 写作Python GUI程序前,须要决定使用哪个GUI平台. 简单来说,平台是图形组件的一个特定集合.能够通过叫做GUI工具包的给定Python模块进行訪问. 工具包 描写叙 ...
- Algorithm Part I:Priority Queues
1.binary heap实现 BinaryHeap.h #ifndef BINARYHEAP_H #define BINARYHEAP_H class BinaryHeap { public: Bi ...
- Difference between datacontract and messagecontract in wcf
在WCF中有两种契约各自是DataContract和MessageContract,这篇博客来讲一下两者的差别.先看一下两者定义契约实体的方式有和不同. 1.数据契约 <span style=& ...
- UVa 884 - Factorial Factors
题目:输出n!中素数因数的个数. 分析:数论.这里使用欧拉筛法计算素数,在计算过程中求解就可以. 传统筛法是利用每一个素数,筛掉自己的整数倍: 欧拉筛法是利用当前计算出的全部素数,乘以当前数字筛数: ...
- MYSQL正在使用select发现现场记录方法,包括一个逗号分隔的字符串
首先,我们创建一个逗号分隔字符串. CREATE TABLE test(id int(6) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),pname VARCHAR ...
- 用java读写properties文件的代码
package com.LY; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.F ...
- windows phone 7 通过麦克风录音,并且播放
原文:windows phone 7 通过麦克风录音,并且播放 //模拟XNA的框架(凡是在wp7中应用xna的都必须先模拟此类) public class XNAAsyncDispatcher : ...
- xml publisher根据条件显示或隐藏列
xml publisher根据条件显示或隐藏列 <?if@column:condition? > -- <?end if?> 样例: 依据PROJECT_FLAG标签显示 ...
- HibernateReview Day1 - Introduction
Hibernate已经学过去大概有半个月了,然后默默的忘掉了……所谓Practice makes perfect. 我尽力重新拾起来. 1.什么是ORM 在介绍Hibernate之前,我们先学习下OR ...
- Js常用技巧
摘录:http://crasywind.blog.163.com/blog/static/7820316920091011643149/ js 常用技巧 1. on contextmenu=" ...