算法:杨辉三角(Pascal's Triangle)
一、杨辉三角介绍
杨辉三角形,又称帕斯卡三角形、贾宪三角形、海亚姆三角形、巴斯卡三角形,是二项式系数的一种写法,形似三角形,在中国首现于南宋杨辉的《详解九章算法》得名,书中杨辉说明是引自贾宪的《释锁算书》,故又名贾宪三角形。在那之前,还有更早发现这个三角的波斯数学家和天文学家,但相关的内容没有以图文保存下来,所以中国的数学家对此研究有很大贡献。
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
以上是杨辉三角的前 9 行,可以看出来每一行的所有数字对应着二项式 (A+B)n 的展开式系数,这里 n 从第 0 行开始。
二、杨辉三角的一些性质与实现

此三角形的性质有(注:最顶的 1 处于第 0 行):
由正整数构成,每一行的数字左右对称;
第(2的幂)行都是奇数;
每一行数字之和都是2的幂;
第N行数字个数都是N;
第N行的第K个数字为组合数
;除每行最左侧与最右侧的数字以外,每个数字等于它的左上方与右上方两个数字之和(也就是说,第 N 行第 K 个数字等于第 N-1 行的第 K-1 个数字与第 K 个数字的和)。
因而固有恒等式:

可用此性质写出整个杨辉三角形。
1 /**
2 * 杨辉三角与 (a+b)^n 二项式系数的展开
3 *
4 * @param n
5 * @param k
6 * @return
7 */
8 private static int binomialCoefficient(int n, int k) {
9 int res = 1;
10 if (k > n - k) {
11 k = n - k;
12 }
13 for (int i = 0; i < k; i++) {
14 res *= (n - i);
15 res /= (i + 1);
16 }
17 return res;
18 }
打印杨辉三角的函数:
1 /**
2 * 打印杨辉三角
3 *
4 * @param n
5 */
6 private static void printPascal(int n) {
7 for (int line = 0; line < n; line++) {
8 for (int i = 0; i <= line; i++) {
9 System.out.print(binomialCoefficient(line, i) + " ");
10 }
11 System.out.println();
12 }
13 }
算法的时间复杂度大致为 O(N3),这里 N 为所打印杨辉三角的行数。
算法:杨辉三角(Pascal's Triangle)的更多相关文章
- [Swift]LeetCode118. 杨辉三角 | Pascal's Triangle
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- 杨辉三角(Pascal Triangle)的几种C语言实现及其复杂度分析
说明 本文给出杨辉三角的几种C语言实现,并简要分析典型方法的复杂度. 本文假定读者具备二项式定理.排列组合.求和等方面的数学知识. 一 基本概念 杨辉三角,又称贾宪三角.帕斯卡三角,是二项式系数在三 ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [leetcode-118]Pascal's triangle 杨辉三角
Pascal's triangle (1过) Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...
- [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,Retur ...
- [Swift]LeetCode119. 杨辉三角 II | Pascal's Triangle II
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
随机推荐
- Spring Boot学习(一)——Spring Boot介绍
Spring Boot介绍 Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式 ...
- CentOS7搭建sftp
openssh-server自带sftp服务 1.添加组: groupadd sftp 2.添加不可登录的sftp用户 useradd -u 1001 -g sftp -s /sbin/no ...
- 记一次docker compose的低级错误
记一次docker compose的低级错误 问题 今天在学习dockercompose的时候,启动docker compose up,结果却出现异常 Error response from da ...
- ThinkPHP5通过composer安装Workerman安装失败问题
报错: topthink/think-worker v3.0.2 requires topthink/framework ^6 https://blog.csdn.net/Douz_lungfish/ ...
- webpack4. 使用autoprefixer 无效
解决办法: 在package.json文件中加上这个 "browserslist": [ "defaults", "not ie < 11&qu ...
- Java基础系列(1)- JDK、JRE、JVM
Java三大版本(Write Once:Run Anywhere) JavaSE:标准版 JavaME:嵌入式开发 JavaEE:E企业级开发 JDK.JRE.JVM JDK是开发工具包 JRE是编译 ...
- Django边学边记—新手Django建项目各流程细节
一.准备虚拟环境 1)安装 virtualenv pip install virtualenv 2)virtualenvwrapper pip install virtualenvwrapper-wi ...
- lua自写限制并发访问模块
注意:ngx.say跟ngx.exit是不可以共存,否则会出现ngx.exit无法正常执行 1.定义lua共享内存20m lua_shared_dict ceshi 20m; 2.再location ...
- centos虚拟机中挂新硬盘
配置一台centos7,主硬盘20G装系统:副硬盘20G作为数据盘(格式:XFS)挂载到根目录:/vdir/ ,XFS是高性能文件系统. 外层vm硬盘添加好后,执行下面 1.fdisk -l //查看 ...
- C# 在PPT中添加数学公式
本次内容介绍在C#程序中给PPT幻灯片添加Latex数学公式,添加公式前,首先需要在幻灯片中插入一个Shape形状,在形状的段落中通过方法Paragraphs.AddParagraphFromLate ...