Datawhale LeetCode腾讯精选50——Task06

LeetCode 043 字符串相乘

Given two non-negative integers num1 and num2 represented as strings, 
return the product of num1 and num2, also represented as a string.

Note: You must not use any built-in BigInteger library 
or convert the inputs to integer directly.

在这里插入图片描述
思路一:竖式乘法,图片出自Leetcode 43:字符串相乘(超详细的解法!!!。具体解题思路和代码也可以看这个作者的文章。
在这里插入图片描述
思路二:直接字符串转整数,然后相乘,不符合题目要考察的意图,但是代码是真的简洁。代码出自Leetcode算法问题43——字符串乘法(Python实现),leetcode,相乘,python

class Solution(object):
    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """      
        return str(int(num1)*int(num2))

或者str(eval(num1+'*'+num2))
LeetCode043字符串相乘官方解决方案

LeetCode 046 全排列

Given an array nums of distinct integers, return all the possible permutations. 
You can return the answer in any order.

在这里插入图片描述
思路一:调用python的permutations()函数。
思路二:递归
思路三:回溯
每种思路的解法参见LeetCode | 0046. Permutations全排列【Python】
这里当然按惯例,放最简单的代码了(即思路一),哦耶~

from itertools import permutations

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        # solution one: permutations
        return list(permutations(nums))
[添加链接描述](https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/solution/mian-shi-ti-38-zi-fu-chuan-de-pai-lie-hui-su-fa-by/)
作者:Wonz
链接:https://juejin.cn/post/6844904081689935879
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

LeetCode46字符串全排列官方解决方案

LeetCode 053 最大子序和

Given an integer array nums, find the contiguous subarray 
(containing at least one number) which has the largest sum and return its sum.

在这里插入图片描述
在这里插入图片描述
思路一:动态规划
个人觉得写的比较好的一个博文LeetCode 第 53 题:“最大子序和”题解。代码也出自这个博文。

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        max = nums[0]
        sum = 0
        for i in nums:
            sum += i
            if sum > max:
                max = sum
            if sum < 0:
                sum = 0
        return max

思路二:分治法
参见leetcode 53 最大子序和 python。思路一提供的文章链接中也有关于分治法的解释,最详细的解释还是看LeetCode53最大子序和官方解决方案
官方解决方案中提到的第二种分治法类似于「线段树求解 LCIS 问题」的 pushUp 操作,感兴趣的可以研究下线段树。

任务链接

team-learning-program/LeetCodeTencent/043 字符串相乘.md
team-learning-program/LeetCodeTencent/046 全排列.md
team-learning-program/LeetCodeTencent/053 最大子序和.md

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值