118. Pascal's Triangle (Graph; WFS)

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]
]
class Solution {
public:
    vector<vector<int> > generate(int numRows) {
        vector<vector<int>> result(numRows, vector<int>());
        if(numRows == 0) return result;
        result[0].push_back(1);
        for(int i =1; i < numRows; i++) //深度其次
        {
            result[i].push_back(1);
            for(int j = 1; j<i;j++) //广度优先
            {
                result[i].push_back(result[i-1][j-1]+result[i-1][j]);
            }
            result[i].push_back(1);
        }
        return result;
    }
};

118. Pascal's Triangle (Graph; WFS)

时间: 2024-10-23 12:12:32

118. Pascal's Triangle (Graph; WFS)的相关文章

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

leetcode 118 Pascal&#39;s Triangle ----- java

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 { List list = new ArrayList<List<Integer>>(); public

No.118 Pascal&#39;s Triangle

No.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] ] 解法:其实很简单,想清楚就好,找好规律!! 1 #include "stdafx.h" 2 #include <string>

leetCode 118. Pascal&#39;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] ] 题目大意: 输入行数,输出如上图所示的数组.(杨辉三角) 思路: 用双vector来处理当前行和下一行. 代码如下: cla

【leetcode】118. Pascal&#39;s Triangle

@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) /************************ * @description: simple. * @time_complexity: O(n) * @space_complexity: O(n) ****

[leedcode 118] 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] ] public class Solution { public List<List<Integer>> generate(int numRows) { //杨辉三角形,把每一行从头对齐,每

LeetCode 118. 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] ] 题目标签:Array 题目给了我们一个numRows,让我们写出这个行数的杨辉三角.来观察一下原题例子,5行的话,第一行只有1,第二行,只有1,第三行,除去第一个1和最后一个1,中间的都是上一行的两边

Leet Code OJ 118. Pascal&#39;s Triangle [Difficulty: Easy]

题目: 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以外,其他元素均可由上行推出,本方案采用lastLine保存上行数

leetcode 118. Pascal&#39;s Triangle &amp;&amp; leetcode 119. Pascal&#39;s Triangle II

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] ]nextline[j] = currentline[j] + currentline[j+1] 1 vector<vector<int> > generate(int numRows) 2