diff --git a/problemset/excel-sheet-column-number/index.py b/problemset/excel-sheet-column-number/index.py index ffc15e8..8431a45 100644 --- a/problemset/excel-sheet-column-number/index.py +++ b/problemset/excel-sheet-column-number/index.py @@ -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')) diff --git a/problemset/find-the-value-of-the-partition/index.py b/problemset/find-the-value-of-the-partition/index.py new file mode 100644 index 0000000..1115e87 --- /dev/null +++ b/problemset/find-the-value-of-the-partition/index.py @@ -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 \ No newline at end of file diff --git a/utilsPython/array.py b/utilsPython/array.py new file mode 100644 index 0000000..3cfab1a --- /dev/null +++ b/utilsPython/array.py @@ -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