diff --git a/QuickSort.py b/QuickSort.py index cd8a673..59b0972 100644 --- a/QuickSort.py +++ b/QuickSort.py @@ -1,3 +1,5 @@ +# coding: utf-8 + def quickSort(alist): quickSortHelper(alist, 0, len(alist)-1) @@ -16,9 +18,9 @@ def partition(alist, first, last): done = False while not done: - while alist[leftmark] <= pivotvlue and leftmark <= rightmark: + while leftmark <= rightmark and alist[leftmark] <= pivotvlue: # bugfix: 先比较index, 不然数组会越界 leftmark += 1 - while alist[rightmark] >= pivotvlue and rightmark >= leftmark: + while rightmark >= leftmark and alist[rightmark] >= pivotvlue: rightmark -= 1 if leftmark > rightmark: @@ -32,3 +34,13 @@ def partition(alist, first, last): alist2 = [1] quickSort(alist2) print(alist2) + + +if __name__ == "__main__": + test_data = [3,2,111,3,-1,0,0,1,0,2,4] + + res_stable = sorted(test_data) + quickSort(test_data) + print(test_data) + print(res_stable) + assert all(map(lambda x: x[0] == x[1], zip(res_stable, test_data))) \ No newline at end of file diff --git "a/Target Offer/\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225.py" "b/Target Offer/\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225.py" index eef1246..0d8d072 100644 --- "a/Target Offer/\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225.py" +++ "b/Target Offer/\344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225.py" @@ -6,13 +6,24 @@ # 可能是python的的整型可以无限大的原因, 导致正数和负数的异或操作不断变成更小的负数而不会溢出 # 使用Swift尝试了一下, 还是可以求得正数和负数的位操作相加运算的 # -*- coding:utf-8 -*- +# class Solution: +# def Add(self, num1, num2): +# while num2: +# sum = num1 ^ num2 +# carry = (num1 & num2) << 1 +# num1 = sum +# num2 = carry +# return num1 +# s = Solution() +# print(s.Add(4, 2)) +# -*- coding:utf-8 -*- +# 通过每次对num1进行与操作保证是一个32位的整形 +# 因此最后我们可以判断符号位是否为1做处理 class Solution: def Add(self, num1, num2): - while num2: - sum = num1 ^ num2 - carry = (num1 & num2) << 1 - num1 = sum - num2 = carry - return num1 -s = Solution() -print(s.Add(4, 2)) + # write code here + while num2 != 0: + temp = num1 ^ num2 + num2 = (num1 & num2) << 1 + num1 = temp & 0xFFFFFFFF + return num1 if num1 >> 31 == 0 else num1 - 4294967296 diff --git "a/Target Offer/\344\272\214\347\273\264\346\225\260\347\273\204\346\237\245\346\211\276.py" "b/Target Offer/\344\272\214\347\273\264\346\225\260\347\273\204\346\237\245\346\211\276.py" index 3d05286..81239fd 100644 --- "a/Target Offer/\344\272\214\347\273\264\346\225\260\347\273\204\346\237\245\346\211\276.py" +++ "b/Target Offer/\344\272\214\347\273\264\346\225\260\347\273\204\346\237\245\346\211\276.py" @@ -29,6 +29,8 @@ def Find(self, array, target): # 判断非法输入 # 可以换成 isinstance(target, (int, float)) 进行判断 if type(target) == float and type(array[0][0]) == int: + if int(target) == target: + return False target = int(target) elif type(target) == int and type(array[0][0]) == float: target = float(int) diff --git "a/Target Offer/\346\233\277\346\215\242\347\251\272\346\240\274.py" "b/Target Offer/\346\233\277\346\215\242\347\251\272\346\240\274.py" index 72e50f2..bd15a41 100644 --- "a/Target Offer/\346\233\277\346\215\242\347\251\272\346\240\274.py" +++ "b/Target Offer/\346\233\277\346\215\242\347\251\272\346\240\274.py" @@ -6,6 +6,20 @@ # -*- coding:utf-8 -*- class Solution: # s 源字符串 + + # 使用append一次遍历即可替换 + # 由于list的append是O(1)的时间复杂度,除了扩容所导致的时间损耗,该算法复杂度为O(n) + def replaceSpaceByAppend(self, s): + string = list(string) + stringReplace = [] + for item in string: + if item == ' ': + stringReplace.append('%') + stringReplace.append('2') + stringReplace.append('0') + else: + stringReplace.append(item) + return "".join(stringReplace) # 创建新的字符串进行替换 def replaceSpace1(self, s):