leetcode笔记: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]
]

二. 题目分析

关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk4lmbkb1aqbqikBU-ndiMlTF20fq2QUjTTFTeTohZ72KFxgBnz4sJha

将该三角形的左边对齐。就行发现,tri[i][j] = tri[i-1][j-1] + tri[i-1][j]。因此这里的代码并没有什么技巧。

三. 演示样例代码

class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> result;
        if (numRows <= 0)
            return result;
        else
        {
            vector<int> temp(1, 1);
            result.push_back(temp);
            if (numRows == 1) return result;
            temp.push_back(1);
            result.push_back(temp);
            if (numRows == 2) return result;

            for (int i = 2; i < numRows; ++i)
            {
                vector<int> temp(i + 1, 1);
                for (int j = 1; j < i; ++j)
                    temp[j] = result[i - 1][j - 1] + result[i - 1][j];
                result.push_back(temp);
            }
        }
        return result;
    }
};

leetcode笔记:Pascal's Triangle

时间: 2024-08-03 07:38:20

leetcode笔记:Pascal&#39;s Triangle的相关文章

leetcode:Pascal&amp;#39;s Triangle

一.     题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二.     分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数的和值 须要注意的是,当行数为0时输出[[1]] 结果为一个二维数组,所以不难想到解决方式. 每层保存前一行的指针,然后当前行数据依据上一行来得到,每一个元素就是上一行两个相邻元素相加(第一个和最后一个元素是1). 算法时间复杂度应该是O(1+2+3+...+n)=O(n^2),空间上仅仅须要二维数

【Leetcode】Pascal&amp;#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? 思路:最简单的方法就是依照[Leetcode]Pascal's Triangle 的方式自顶向下依次求解,但会造成空间的浪费.若仅仅用一个vect

[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

LeetCode——Pascal&amp;#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] ] 原题链接:https://oj.leetcode.com/problems/pascals-triangle/ 题目 :给定n,生成n行的帕斯卡三角形. 思路:帕斯卡三角形 也就是 杨辉三角形,依据

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

题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public List<Integer> getRow(int rowIndex) { if (rowIndex < 0) { return null; } List<List<Integer>> pascalTriangle = new ArrayList<List<I

LeetCode 119:Pascal&amp;#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]. 题目要求计算杨辉三角某一行的元素.这个也是二项式系数的计算问题. class Solution { public: vector<int> getRow(int rowIndex) { vector<int> result; vector<int> tmp;

Pascal&amp;#39;s Triangle I,II

题目来自于Leetcode https://leetcode.com/problems/pascals-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] ] class Solution { public: vector<vecto

Pascal&amp;#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? Hide Tags Array vector<int> getRow(int rowIndex) { vector<int> res

LeetCode118:Pascal&amp;#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 //解题思路:利用一个中间vector来保存每层的数 class Solution { pu