Skip to content

Commit

Permalink
✨ feat: 找出分区值 python3实现
Browse files Browse the repository at this point in the history
  • Loading branch information
王洋 committed Jul 26, 2024
1 parent 18d97e7 commit adbe5df
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
4 changes: 2 additions & 2 deletions problemset/excel-sheet-column-number/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def titleToNumber(self, columnTitle: str) -> int:
res = res + (c -_A) * math.pow(26, i)
return int(res)

# s = Solution()
# print(s.titleToNumber('A'))
s = Solution()
print(s.titleToNumber('A'))
# print(s.titleToNumber('AB'))
# print(s.titleToNumber('ZY'))
27 changes: 27 additions & 0 deletions problemset/find-the-value-of-the-partition/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 从utilsPython/array.py导入quick_sort
# from utilsPython.array import quick_sort
from typing import List

def quick_sort(arr: List[int]) -> List[int]:
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)

class Solution:
def findValueOfPartition(self, nums: List[int]) -> int:
sortedNums = quick_sort(nums)
n = len(sortedNums)
res = -1
for i in range(0, n-1):
x = sortedNums[i + 1] - sortedNums[i]
if res == -1 or x < res:
res = x
return res

s = Solution()
print(s.findValueOfPartition([1,3,2,4])) # 1
print(s.findValueOfPartition([100,1,10])) # 9
101 changes: 101 additions & 0 deletions utilsPython/array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# array.py

from typing import List

def quick_sort(arr: List[int]) -> List[int]:
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)

def merge_sort(arr: List[int]) -> List[int]:
if len(arr) <= 1:
return arr
middle = len(arr) // 2
left = merge_sort(arr[:middle])
right = merge_sort(arr[middle:])
return merge(left, right)

def merge(left: List[int], right: List[int]) -> List[int]:
result = []
while left and right:
if left[0] < right[0]:
result.append(left.pop(0))
else:
result.append(right.pop(0))
result.extend(left if left else right)
return result

def heap_sort(arr: List[int]) -> List[int]:
def heapify(arr: List[int], n: int, i: int):
largest = i
left = 2 * i + 1
right = 2 * i + 2

if left < n and arr[left] > arr[largest]:
largest = left

if right < n and arr[right] > arr[largest]:
largest = right

if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)

n = len(arr)
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)

for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)

return arr

def shell_sort(arr: List[int]) -> List[int]:
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2
return arr

def insertion_sort(arr: List[int]) -> List[int]:
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr

def selection_sort(arr: List[int]) -> List[int]:
for i in range(len(arr)):
min_idx = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr

def bubble_sort(arr: List[int]) -> List[int]:
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
break
return arr

0 comments on commit adbe5df

Please sign in to comment.