力扣题:二维数组及滚动数组-10.2

力扣题-10.2

[力扣刷题攻略] Re:从零开始的力扣刷题生活

力扣题1:118. 杨辉三角

解题思想:使用temp保存前一行的结果,temp1保存当前行结果,遍历数组即可,需要考虑第一个和最后一个是1

在这里插入图片描述

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        if numRows==1:
            return [[1]]
        else:
            result =[[1]]
            temp = [1]
            for i in range(1,numRows):
                temp1 = []
                for j in range(len(temp)):
                    if j==0:
                        temp1.append(1)
                    else:
                        temp1.append(temp[j-1]+temp[j])
                temp1.append(1)
                result.append(temp1)
                temp=temp1
            return result
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
        if(numRows == 1){
            return {{1}};
        }
        else{
            std::vector<std::vector<int>> result;
            result.push_back({1});
            std::vector<int> temp = {1};
            for (int i = 1; i < numRows; i++){
                std::vector<int> temp1;
                for (int j = 0; j < temp.size(); j++) {
                    if (j == 0){
                        temp1.push_back(1);
                    }
                    else{
                        temp1.push_back(temp[j - 1] + temp[j]);
                    }
                }
                temp1.push_back(1);
                result.push_back(temp1);
                temp = temp1;
            }
            return result;
        }
    }
};

力扣题2:119. 杨辉三角 II

解题思想:
1.与上一题一样,最后return的结果变成当前行即可
2.官方题解:线性递推的方式得到同一行的相邻组合数关系在这里插入图片描述

在这里插入图片描述

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        if rowIndex==0:
            return [1]
        else:
            result =[[1]]
            temp = [1]
            for i in range(0,rowIndex):
                temp1 = []
                for j in range(len(temp)):
                    if j==0:
                        temp1.append(1)
                    else:
                        temp1.append(temp[j-1]+temp[j])
                temp1.append(1)
                result.append(temp1)
                temp=temp1
            return temp
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        if(rowIndex == 0){
            return {1};
        }
        else{
            std::vector<std::vector<int>> result;
            result.push_back({1});
            std::vector<int> temp = {1};
            for (int i = 0; i < rowIndex; i++){
                std::vector<int> temp1;
                for (int j = 0; j < temp.size(); j++) {
                    if (j == 0){
                        temp1.push_back(1);
                    }
                    else{
                        temp1.push_back(temp[j - 1] + temp[j]);
                    }
                }
                temp1.push_back(1);
                result.push_back(temp1);
                temp = temp1;
            }
            return temp;
        }
    }
};
class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        row = [0] * (rowIndex + 1)
        row[0] = 1
        for i in range(1, rowIndex + 1):
            row[i] = row[i - 1] * (rowIndex - i + 1) // i
        return row
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> row(rowIndex + 1);
        row[0] = 1;
        for (int i = 1; i <= rowIndex; ++i) {
            row[i] = 1LL * row[i - 1] * (rowIndex - i + 1) / i;
        }
        return row;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值