mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-03 22:41:58 +00:00
添加了python版本代码
为数据结构和算法文件夹下的代码增加了python语言版本
This commit is contained in:
@@ -144,6 +144,8 @@ BC < CB 整理得 B / (10 ^ b - 1) < C / (10 ^ c - 1);
|
||||
|
||||
好啦,我们证明我们定义的规则有效下面我们直接看代码吧。继续使用我们的三向切分来解决
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public String minNumber(int[] nums) {
|
||||
@@ -166,23 +168,23 @@ class Solution {
|
||||
return;
|
||||
}
|
||||
int low = left;
|
||||
int hight = right;
|
||||
int high = right;
|
||||
int i = low+1;
|
||||
String pivot = arr[low];
|
||||
|
||||
while (i <= hight) {
|
||||
while (i <= high) {
|
||||
//比较大小
|
||||
if ((pivot+arr[i]).compareTo(arr[i]+pivot) > 0 ) {
|
||||
swap(arr,i++,low++);
|
||||
} else if ((pivot+arr[i]).compareTo(arr[i]+pivot) < 0) {
|
||||
swap(arr,i,hight--);
|
||||
swap(arr,i,high--);
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
quickSort(arr,left,low-1);
|
||||
quickSort(arr,hight+1,right);
|
||||
quickSort(arr,high+1,right);
|
||||
|
||||
}
|
||||
public void swap(String[] arr, int i, int j) {
|
||||
@@ -193,6 +195,53 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python Code:
|
||||
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def minNumber(self, nums: List[int])->str:
|
||||
|
||||
arr = [''] * len(nums)
|
||||
# 解决大数问题,将数字转换为字符串
|
||||
for i in range(0, len(nums)):
|
||||
arr[i] = str(nums[i])
|
||||
|
||||
self.quickSort(arr, 0, len(arr) - 1)
|
||||
s = ''
|
||||
for x in arr:
|
||||
s += x
|
||||
return s
|
||||
|
||||
def quickSort(self, arr: List[str], left: int, right: int):
|
||||
if left >= right:
|
||||
return
|
||||
low = left
|
||||
high = right
|
||||
i = low + 1
|
||||
pivot = arr[low]
|
||||
|
||||
while i <= high:
|
||||
# 比较大小
|
||||
if int(pivot + arr[i]) > int(arr[i] + pivot):
|
||||
self.swap(arr, i, low)
|
||||
i += 1
|
||||
low += 1
|
||||
elif int(pivot + arr[i]) < int(arr[i] + pivot):
|
||||
self.swap(arr, i, high)
|
||||
high -= 1
|
||||
else:
|
||||
i += 1
|
||||
|
||||
self.quickSort(arr, left, low - 1)
|
||||
self.quickSort(arr, high + 1, right)
|
||||
|
||||
def swap(self, arr: List[str], i: int, j: int):
|
||||
temp = arr[i]
|
||||
arr[i] = arr[j]
|
||||
arr[j] = temp
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user