Leetcode精选50题-Day06
043. 字符串相乘
1. 题目描述
2. 思路&代码
人为建立映射
class Solution:
def multiply(self, num1: str, num2: str) -> str:
numDict = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
l1 = list(num1)
l2 = list(num2)
n1 = 0
n2 = 0
bit1 = len(num1) - 1
bit2 = len(num2) - 1
for i in l1:
for key in numDict.keys():
if i == key:
n1 += numDict[key] * 10**bit1
bit1 -= 1
for j in l2:
for key in numDict.keys():
if j == key:
n2 += numDict[key] * 10**bit2
bit2 -= 1
return str(n1*n2)
作者:yi-wen-statistics
链接:https://leetcode-cn.com/problems/multiply-strings/solution/bao-li-qiu-jie-by-yi-wen-statistics-5/
竖式计算:
class Solution:
def multiply(self, num1: str, num2: str) -> str:
if num1 == '0' or num2 == '0':
return '0'
initial = self.oneToManyMultiply(num1[-1], num2)
start = 1
for i in range(len(num1)-2, -1, -1):
initial = self.manyToManyAdd(initial, self.oneToManyMultiply(num1[i], num2), start)
start += 1
return ''.join([str(char) for char in initial[::-1]])
def oneToManyMultiply(self, one, many):
res = []
for i in range(len(many)-1, -1, -1):
tmp = int(one)*int(many[i]) + res.pop() if res else int(one) * int(many[i])
res.append(tmp % 10)
res.append(tmp//10)
return res if res[-1] != 0 else res[:-1]
def manyToManyAdd(self, many1, many2, start):
n1, n2 = len(many1), len(many2)
res = many1[:start]
i, j = start, 0
carry = 0
while i < n1 or j < n2 or carry != 0:
numMany1 = many1[i] if i < n1 else 0
numMany2 = many2[j] if j < n2 else 0
tmp = numMany1 + numMany2 + carry
res.append(tmp%10)
carry = tmp // 10
i += 1
j += 1
return res
作者:yi-wen-statistics
链接:https://leetcode-cn.com/problems/multiply-strings/solution/bao-li-qiu-jie-by-yi-wen-statistics-5/
3. 复杂度分析
1. 题目描述
2. 思路&代码
回溯法
class Solution:
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def backtrack(first = 0):
# 所有数都填完了
if first == n:
res.append(nums[:])
for i in range(first, n):
# 动态维护数组
nums[first], nums[i] = nums[i], nums[first]
# 继续递归填下一个数
backtrack(first + 1)
# 撤销操作
nums[first], nums[i] = nums[i], nums[first]
n = len(nums)
res = []
backtrack()
return res
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/permutations/solution/quan-pai-lie-by-leetcode-solution-2/
3. 复杂度分析
1. 题目描述
2. 思路&代码
暴力求解
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
tmp = nums[0]
max_ = tmp
n = len(nums)
for i in range(1,n):
# 当当前序列加上此时的元素的值大于tmp的值,说明最大序列和可能出现在后续序列中,记录此时的最大值
if tmp + nums[i]>nums[i]:
max_ = max(max_, tmp+nums[i])
tmp = tmp + nums[i]
else:
#当tmp(当前和)小于下一个元素时,当前最长序列到此为止。以该元素为起点继续找最大子序列,
# 并记录此时的最大值
max_ = max(max_, tmp, tmp+nums[i], nums[i])
tmp = nums[i]
return max_
作者:pandawakaka
链接:https://leetcode-cn.com/problems/maximum-subarray/solution/bao-li-qiu-jie-by-pandawakaka/
分治
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
n = len(nums)
#递归终止条件
if n == 1:
return nums[0]
else:
#递归计算左半边最大子序和
max_left = self.maxSubArray(nums[0:len(nums) // 2])
#递归计算右半边最大子序和
max_right = self.maxSubArray(nums[len(nums) // 2:len(nums)])
#计算中间的最大子序和,从右到左计算左边的最大子序和,从左到右计算右边的最大子序和,再相加
max_l = nums[len(nums) // 2 - 1]
tmp = 0
for i in range(len(nums) // 2 - 1, -1, -1):
tmp += nums[i]
max_l = max(tmp, max_l)
max_r = nums[len(nums) // 2]
tmp = 0
for i in range(len(nums) // 2, len(nums)):
tmp += nums[i]
max_r = max(tmp, max_r)
#返回三个中的最大值
return max(max_right,max_left,max_l+max_r)
作者:pandawakaka
链接:https://leetcode-cn.com/problems/maximum-subarray/solution/bao-li-qiu-jie-by-pandawakaka/