leetcode 【 Pascal's Triangle 】python 实现

题目

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]
]

代码:oj测试通过 Runtime: 46 ms

 1 class Solution:
 2     # @return a list of lists of integers
 3     def generate(self, numRows):
 4         if numRows < 1:
 5             return []
 6         pascal = []
 7         first_row = [1]
 8         pascal.append(first_row)
 9         for i in range(1,numRows):
10             tmp = []
11             tmp.append(1)
12             for j in range(len(pascal[i-1])):
13                 if j == len(pascal[i-1])-1:
14                     tmp.append(1)
15                 else:
16                     tmp.append(pascal[i-1][j] + pascal[i-1][j+1])
17             pascal.append(tmp)
18         return pascal

思路

排除几个special case

然后把pascal写成如下的形式,比较容易写代码:

[1]

[1,1]

[1,2,1]

[1,3,3,1]

[1,4,6,4,1]

leetcode 【 Pascal's Triangle 】python 实现

时间: 2024-07-31 22:15:13

leetcode 【 Pascal's Triangle 】python 实现的相关文章

LeetCode——Pascal&#39;s Triangle

Description: 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] ] public class Solution { public List<List<Integer>> generate(int numRows) { List

LeetCode——Pascal&#39;s Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

LeetCode: Pascal&#39;s Triangle 解题报告

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] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven 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 us

(LeetCode)Pascal&#39;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] ] Subscribe to see which companies asked this question 解题分析: 题目的这个帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉

(LeetCode)Pascal&#39;s Triangle II --- 杨辉三角进阶(滚动数组思想)

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? Subscribe to see which companies asked this question 解题分析: 此处有空间的限制,因此不能正

LeetCode Pascal&#39;s Triangle II (杨辉三角)

题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if(rowIndex==0) return vector<int>(1,1); //0和1特殊处理 5 if(rowIndex==1) return vector<int>(2,1); 6

LeetCode: Pascal&#39;s Triangle [118]

[题目] 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] ] [题意] 给定整数numRows, 要求生成杨辉三角的前numRows行 [思路] 杨辉三角有以下特点: 1. 第n行有n个元素 2. 每行的收尾都是1, 其余元素A[n][i]=A[n-

LeetCode: Pascal&#39;s Triangle II [119]

[题目] 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? [题意] 给定行索引k, k从0开始,返回该索引指向的杨辉三角的行 要求只能使用O(k)的额外空间 [思路] 申请两个k+1大小的数组,

[LeetCode]Pascal&amp;#39;s Triangle

称号:定行数n,生成n帕斯卡三角行 算法:步骤通过阵列工序 public class Solution { public List<List<Integer>> generate(int numRows) { if (numRows < 0) { return null; } List<List<Integer>> pascalTriangle = new ArrayList<List<Integer>>(); for (int