leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2

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<vector<int>> generate(int numRows) {
         vector<vector<int>> res = {};
        for (int i = 0; i < numRows; i++) {
            res.push_back(vector<int>(i + 1, 1));
            for(int j = 1; j < i; j++) {
                res[i][j] = (res[i - 1][j] + res[i - 1][j - 1]);
            }
        }
        return res;

    }

Pascal‘s Triangle II
Total Accepted: 46342
Total Submissions: 157260

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?

我的解决方案:

从没一行的倒数第二个算起,往前面逆推:

 vector<int> getRow(int rowIndex)
    {
        vector<int> result(rowIndex + 1, 1);

        for(int i = 1; i <= rowIndex; ++i)
        {
            for(int j = i - 1; j > 0; --j)
            {
                result[j] = result[j] + result[j - 1];
            }
        }

        return result;
    }

递归的解决方案:

vector<int> getRow(int rowIndex) {
    vector<int> result;

    if (rowIndex == 0) {
        result.push_back(1);

        return result;
    } else {
        vector<int> vec = getRow(rowIndex - 1);
        result.push_back(1);
        for (size_t i = 0; i < vec.size() - 1; i++) {
            result.push_back(vec[i] + vec[i+1]);
        }
        result.push_back(1);
    }
}

python 解决方案:

class Solution:
# @param {integer} rowIndex
# @return {integer[]}
def getRow(self, rowIndex):
    row = [1]
    for i in range(1, rowIndex+1):
        row = list(map(lambda x,y: x+y, [0]+row, row + [0]))
    return row

版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2

时间: 2024-12-21 08:29:56

leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2的相关文章

118. 119. 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] ] class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<

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

LeetCode开心刷题五十一天——118. Pascal&#39;s Triangle 接触跳转表概念,不知用处 lamda逗号导致表达式加法奇怪不理解119. Pascal&#39;s Triangle II

118. Pascal's Triangle Easy 87984FavoriteShare Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1],

C#解leetcode: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? (注意:这里要求空间为O(k)) 一个满足条件的答案如下: public class Solution { public IList<int

LeetCode 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? 题目标签:Array 这道题目与之前那题不同的地方在于,之前是给我们一个行数n,让我们把这几行的全部写出来,这样就可以在每写新的一行的时候根据之前的那

LeetCode 119 Pascal&#39;s Triangle II(帕斯卡三角形II)(vector、数学公式)(*)

翻译 给定一个索引K,返回帕斯卡三角形的第K行. 例如,给定K=3, 返回[1,3,3,1]. 注释: 你可以改进你的算法只用O(k)的额外空间吗? 原文 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 sp

leetcode || 118、Pascal&#39;s Triangle

problem: 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] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值

[LeetCode] 119. Pascal&#39;s Triangle II 杨辉三角 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? 118. Pascal's Triangle 的拓展,给一个索引k,返回杨辉三角的第k行. 解法:题目要求优化到 O(k) 的空间复杂,那么就不能把每

Java [Leetcode 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]. 解题思路: 每次在上一个list前面插入1,然后后面的每两个间相加赋值给前一个数. 代码描述: public class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> r