43、字符串相乘
class Solution:
def multiply(self, num1: str, num2: str) -> str:
num1_len = len(num1)
num2_len = len(num2)
res = [0] * (num1_len + num2_len)
for i in range(num1_len-1,-1,-1):
for j in range(num2_len-1,-1,-1):
tmp = int(num1[i]) * int(num2[j]) + int(res[i+j+1])
res[i+j+1] = tmp%10 # 余数作为当前位
res[i+j] = res[i+j] + tmp//10 # 前一位加上,进位(商作为进位)
res = list(map(str, res))
# print(res)
for i in range(num1_len+num2_len):
print(i)
if res[i]!='0': # 找到第一个非0数字,后面就是结果
return ''.join(res[i:])
return '0'
46、全排列
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
ans = []
l = []
def backtrack(nums):
if not nums:
ans.append(l[:])
return
for num in nums:
l.append(num)
nums_temp = nums[:]
nums_temp.remove(num)
backtrack(nums_temp)
l.pop()
backtrack(nums)
return ans
53、最大子序和
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
return max(itertools.accumulate(nums,lambda a,b:b+max(a,0)))