- 浏览: 19021 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述
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]
]
解题思路
本题的目的是根据不同的数字,构建不同的三角形。需要注意的就是每个数字的产生特点。
自己的代码
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]
]
解题思路
本题的目的是根据不同的数字,构建不同的三角形。需要注意的就是每个数字的产生特点。
自己的代码
package leetcode; import java.util.ArrayList; import java.util.List; public class PascalsTriangle { public List<List<Integer>> generate(int numRows) { List<List<Integer>> list = new ArrayList<List<Integer>>(); if(numRows < 0) return null; else if(numRows == 0) return list; else{ for(int times = 1; times <= numRows; times++){ if(times == 1) { List<Integer> tempList = new ArrayList<Integer>(); tempList.add(1); list.add(tempList); } else if(times == 2){ List<Integer> tempList = new ArrayList<Integer>(); tempList.add(1); tempList.add(1); list.add(tempList); } else{ List<Integer> tempList = new ArrayList<Integer>(); tempList.add(1); for(int i = 1; i < times-1; i++){ int num = list.get(times-2).get(i) + list.get(times-2).get(i-1); tempList.add(num); } tempList.add(1); list.add(tempList); } } } return list; } public static void main(String[] args) { PascalsTriangle pt = new PascalsTriangle(); System.out.println(pt.generate(0)); //System.out.println(pt.generate(1).toString()); //System.out.println(pt.generate(2).toString()); //System.out.println(pt.generate(3).toString()); //System.out.println(pt.generate(4).toString()); //System.out.println(pt.generate(5).toString()); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 297String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 398题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 282题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 418题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 340题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 301题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 326题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 345题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 353题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 322题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 343题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 356题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 306题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 255题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 292题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 231题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 290题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 255题目描述 Given an index k, return t ... -
Plus One
2014-10-19 11:51 334题目描述 Given a non-negative numbe ... -
Merge Sorted Array
2014-10-18 10:45 396题目描述 Given two sorted integer a ...
相关推荐
打印杨辉三角 . 杨辉三角(Pascal's Triangle)是一个在数学中非常著名的数列,它以一种特定的模式排列数字,内容以5种代码实例实现
主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
Generate rows of Pascal s triangle - up to 34 (because of signed integer precision limitation).
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)
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的程度, ...
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, ...
杨辉三角是中国南宋数学家杨辉在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. ...
杨辉三角(Pascal's Triangle)是一个在数学上常见的二维数表,它的构造规则是:每行数字左右对称,每行数字两端的数都是1。从第三行开始,中间的每一个数都是它肩上的两个数之和。以下是一个用C语言实现杨辉三角的...
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_...
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 -...
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 II Spiral Matrix Spiral Matrix II ZigZag Conversion Divide Two Integers Text Justification Max Points on a Line Community QQ 群: 237669375 Github: ...
2.使用数组作为带符号的缓冲区118.Pascal's Triangle -> 理解结构并做167 Two Sum II - 输入数组已排序:使用排序数组的条件,使用前后两个指针35.Search Insert Position -> 线性搜索/二分搜索(左右各有1个间隙) ...
leetcode添加元素使和等于 Leetcode Part1 共55道 1 plusOne easy 描述:用一组数据表示一个整数,实现整数加一的操作 主要思路:主要考虑最高位进位的情况,可以创建一个长度加一的...Pascal's Triangle II easy 描
Pascal's Triangle (杨辉三角) 124 二叉树最大路径和 136 x ^ x = 0 169 Majority Vote Algorithm (最大投票数算法) 240 检索二阶矩阵 189 数组操作的时间复杂度比较 206 反转单向链表 226 反转二叉树 459 重复子...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...