归并排序
代码语言:javascript
复制def merge(le, ri):
res = []
i = j = 0
while i < len(le) and j < len(ri):
if le[i] < ri[j]:
res.append(le[i])
i = 1
else:
res.append(ri[j])
j = 1
res = res le[i:] ri[j:]
return res
def mergeSort(nums):
if len(nums) <= 1:
return nums
mid = len(nums) // 2
left = mergeSort(nums[:mid])
right = mergeSort(nums[mid:])
res = merge(left, right)
return res
nums = [6,2,1,5,4,3,2,2,7]
res = mergeSort(nums)
print(res)
leetcode 241 为运算表达式设计优先级
代码语言:javascript
复制class Solution:
def diffWaysToCompute(self, input: str) -> List[int]:
res = []
n = len(input)
for i in range(n):
c = input[i]
if c == ' ' or c == '-' or c == '*':
left = self.diffWaysToCompute(input[:i])
right = self.diffWaysToCompute(input[i 1:])
for a in left:
for b in right:
if c == ' ':
res.append(a b)
elif c == '-':
res.append(a - b)
elif c == '*':
res.append(a * b)
if len(res) == 0:
res.append(int(input))
return res