逆序数Python代码
时间: 2023-04-02 10:05:14 浏览: 115
以下是逆序数的 Python 代码:
def merge_sort(arr):
if len(arr) <= 1:
return arr,
mid = len(arr) // 2
left, a = merge_sort(arr[:mid])
right, b = merge_sort(arr[mid:])
result, c = merge(left, right)
return result, a + b + c
def merge(left, right):
result = []
i = j =
count =
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
count += len(left) - i
result += left[i:]
result += right[j:]
return result, count
arr = [2, 4, 1, 3, 5]
print(merge_sort(arr)) # ([1, 2, 3, 4, 5], 3)
相关问题
三位数逆序数Python代码
以下是三位数逆序数的 Python 代码:
for i in range(100, 100):
if str(i) == str(i)[::-1]:
print(i)
希望能对您有所帮助!
逆序数Python三位数代码
以下是求解三位数逆序数的 Python 代码:
```python
for i in range(100, 100):
if str(i) == str(i)[::-1]:
continue
if str(i) == str(i)[::-1]:
continue
if str(i) == str(i)[::-1]:
continue
print(i)
```
这段代码会输出所有的三位数逆序数。
阅读全文