From ae600869f4f883418864ee92101ecd10a6b86e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=A3=AE?= <627989472@qq.com> Date: Thu, 4 May 2017 16:32:33 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Update=20=E4=B8=8D=E7=94=A8=E5=8A=A0?= =?UTF-8?q?=E5=87=8F=E4=B9=98=E9=99=A4=E5=81=9A=E5=8A=A0=E6=B3=95.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过每次对num1进行与操作保证是一个32位的整形,因此最后我们可以判断符号位是否为1做处理。可以使Python AC --- ...44\345\201\232\345\212\240\346\263\225.py" | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) 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 From 9f0a15e6e7a8fd56813d8f2b2e9a0d59eca61ab9 Mon Sep 17 00:00:00 2001 From: wangershi <33387561+wangershi@users.noreply.github.com> Date: Sun, 5 Nov 2017 15:42:50 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Update=20=E4=BA=8C=E7=BB=B4=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=9F=A5=E6=89=BE.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 需要判断float是否有小数点,如果有小数点的话float肯定不是整数 --- ...\273\264\346\225\260\347\273\204\346\237\245\346\211\276.py" | 2 ++ 1 file changed, 2 insertions(+) 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) From 85dea78ce6e6988e83d753daf051384a1ab027e6 Mon Sep 17 00:00:00 2001 From: wangershi <33387561+wangershi@users.noreply.github.com> Date: Mon, 6 Nov 2017 19:39:27 +0800 Subject: [PATCH 3/4] =?UTF-8?q?Update=20=E6=9B=BF=E6=8D=A2=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit replace1方法和replace2方法都是O(n*m)的时间复杂度,这里m是空格的数量,因为list的insert是一个O(n)的复杂度。 而list的append是一个O(1)的时间复杂度,除了扩容时的时间损耗,append方法只需要遍历一次数组即可得结果。 --- ...233\277\346\215\242\347\251\272\346\240\274.py" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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): From c4b94d1182d3bd4aa2ddce81dfb5045fbf1de14a Mon Sep 17 00:00:00 2001 From: reece Date: Fri, 17 Aug 2018 17:50:00 +0800 Subject: [PATCH 4/4] fix index out of range --- QuickSort.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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