Exercise 1.11---1.12 Pascal's Triangle

1.11 F‘ibonates函数的变种,略过。

1.12 题目

The following pattern of numbers is called Pascal‘s triangle.

    1

1    1

1     2    1

1     3     3    1

1     4     6     4     1

......

The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it.35 Write a procedure that computes elements of Pascal‘s triangle by means of a recursive process.

只要按照如下公式计算即可:

  number_at(row, line)

  number_at(1,N)=1

  number_at(N,N)=1

number_at(N,M)=number_at(N-1,M-1)+number_at(N-1,M)

Exercise 1.11---1.12 Pascal's Triangle

时间: 2024-08-10 23:06:37

Exercise 1.11---1.12 Pascal's Triangle的相关文章

118. 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] ] 分析 第k层有k个元素,每层第一个及最后一个元素值为1,对于(k>2)层,第n(n>1 && n < k)个元素A[k][n] = A[k-1][n-1]+A[k-1][n];

119. 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? 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 5 vect

LeetCode OJ: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] ] 帕斯卡三角,很简单的问题,见代码: 1 class Solution { 2 public: 3 vector<vector<int>> generate(int numRows) {

Pascal&#39;s Triangle II 解答

Question Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Solution Similar with Pascal's Triangle. Note that the index starts from 0. 1 public class Solution { 2 public List<Integer> getRow(in

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

1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 vector<vector<int>> tri; 5 if(rowIndex==0) 6 { 7 vector<int> c; 8 c.push_back(1); 9 return c; 10 } 11 vector<int> b; 12 b.push_back(0); 13 b.push_back(1); 14

leetcode 【 Pascal&#39;s Triangle II 】python 实现

题目: 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? 代码:oj测试通过 Runtime: 48 ms 1 class Solution: 2 # @return a list of intege

leetcode 【 Pascal&#39;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 generat

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.

LeetCode119 Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. (Easy) For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 分析: 上一题的followup,注意只能开O(k)的空间,实际上就是利用滚动数组的解法,每一行只与其上一行有关. 代码: 1 clas