- 浏览: 173079 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
简单的方法就是从0开始一层一层的生成,直到生成到第k行,这样空间复杂度为O(n^2), 代码如下:
我们可以进行优化,根据杨辉三角的规律,每一行都是一种组合的形式,例如第k行中的序列为:C(k, 0),C(k, 1),C(k, 2), . . . . .C(k, k - 1),C(k, k)。这样第C(k, i)个元素就是C(k, i - 1) * (k - (i - 1)) / i ,所以我们可以从第一个元素开始,通过这个公式来生成剩余的元素,这样空间复杂度为O(k)。公式的计算期间会有溢出的情况,我们把它转换成Long型。代码如下:
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
简单的方法就是从0开始一层一层的生成,直到生成到第k行,这样空间复杂度为O(n^2), 代码如下:
public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> list = new ArrayList<Integer>(); if(rowIndex < 0) return list; list.add(1); for(int i = 1; i <= rowIndex; i++) { List<Integer> cur = new ArrayList<Integer>(); cur.add(1); for(int j = 0; j < list.size() - 1; j++) { cur.add(list.get(j) + list.get(j + 1)); } cur.add(1); list = cur; } return list; } }
我们可以进行优化,根据杨辉三角的规律,每一行都是一种组合的形式,例如第k行中的序列为:C(k, 0),C(k, 1),C(k, 2), . . . . .C(k, k - 1),C(k, k)。这样第C(k, i)个元素就是C(k, i - 1) * (k - (i - 1)) / i ,所以我们可以从第一个元素开始,通过这个公式来生成剩余的元素,这样空间复杂度为O(k)。公式的计算期间会有溢出的情况,我们把它转换成Long型。代码如下:
public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> list = new ArrayList<Integer>(); if(rowIndex < 0) return list; list.add(1); for(int i = 1; i < rowIndex + 1; i++) { list.add((int)((long)list.get(i - 1) * (rowIndex - i + 1) / i)); } return list; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 224Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 222You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 339Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 330Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 448Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 427Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 425The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 383Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 514Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 528Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 366All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 858Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 879Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 552Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 598Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 761Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 731You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 626For a undirected graph with tre ...
相关推荐
主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
Pascal's Triangle easy O O 119 Pascal's Triangle II easy O 要满足只用一个array大小空间O(k) k为input大小来完成,须具备backtracking概念 151 Reverse Words in a String medium O 这题有点算是easy的程度, ...
2 Pascal’s Triangle 5 3 Binomial Coefficient Identities 11 II Counting: Intermediate 19 4 Finding a Polynomial 21 5 The Upward-Extended Pascal’s Triangle 25 6 Recurrence Relations and Fibonacci ...
leetcode-js Leecode 经典题目 JavaScript TypeScript 题解。 Leetcode's answers by JavaScript and TypeScript. easy 66.加一 (Plus One) 67.二进制求和 (Add Binary) ...119.杨辉三角 II (Pascal's Triangle)
Generate rows of Pascal s triangle - up to 34 (because of signed integer precision limitation).
119_Pascal's_Triangle_II 169_Majority_Element 229_Majority_Element_II 274_H_索引 275_H_Index_II 217_Contain_Duplicate 55_Jump_Game 45_Jump_Game_II 121_Best_Time_to_Buy_and_Sell_Stock 122_Best_Time_to_...
Topics covered: Overview of fractals and chaos theory, feedback and multiple reduction copy machines (MRCMs), the Cantor Set, the Sierpinski Gasket and Carpet, the Pascal Triangle, the Koch Curve, ...
Topics covered: Overview of fractals and chaos theory, feedback and multiple reduction copy machines (MRCMs), the Cantor Set, the Sierpinski Gasket and Carpet, the Pascal Triangle, the Koch Curve, ...
Topics covered: Overview of fractals and chaos theory, feedback and multiple reduction copy machines (MRCMs), the Cantor Set, the Sierpinski Gasket and Carpet, the Pascal Triangle, the Koch Curve, ...
Pascal's Triangle II Spiral Matrix Spiral Matrix II ZigZag Conversion Divide Two Integers Text Justification Max Points on a Line Community QQ 群: 237669375 Github: ...
杨辉三角是中国南宋数学家杨辉在1261年所著的《详解九章算法》一书...杨辉三角,也被称为帕斯卡三角(Pascal's Triangle),是一个二维的数字三角形。它的每一行都基于上一行来构建,并且具有一些非常有趣的数学性质。
Pascal's Triangle v. Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. ...
leetcode添加元素使和等于 Leetcode Part1 共55道 1 plusOne easy 描述:用一组数据表示一个整数,实现整数加一的操作 主要思路:主要考虑最高位进位的情况,可以创建一个长度加一的...Pascal's Triangle II easy 描
杨辉三角(Pascal's Triangle)是一个在数学上常见的二维数表,它的构造规则是:每行数字左右对称,每行数字两端的数都是1。从第三行开始,中间的每一个数都是它肩上的两个数之和。以下是一个用C语言实现杨辉三角的...
Pascal's Triangle #0121 - Best Time to Buy and Sell Stock #0125 - Valid Palindrome #0136 - Single Number #0167 - Two Sum - Input Array is sorted #0189 - Rotate Array #0217 - Contains Duplicate #0242 -...
2.使用数组作为带符号的缓冲区118.Pascal's Triangle -> 理解结构并做167 Two Sum II - 输入数组已排序:使用排序数组的条件,使用前后两个指针35.Search Insert Position -> 线性搜索/二分搜索(左右各有1个间隙) ...
Pascal's Triangle Given two sorted integer arrays A and B, merge B into A as one sorted array.Note: You may assume that A has enough space (size that is greater or equal to m + n)to hold additional ...
Pascal's Triangle (杨辉三角) 124 二叉树最大路径和 136 x ^ x = 0 169 Majority Vote Algorithm (最大投票数算法) 240 检索二阶矩阵 189 数组操作的时间复杂度比较 206 反转单向链表 226 反转二叉树 459 重复子...
462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/...