From ffd65ffa6db03ac9a10bed91eaff654de1cca33e Mon Sep 17 00:00:00 2001 From: niujiaxi <68394845@163.com> Date: Tue, 22 Jan 2019 18:13:33 +0800 Subject: [PATCH] ok --- hw1/Heater2.py | 6 ++++++ hw1/IntReverse.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw1/LengthOfLongestSubstring.py | 33 +++++++++++++++++++++++++++++++++ hw1/PeakIndexInMountainArray.py | 3 +-- hw1/ToeplitzMatrix.py | 3 +++ hw1/Zconvert.py | 18 +++++++++++------- hw1/test.py | 19 ++++++++++++++++--- 7 files changed, 136 insertions(+), 12 deletions(-) create mode 100644 hw1/IntReverse.py create mode 100644 hw1/LengthOfLongestSubstring.py diff --git a/hw1/Heater2.py b/hw1/Heater2.py index d7a657c..f4b193f 100644 --- a/hw1/Heater2.py +++ b/hw1/Heater2.py @@ -4,6 +4,7 @@ class Heater: houses.sort() radius = 0 i = 0 + # 哨兵 heaters = [-1] + heaters + [float('inf')] for house in houses: while house > heaters[i]: @@ -13,6 +14,11 @@ class Heater: print(radius) +houses = [1, 2, 4, 9] +heaters = [-1, 1, 8, float('inf')] + +Heater().findRadius(houses, heaters) + houses = [1, 2, 3] heaters = [2] Heater().findRadius(houses, heaters) diff --git a/hw1/IntReverse.py b/hw1/IntReverse.py new file mode 100644 index 0000000..3d635b5 --- /dev/null +++ b/hw1/IntReverse.py @@ -0,0 +1,66 @@ +class Solution: + def reverse(self, x): + """ + :type x: int + :rtype: int + """ + stark = [] + symbol = "" + for c in str(x): + if c != '-': + stark.append(c) + else: + symbol = '-' + + value = "" + if symbol != "": + value = value + symbol + + while stark: + value = value + stark.pop() + + rtype = int(value) + if rtype > 2147483647 or rtype < -2147483648: + return 0 + else: + return rtype + + +i = 123 +Solution().reverse(i) + +i = -123 +Solution().reverse(i) + +i = 120 +Solution().reverse(i) + + +class Solution1: + def reverse(self, x): + """ + :type x: int + :rtype: int + """ + max = 2147483647 + sign = 1 if x >= 0 else -1 + y = 0 + while True: + a = x // 10 + b = x - a * 10 + y = b + y * 10 + x = a + if y > max: + return 0 + if x == 0: + return sign * y + + +i = 123 +print(Solution1().reverse(i)) + +i = -123 +print(Solution1().reverse(i)) + +i = 120 +print(Solution1().reverse(i)) diff --git a/hw1/LengthOfLongestSubstring.py b/hw1/LengthOfLongestSubstring.py new file mode 100644 index 0000000..f3d02bc --- /dev/null +++ b/hw1/LengthOfLongestSubstring.py @@ -0,0 +1,33 @@ +""" +给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 + +示例 1: + +输入: "abcabcbb" +输出: 3 +解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 +示例 2: + +输入: "bbbbb" +输出: 1 +解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 +示例 3: + +输入: "pwwkew" +输出: 3 +解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 + 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串 + +""" + + +class Solution: + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + index = 0 + next = index + 1 + for c in s: + print(c) diff --git a/hw1/PeakIndexInMountainArray.py b/hw1/PeakIndexInMountainArray.py index a621209..65ef1e6 100644 --- a/hw1/PeakIndexInMountainArray.py +++ b/hw1/PeakIndexInMountainArray.py @@ -4,8 +4,7 @@ def peakIndexInMountainArray(mountain): while low <= height: mid = (height + low) // 2 if array[mid] > array[mid - 1] and array[mid] > array[mid + 1]: - print(mid) - break + return mid elif array[mid] > array[mid + 1]: height = mid - 1 else: diff --git a/hw1/ToeplitzMatrix.py b/hw1/ToeplitzMatrix.py index 1fa586e..0d3fecb 100644 --- a/hw1/ToeplitzMatrix.py +++ b/hw1/ToeplitzMatrix.py @@ -12,9 +12,12 @@ def isToeplitzMatri(matrix): [1,2,3,4], [5,1,2,3], [9,5,1,2] + """ matrix = [[1, 2, 3, 4], [5, 1, 2, 3], [9, 5, 1, 2]] print(isToeplitzMatri(matrix)) + + diff --git a/hw1/Zconvert.py b/hw1/Zconvert.py index 64062df..73489a2 100644 --- a/hw1/Zconvert.py +++ b/hw1/Zconvert.py @@ -1,5 +1,7 @@ def convert(s, numRows): # 初始化数组 + if numRows != 1: + return s rows = [''] * numRows index = 0 isInc = True @@ -16,12 +18,14 @@ def convert(s, numRows): for row in rows: str = str + row - print(str) + return str -str = "0123456789" -str = "LEETCODEISHIRING" - -convert(str, 3) -convert(str, 4) - +# +# str = "0123456789" +# str = "LEETCODEISHIRING" +# +# convert(str, 3) +# convert(str, 4) +str = "AB" +print(convert(str, 1)) diff --git a/hw1/test.py b/hw1/test.py index 6683a78..c18e896 100644 --- a/hw1/test.py +++ b/hw1/test.py @@ -2,23 +2,36 @@ def quick_sort(array, l, r): if l >= r: return stack = [] + # 存储分区 左 右 0,6 的索引 stack.append(l) stack.append(r) - while stack: + while stack: # 如果没数据表示所有分区结束 low = stack.pop(0) high = stack.pop(0) + # 越界检查 if high - low <= 0: continue + # 取右值x作为基准 x = array[high] i = low - 1 + # 从左边开始遍历 for j in range(low, high): + # 是否小于等于基准值x + # 每次交换都放到左边 如果小于基准值 + # 1, 7, 8, 9, 30, 5, 6 + # x = 6 + # i = -1 + # j=0 i=0 1 < 6 j 和 i 交换 + # j=5 i=1 5 > 6 j 和 i 交换 if array[j] <= x: - i += 1 + i = i + 1 array[i], array[j] = array[j], array[i] + # 最后取第一个比x值的和x值交换 这样 第一遍排序 1 5 6 9 30 7 8 x=6 array[i + 1], array[high] = array[high], array[i + 1] + # 根据x值分区 low~i 是比当前x小的 x+1 是当前的x值 i+2~high 是比当前x值大的 stack.extend([low, i, i + 2, high]) print(array) array = [1, 7, 8, 9, 30, 5, 6] -quick_sort(array, 0, len(array)-1) +quick_sort(array, 0, len(array) - 1) -- libgit2 0.26.0