添加了python版本代码

为数据结构和算法文件夹下的代码增加了python语言版本
pull/28/head
goodyong 2021-07-05 22:25:41 +08:00
parent a503e97f11
commit 4e661354d4
16 changed files with 1088 additions and 83 deletions

View File

@ -68,6 +68,8 @@
####
Java Code:
```java
class Solution {
public int strStr(String haystack, String needle) {
@ -101,10 +103,38 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def strStr(self, haystack: str, needle: str)->int:
haylen = len(haystack)
needlen = len(needle)
#
if haylen < needlen:
return -1
if needlen == 0:
return 0
#
for i in range(0, haylen - needlen + 1):
#
j = 0
while j < needlen:
if haystack[i + j] != needle[j]:
break
j += 1
#
if j == needlen:
return i
return -1
```
BF退
Java Code:
```java
class Solution {
public int strStr(String haystack, String needle) {
@ -132,3 +162,29 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def strStr(self, haystack: str, needle: str)->int:
# ij
i = 0
j = 0
#
halen = len(haystack)
nelen = len(needle)
# i
while i < halen and j < nelen:
# j
if haystack[i] == needle[j]:
j += 1
else:
# j i
i -= j
j = 0
i += 1
# -1
renum = i - nelen if j == nelen else -1
return renum
```

View File

@ -122,6 +122,8 @@ BM 算法是从右往左进行比较,发现坏字符的时候此时 cac 已
AC KMP
Java Code:
```java
class Solution {
public int strStr(String haystack, String needle) {
@ -215,6 +217,89 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def strStr(self, haystack: str, needle: str)->int:
haylen = len(haystack)
needlen = len(needle)
return self.bm(haystack, haylen, needle, needlen)
#
def badChar(self, b: str, m: int, bc: List[int]):
#
for i in range(0, 256):
bc[i] = -1
# m a
for i in range(0, m,):
ascii = ord(b[i])
bc[ascii] = i#
#
def goodSuffix(self, b: str, m: int, suffix: List[int], prefix: List[bool]):
#
for i in range(0, m):
suffix[i] = -1
prefix[i] = False
for i in range(0, m - 1):
j = i
k = 0
while j >= 0 and b[j] == b[m - 1 - k]:
j -= 1
k += 1
suffix[k] = j + 1
if j == -1:
prefix[k] = True
def bm(self, a: str, n: int, b: str, m: int)->int:
bc = [0] * 256#
self.badChar(b, m, bc)
#
suffix_index = [0] * m
# True
ispre = [False] * m
self.goodSuffix(b, m, suffix_index, ispre)
i = 0#
#
while i <= n - m:
#
j = m - 1
while j >= 0:
if a[i + j] != b[j]:
break
j -= 1
#
if j < 0:
return i
#
#
x = j - bc[ord(a[i + j])]
y = 0
# ,
if y < m - 1 and m - 1 - j > 0:
y = self.move(j, m, suffix_index, ispre)
#
i += max(x, y)
return -1
# j
def move(j: int, m: int, suffix_index: List[int], ispre: List[bool])->int:
#
k = m - 1 - j
# k
if suffix_index[k] != -1:
return j - suffix_index[k] + 1
#
for r in range(j + 2, m):
# //如果是头部
if ispre[m - r] == True:
return r
# m
return m
```
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/头缀函数.145da63ig3s0.png)

View File

@ -113,7 +113,7 @@ class Solution {
k = next[k];
}
// 相同情况,就是 k的下一位和 i 相同时,此时我们已经知道 [0,i-1]的最长前后缀
//然后 k - 1 又和 i 相同最长前后缀加1即可
//然后 k + 1 又和 i 相同最长前后缀加1即可
if (needle[k+1] == needle[i]) {
++k;
}
@ -125,5 +125,63 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def strStr(self, haystack: str, needle: str)->int:
#
if len(needle) == 0:
return 0
if len(haystack) == 0:
return -1
#
halen = len(haystack)
nelen = len(needle)
#
return self.kmp(haystack, halen, needle, nelen)
def kmp(self, hasyarr: str, halen: int, nearr: str, nelen: int)->int:
# next
next = self.next(nearr, nelen)
j = 0
for i in range(0, halen):
# next
# ,
while j > 0 and hasyarr[i] != nearr[j]:
j = next[j - 1] + 1
#
if nelen - j + i > halen:
return -1
#
if hasyarr[i] == nearr[j]:
j += 1
#
if j == nelen:
return i - nelen + 1
return -1
#
#
def next(self, needle: str, len:int)->List[int]:
# next
next = [0] * len
#
next[0] = -1
k = -1
for i in range(1, len):
# [0,i-1]k+1i
# next[k]
# k+1next,next[k+1]
while k != -1 and needle[k + 1] != needle[i]:
k = next[k]
# k i [0,i-1]
# k + 1 i 1
if needle[k + 1] == needle[i]:
k += 1
next[i] = k
return next
```

View File

@ -80,6 +80,8 @@
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
@ -102,6 +104,25 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def sortArray(self, nums: List[int])->List[int]:
leng = len(nums)
for i in range(0, leng):
for j in range(i + 1, leng):
if nums[i] > nums[j]:
self.swap(nums, i, j)
return nums
def swap(self, nums: List[int], i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```
nums[i] nums[j] nums[i] > nums[j] nums[0]
****
@ -143,6 +164,8 @@ class Solution {
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
@ -172,6 +195,32 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def sortArray(self, nums: List[int])->List[int]:
leng = len(nums)
#
flag = True
for i in range(0, leng):
if not flag:
break
flag = False
for j in range(0, leng - i - 1):
if nums[j] > nums[j + 1]:
self.swap(nums, j, j + 1)
# true
flag = True
return nums
def swap(self, nums: List[int], i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```
****

View File

@ -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
```

View File

@ -3,7 +3,7 @@
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
@ -99,8 +99,10 @@
Java Code:
```java
public void swim (int index) {
public void swim (int[] nums, int index) {
while (index > 1 && nums[index/2] > nums[index]) {
swap(index/2,index);//交换
index = index/2;
@ -108,6 +110,15 @@ public void swim (int index) {
}
```
Python Code:
```python
def swim(nums: int, index: int):
while index > 1 and nums[int(index/2)] > nums[index]:
swap(int(index/2), index)#
index = int(index/2)
```
@ -145,6 +156,8 @@ public void swim (int index) {
Java Code:
```java
public void sink (int[] nums, int index,int len) {
while (true) {
@ -165,6 +178,24 @@ public void sink (int[] nums, int index,int len) {
}
```
Python Code:
```python
def sink(nums: list, index: int, len: int):
while True:
#
j = 2 * index
if j < len-1 and nums[j] < nums[j+1]:
j += 1
#
if j < len and nums[index] < nums[j]:
swap(nums, index, j)
else:
break
#
index = j
```
@ -187,6 +218,8 @@ public void sink (int[] nums, int index,int len) {
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
@ -238,6 +271,44 @@ class Solution {
}
```
Python Code:
```python
def sortArray(nums: list)->list:
leng = len(nums)
a = [0] + nums
#
for i in range(int(leng / 2), 0, -1):
sink(a, i, leng)
k = leng
#
while k > 1:
swap(a, 1, k)
k -= 1
sink(a, 1, k)
for i in range(1, leng + 1):
nums[i - 1] = a[i]
return nums
def swap(nums: list, i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
def sink(nums: list, k: int, end: int):
while 2 * k <= end:
j = 2 * k
if j + 1 <= end and nums[j + 1] > nums[j]:
j += 1
if nums[j] > nums[k]:
swap(nums, j, k)
else:
break
k = j
```
广

View File

@ -70,6 +70,8 @@
####
Java Code:
```java
class Solution {
public int strStr(String haystack, String needle) {
@ -103,10 +105,37 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def strStr(self, haystack: str, needle: str)->int:
haylen = len(haystack)
needlen = len(needle)
#
if haylen < needlen:
return -1
if needlen == 0:
return 0
#
for i in range(0, haylen - needlen + 1):
#
j = 0
while j < needlen:
if haystack[i + j] != needle[j]:
break
j += 1
#
if j == needlen:
return i
return -1
```
BF退
Java Code:
```java
class Solution {
public int strStr(String haystack, String needle) {
@ -134,6 +163,32 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def strStr(self, haystack: str, needle: str)->int:
# ij
i = 0
j = 0
#
halen = len(haystack)
nelen = len(needle)
# i
while i < halen and j < nelen:
# j
if haystack[i] == needle[j]:
j += 1
else:
# j i
i -= j
j = 0
i += 1
# -1
renum = i - nelen if j == nelen else -1
return renum
```
## BM(Boyer-Moore)
@ -262,6 +317,8 @@ BM 算法是从右往左进行比较,发现坏字符的时候此时 cac 已
AC KMP
Java Code:
```java
class Solution {
public int strStr(String haystack, String needle) {
@ -355,6 +412,89 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def strStr(self, haystack: str, needle: str)->int:
haylen = len(haystack)
needlen = len(needle)
return self.bm(haystack, haylen, needle, needlen)
#
def badChar(self, b: str, m: int, bc: List[int]):
#
for i in range(0, 256):
bc[i] = -1
# m a
for i in range(0, m,):
ascii = ord(b[i])
bc[ascii] = i#
#
def goodSuffix(self, b: str, m: int, suffix: List[int], prefix: List[bool]):
#
for i in range(0, m):
suffix[i] = -1
prefix[i] = False
for i in range(0, m - 1):
j = i
k = 0
while j >= 0 and b[j] == b[m - 1 - k]:
j -= 1
k += 1
suffix[k] = j + 1
if j == -1:
prefix[k] = True
def bm(self, a: str, n: int, b: str, m: int)->int:
bc = [0] * 256#
self.badChar(b, m, bc)
#
suffix_index = [0] * m
# True
ispre = [False] * m
self.goodSuffix(b, m, suffix_index, ispre)
i = 0#
#
while i <= n - m:
#
j = m - 1
while j >= 0:
if a[i + j] != b[j]:
break
j -= 1
#
if j < 0:
return i
#
#
x = j - bc[ord(a[i + j])]
y = 0
# ,
if y < m - 1 and m - 1 - j > 0:
y = self.move(j, m, suffix_index, ispre)
#
i += max(x, y)
return -1
# j
def move(j: int, m: int, suffix_index: List[int], ispre: List[bool])->int:
#
k = m - 1 - j
# k
if suffix_index[k] != -1:
return j - suffix_index[k] + 1
#
for r in range(j + 2, m):
# //如果是头部
if ispre[m - r] == True:
return r
# m
return m
```
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/头缀函数.145da63ig3s0.png)
@ -413,6 +553,8 @@ next 数组存的咱们最长公共前后缀中,前缀的结尾字符下标。
** next **
Java Code:
```java
class Solution {
public int strStr(String haystack, String needle) {
@ -486,5 +628,64 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def strStr(self, haystack: str, needle: str)->int:
#
if len(needle) == 0:
return 0
if len(haystack) == 0:
return -1
#
halen = len(haystack)
nelen = len(needle)
#
return self.kmp(haystack, halen, needle, nelen)
def kmp(self, hasyarr: str, halen: int, nearr: str, nelen: int)->int:
# next
next = self.next(nearr, nelen)
j = 0
for i in range(0, halen):
# next
# ,
while j > 0 and hasyarr[i] != nearr[j]:
j = next[j - 1] + 1
#
if nelen - j + i > halen:
return -1
#
if hasyarr[i] == nearr[j]:
j += 1
#
if j == nelen:
return i - nelen + 1
return -1
#
#
def next(self, needle: str, len:int)->List[int]:
# next
next = [0] * len
#
next[0] = -1
k = -1
for i in range(1, len):
# [0,i-1]k+1i
# next[k]
# k+1next,next[k+1]
while k != -1 and needle[k + 1] != needle[i]:
k = next[k]
# k i [0,i-1]
# k - 1 i 1
if needle[k + 1] == needle[i]:
k += 1
next[i] = k
return next
```
[](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/qrcode_for_gh_1f36d2ef6df9_258.5lojyphpkso0.jpg)

View File

@ -28,6 +28,8 @@
****
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
@ -58,6 +60,34 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def sortArray(self, nums: List[int])->List[int]:
increment = len(nums)
#
while increment > 1:
#
increment = int(increment / 2)
#
for i in range(0, increment):
#
for j in range(i + increment, len(nums), increment):
temp = nums[j]
k = j - increment
while k >= 0:
if temp < nums[k]:
nums[k + increment] = nums[k]
k -= increment
continue
break
nums[k + increment] = temp
return nums
```
使
![](https://cdn.jsdelivr.net/gh/tan45du/bedphoto2@master/20210122/微信截图_20210127212901.62c3o3ss6pg0.png)

View File

@ -54,6 +54,8 @@
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
@ -90,6 +92,48 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def sortArray(self, nums: List[int])->List[int]:
self.mergeSort(nums, 0, len(nums) - 1)
return nums
def mergeSort(self, arr: List[int], left: int, right: int):
if left < right:
mid = left + ((right - left) >> 1)
self.mergeSort(arr, left, mid)
self.mergeSort(arr, mid + 1, right)
self.merge(arr, left, mid, right)
#
def merge(self, arr: List[int], left: int, mid: int, right: int):
#
temparr = [0] * (right - left + 1)
temp1 = left
temp2 = mid + 1
index = 0
#
while temp1 <= mid and temp2 <= right:
if arr[temp1] <= arr[temp2]:
temparr[index] = arr[temp1]
index += 1
temp1 += 1
else:
temparr[index] = arr[temp2]
index += 1
temp2 += 1
#
if temp1 <= mid:
temparr[index: index + mid - temp1 + 1] = arr[temp1: temp1 + mid - temp1 + 1]
if temp2 <= right:
temparr[index: index + right - temp2 + 1] = arr[temp2: temp2 + right - temp2 + 1]
#
arr[left: left + right- left + 1] = temparr[0: right - left + 1]
```
****
O(n) logn, O(nlogn) O(nlogn) 广 O(1)
@ -134,6 +178,8 @@ class Solution {
merge
Java Code:
```java
class Solution {
public int[] sortArray (int[] nums) {
@ -162,7 +208,7 @@ class Solution {
}
public void merge (int[] arr,int left, int mid, int right) {
//第一步,定义一个新的临时数组
int[] temparr = new int[right -left + 1];
int[] temparr = new int[right - left + 1];
int temp1 = left, temp2 = mid + 1;
int index = 0;
//对应第二步,比较每个指针指向的值,小的存入大集合
@ -182,3 +228,55 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def sortArray(self, nums: List[int])->List[int]:
# 124816.....
k = 1
leng = len(nums)
while k < leng:
self.mergePass(nums, k, leng)
k *= 2
print(nums)
return nums
def mergePass(self, array: List[int], k: int, leng: int):
i = 0
while i < leng - 2 * k:
#
self.merge(array, i, i + k - 1, i + 2 * k - 1)
i += 2 * k
#
if i + k < leng:
self.merge(array, i, i + k - 1, leng - 1)
#
def merge(self, arr: List[int], left: int, mid: int, right: int):
#
temparr = [0] * (right - left + 1)
temp1 = left
temp2 = mid + 1
index = 0
#
while temp1 <= mid and temp2 <= right:
if arr[temp1] <= arr[temp2]:
temparr[index] = arr[temp1]
index += 1
temp1 += 1
else:
temparr[index] = arr[temp2]
index += 1
temp2 += 1
#
if temp1 <= mid:
temparr[index: index + mid - temp1 + 1] = arr[temp1: temp1 + mid - temp1 + 1]
if temp2 <= right:
temparr[index: index + right - temp2 + 1] = arr[temp2: temp2 + right - temp2 + 1]
#
arr[left: left + right- left + 1] = temparr[0: right - left + 1]
```

View File

@ -36,7 +36,7 @@
nums[hight] hight hight low
nums[high] high high low
@ -46,6 +46,8 @@
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
@ -54,30 +56,30 @@ class Solution {
return nums;
}
public void quickSort (int[] nums, int low, int hight) {
public void quickSort (int[] nums, int low, int high) {
if (low < hight) {
int index = partition(nums,low,hight);
if (low < high) {
int index = partition(nums,low,high);
quickSort(nums,low,index-1);
quickSort(nums,index+1,hight);
quickSort(nums,index+1,high);
}
}
public int partition (int[] nums, int low, int hight) {
public int partition (int[] nums, int low, int high) {
int pivot = nums[low];
while (low < hight) {
while (low < high) {
//移动hight指针
while (low < hight && nums[hight] >= pivot) {
hight--;
while (low < high && nums[high] >= pivot) {
high--;
}
//填坑
if (low < hight) nums[low] = nums[hight];
while (low < hight && nums[low] <= pivot) {
if (low < high) nums[low] = nums[high];
while (low < high && nums[low] <= pivot) {
low++;
}
//填坑
if (low < hight) nums[hight] = nums[low];
if (low < high) nums[high] = nums[low];
}
//基准数放到合适的位置
nums[low] = pivot;
@ -107,26 +109,26 @@ class Solution {
}
public void quickSort (int[] nums, int low, int hight) {
public void quickSort (int[] nums, int low, int high) {
if (low < hight) {
int index = partition(nums,low,hight);
if (low < high) {
int index = partition(nums,low,high);
quickSort(nums,low,index-1);
quickSort(nums,index+1,hight);
quickSort(nums,index+1,high);
}
}
public int partition (int[] nums, int low, int hight) {
public int partition (int[] nums, int low, int high) {
int pivot = nums[low];
int start = low;
while (low < hight) {
while (low < hight && nums[hight] >= pivot) hight--;
while (low < hight && nums[low] <= pivot) low++;
if (low >= hight) break;
swap(nums, low, hight);
while (low < high) {
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[low] <= pivot) low++;
if (low >= high) break;
swap(nums, low, high);
}
//基准值归位
swap(nums,start,low);
@ -175,29 +177,29 @@ class Solution {
stack.push(0);
while (!stack.isEmpty()) {
int low = stack.pop();
int hight = stack.pop();
int high = stack.pop();
if (low < hight) {
int index = partition(nums, low, hight);
if (low < high) {
int index = partition(nums, low, high);
stack.push(index - 1);
stack.push(low);
stack.push(hight);
stack.push(high);
stack.push(index + 1);
}
}
return nums;
}
public int partition (int[] nums, int low, int hight) {
public int partition (int[] nums, int low, int high) {
int pivot = nums[low];
int start = low;
while (low < hight) {
while (low < high) {
while (low < hight && nums[hight] >= pivot) hight--;
while (low < hight && nums[low] <= pivot) low++;
if (low >= hight) break;
swap(nums, low, hight);
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[low] <= pivot) low++;
if (low >= high) break;
swap(nums, low, high);
}
swap(nums,start,low);
return low;
@ -226,13 +228,13 @@ class Solution {
```java
int mid = low + ((hight-low) >> 1);
if (nums[low] > nums[hight]) swap(nums,low,hight);
if (nums[mid] > nums[hight]) swap(nums,mid,hight);
int mid = low + ((high-low) >> 1);
if (nums[low] > nums[high]) swap(nums,low,high);
if (nums[mid] > nums[high]) swap(nums,mid,high);
if (nums[mid] > nums[low]) swap(nums,mid,low);
```
nums[low] nums[hight], nums[mid], [4,2,3] [3,2,4]. 3
nums[low] nums[high], nums[mid], [4,2,3] [3,2,4]. 3
****
@ -242,28 +244,28 @@ class Solution {
quickSort(nums,0,nums.length-1);
return nums;
}
public void quickSort (int[] nums, int low, int hight) {
if (low < hight) {
int index = partition(nums,low,hight);
public void quickSort (int[] nums, int low, int high) {
if (low < high) {
int index = partition(nums,low,high);
quickSort(nums,low,index-1);
quickSort(nums,index+1,hight);
quickSort(nums,index+1,high);
}
}
public int partition (int[] nums, int low, int hight) {
public int partition (int[] nums, int low, int high) {
//三数取中,大家也可以使用其他方法
int mid = low + ((hight-low) >> 1);
if (nums[low] > nums[hight]) swap(nums,low,hight);
if (nums[mid] > nums[hight]) swap(nums,mid,hight);
int mid = low + ((high-low) >> 1);
if (nums[low] > nums[high]) swap(nums,low,high);
if (nums[mid] > nums[high]) swap(nums,mid,high);
if (nums[mid] > nums[low]) swap(nums,mid,low);
//下面和之前一样,仅仅是多了上面几行代码
int pivot = nums[low];
int start = low;
while (low < hight) {
while (low < hight && nums[hight] >= pivot) hight--;
while (low < hight && nums[low] <= pivot) low++;
if (low >= hight) break;
swap(nums, low, hight);
while (low < high) {
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[low] <= pivot) low++;
if (low >= high) break;
swap(nums, low, high);
}
swap(nums,start,low);
return low;
@ -291,38 +293,38 @@ class Solution {
return nums;
}
public void quickSort (int[] nums, int low, int hight) {
public void quickSort (int[] nums, int low, int high) {
if (hight - low <= INSERTION_SORT_MAX_LENGTH) {
insertSort(nums,low,hight);
if (high - low <= INSERTION_SORT_MAX_LENGTH) {
insertSort(nums,low,high);
return;
}
int index = partition(nums,low,hight);
int index = partition(nums,low,high);
quickSort(nums,low,index-1);
quickSort(nums,index+1,hight);
quickSort(nums,index+1,high);
}
public int partition (int[] nums, int low, int hight) {
public int partition (int[] nums, int low, int high) {
//三数取中,大家也可以使用其他方法
int mid = low + ((hight-low) >> 1);
if (nums[low] > nums[hight]) swap(nums,low,hight);
if (nums[mid] > nums[hight]) swap(nums,mid,hight);
int mid = low + ((high-low) >> 1);
if (nums[low] > nums[high]) swap(nums,low,high);
if (nums[mid] > nums[high]) swap(nums,mid,high);
if (nums[mid] > nums[low]) swap(nums,mid,low);
int pivot = nums[low];
int start = low;
while (low < hight) {
while (low < hight && nums[hight] >= pivot) hight--;
while (low < hight && nums[low] <= pivot) low++;
if (low >= hight) break;
swap(nums, low, hight);
while (low < high) {
while (low < high && nums[high] >= pivot) high--;
while (low < high && nums[low] <= pivot) low++;
if (low >= high) break;
swap(nums, low, high);
}
swap(nums,start,low);
return low;
}
public void insertSort (int[] nums, int low, int hight) {
public void insertSort (int[] nums, int low, int high) {
for (int i = low+1; i <= hight; ++i) {
for (int i = low+1; i <= high; ++i) {
int temp = nums[i];
int j;
for (j = i-1; j >= 0; --j) {
@ -366,6 +368,8 @@ class Solution {
**++**
Java Code:
```java
class Solution {
private static final int INSERTION_SORT_MAX_LENGTH = 7;
@ -374,19 +378,19 @@ class Solution {
return nums;
}
public void quickSort(int nums[], int low, int hight) {
public void quickSort(int nums[], int low, int high) {
//插入排序
if (hight - low <= INSERTION_SORT_MAX_LENGTH) {
insertSort(nums,low,hight);
if (high - low <= INSERTION_SORT_MAX_LENGTH) {
insertSort(nums,low,high);
return;
}
//三数取中
int mid = low + ((hight-low) >> 1);
if (nums[low] > nums[hight]) swap(nums,low,hight);
if (nums[mid] > nums[hight]) swap(nums,mid,hight);
int mid = low + ((high-low) >> 1);
if (nums[low] > nums[high]) swap(nums,low,high);
if (nums[mid] > nums[high]) swap(nums,mid,high);
if (nums[mid] > nums[low]) swap(nums,mid,low);
//三向切分
int left = low, i = low + 1, right = hight;
int left = low, i = low + 1, right = high;
int pvoit = nums[low];
while (i <= right) {
if (pvoit < nums[i]) {
@ -401,11 +405,11 @@ class Solution {
}
}
quickSort(nums,low,left-1);
quickSort(nums,right+1,hight);
quickSort(nums,right+1,high);
}
public void insertSort (int[] nums, int low, int hight) {
public void insertSort (int[] nums, int low, int high) {
for (int i = low+1; i <= hight; ++i) {
for (int i = low+1; i <= high; ++i) {
int temp = nums[i];
int j;
for (j = i-1; j >= 0; --j) {
@ -426,5 +430,65 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
INSERTION_SORT_MAX_LENGTH = 7
def sortArray(self, nums: List[int])->List[int]:
self.quickSort(nums, 0, len(nums) - 1)
return nums
def quickSort(self, nums: List[int], low: int, high: int):
#
if high - low <= self.INSERTION_SORT_MAX_LENGTH:
self.insertSort(nums, low, high)
return
#
mid = low + ((high - low) >> 1)
if nums[low] > nums[high]:
self.swap(nums, low, high)
if nums[mid] > nums[high]:
self.swap(nums, mid, high)
if nums[mid] > nums[low]:
self. swap(nums, mid, low)
#
left = low
i = low + 1
right = high
pivot = nums[low]
while i <= right:
if pivot < nums[i]:
self.swap(nums, i, right)
right -= 1
elif pivot == nums[i]:
i += 1
else:
self.swap(nums, left, i)
left += 1
i += 1
self.quickSort(nums, low, left - 1)
self.quickSort(nums, right + 1, high)
def insertSort(self, nums: List[int], low: int, high: int):
for i in range(low + 1, high + 1):
temp = nums[i]
j = i - 1
while j >= 0:
if temp < nums[j]:
nums[j + 1] = nums[j]
j -= 1
continue
break
nums[j + 1] = temp
def swap(self, nums: List[int], i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```

View File

@ -22,6 +22,8 @@
****
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
@ -48,6 +50,31 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def sortArray(self, nums: List[int])->List[int]:
# i 1
for i in range(1, len(nums)):
#
temp = nums[i]
#
j = i - 1
while j >= 0:
#
if temp < nums[j]:
nums[j + 1] = nums[j]
j -= 1
continue
#
break
#
nums[j + 1] = temp
return nums
```
****
O(n)

View File

@ -22,6 +22,8 @@
****
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
@ -46,6 +48,29 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def sortArray(self, nums: List[int])->List[int]:
leng = len(nums)
min = 0
for i in range(0, leng):
min = i
#
for j in range(i + 1, leng):
if nums[min] > nums[j]:
min = j
if min != i:
self.swap(nums, i, min)
return nums
def swap(self, nums: List[int], i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```
****

View File

@ -40,6 +40,8 @@
Java Code:
```java
class Solution {
private int count;
@ -95,3 +97,56 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
count = 0
def reversePairs(self, nums: List[int])->int:
self.count = 0
self.merge(nums, 0, len(nums) - 1)
return self.count
def merge(self, nums: List[int], left: int, right: int):
if left < right:
mid = left + ((right - left) >> 1)
self.merge(nums, left, mid)
self.merge(nums, mid + 1, right)
self.mergeSort(nums, left, mid, right)
def mergeSort(self, nums: List[int], left: int, mid: int, right: int):
temparr = [0] * (right - left + 1)
temp1 = left
temp2 = mid + 1
index = 0
while temp1 <= mid and temp2 <= right:
#
if nums[temp1] > 2 * nums[temp2]:
self.count += mid - temp1 + 1
temp2 += 1
else:
temp1 += 1
# 使
temp1 = left
temp2 = mid + 1
#
while temp1 <= mid and temp2 <= right:
if nums[temp1] <= nums[temp2]:
temparr[index] = nums[temp1]
index += 1
temp1 += 1
else:
temparr[index] = nums[temp2]
index += 1
temp2 += 1
#
if temp1 <= mid:
temparr[index: index + mid - temp1 + 1] = nums[temp1: temp1 + mid - temp1 + 1]
if temp2 <= right:
temparr[index: index + right - temp2 + 1] = nums[temp2: temp2 + right - temp2 + 1]
nums[left: left + right- left + 1] = temparr[0: right - left + 1]
```

View File

@ -132,6 +132,33 @@ public:
};
```
Python Code:
```python
from typing import List
class Solution:
def sortColors(self, nums: List[int]):
leng = len(nums)
left = 0
#
i = left
right = leng - 1
while i <= right:
if nums[i] == 2:
self.swap(nums, i, right)
right -= 1
elif nums[i] == 0:
self.swap(nums, i, left)
i += 1
left += 1
else:
i += 1
def swap(self, nums: List[int], i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```
使
[0,0,0,1,1,1,2,2,2]
@ -216,5 +243,29 @@ public:
};
```
```python
from typing import List
class Solution:
def sortColors(self, nums: List[int]):
left = 0
leng = len(nums)
right = leng - 1
for i in range(0, right + 1):
if nums[i] == 0:
self.swap(nums, i, left)
left += 1
if nums[i] == 2:
swap(nums, i, right)
right -= 1
# 1 i i--
if nums[i] != 1:
i -= 1
def swap(nums: List[int], i: int, j: int):
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```

View File

@ -128,6 +128,8 @@ temp[8] = 5。然后再将 presum[5] 减 1 。
Java Code:
```java
class Solution {
public int[] sortArray(int[] nums) {
@ -169,6 +171,44 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
def sortArray(self,nums: List[int])->List[int]:
leng = len(nums)
if leng < 1:
return nums
#
max = nums[0]
min = nums[0]
for x in nums:
if max < x:
max = x
if min > x:
min = x
# presum ,
#
presum = [0] * (max - min + 1)
for x in nums:
presum[x - min] += 1
for i in range(1, len(presum)):
presum[i] = presum[i - 1] + presum[i]
#
temp = [0] * leng
#
for i in range(leng - 1, -1, -1):
# presum
index = presum[nums[i] - min] - 1
temp[index] = nums[i]
#
presum[nums[i] - min] -= 1
# copy
nums = temp
return nums
```
****

View File

@ -35,6 +35,8 @@
****
Java Code:
```java
class Solution {
//全局变量
@ -80,7 +82,51 @@ class Solution {
}
```
Python Code:
```python
from typing import List
class Solution:
count = 0
def reversePairs(self, nums: List[int])->int:
self.count = 0
self.mergeSort(nums, 0, len(nums) - 1)
return self.count
def mergeSort(self, arr: List[int], left: int, right: int):
if left < right:
mid = left + ((right - left) >> 1)
self.mergeSort(arr, left, mid)
self.mergeSort(arr, mid + 1, right)
self.merge(arr, left, mid, right)
#
def merge(self, arr: List[int], left: int, mid: int, right: int):
#
temparr = [0] * (right - left + 1)
temp1 = left
temp2 = mid + 1
index = 0
#
while temp1 <= mid and temp2 <= right:
if arr[temp1] <= arr[temp2]:
temparr[index] = arr[temp1]
index += 1
temp1 += 1
else:
self.count += (mid - temp1 + 1)
temparr[index] = arr[temp2]
index += 1
temp2 += 1
#
if temp1 <= mid:
temparr[index: index + mid - temp1 + 1] = arr[temp1: temp1 + mid - temp1 + 1]
if temp2 <= right:
temparr[index: index + right - temp2 + 1] = arr[temp2: temp2 + right - temp2 + 1]
#
arr[left: left + right- left + 1] = temparr[0: right - left + 1]
```
leetcode 912