Skip to content

Commit

Permalink
2019-09-08
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Sep 8, 2019
1 parent 4688dc3 commit 04d1899
Show file tree
Hide file tree
Showing 178 changed files with 2,893 additions and 2,212 deletions.
15 changes: 15 additions & 0 deletions 0001.两数之和/0001-两数之和.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
hashmap = {}
for i, x in enumerate(nums):
if hashmap.has_key(target - x):
return [hashmap[target - x], i]
else:
hashmap[x] = i

return []
39 changes: 39 additions & 0 deletions 0002.两数相加/0002-两数相加.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if self.getLength(l1) < self.getLength(l2):
l1, l2 = l2, l1
head = l1
while(l2):
l1.val += l2.val
l1 = l1.next
l2 = l2.next

p = head
while(p):
if p.val > 9:
p.val -= 10
if p.next:
p.next.val += 1
else:
p.next = ListNode(1)
p = p.next
return head


def getLength(self, l):
tmp = 0
while(l):
tmp += 1
l = l.next
return tmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
record = {}
start, res = 0, 0
for end in range(len(s)):
if s[end] in record:
start = max(start, record[s[end]] + 1)

record[s[end]] = end
res = max(res, end - start + 1)

return res
Original file line number Diff line number Diff line change
@@ -1,46 +1,22 @@
from heapq import *
class MedianFinder(object):
# 维护两个堆,一个大顶堆,一个小顶堆,小顶堆里的数比大顶堆里的数都要大,
# 如果有两个潜在的中位数(两个堆size相同),数据流的中位数就是两个堆顶之和除以2
# 如果只有一个中位数,就看size更小的那个堆的堆顶
# 如果新进来的数比小顶堆的数要小,就把它插入大顶堆
# 如果新进来的数比小顶堆的数要大,就把它插入小顶堆
# 调整两个堆,使得size 差最大为1
class DoubleHeap(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.max_h = list()
self.min_h = list()
heapify(self.max_h)
heapify(self.min_h)
self.maxh = []
self.minh = []
heapify(self.maxh)
heapify(self.minh)


def addNum(self, num):
"""
:type num: int
:rtype: None
"""
heappush(self.min_h, num)
heappush(self.max_h, -heappop(self.min_h))
if len(self.max_h) > len(self.min_h):
heappush(self.min_h, -heappop(self.max_h))

def findMedian(self):
"""
:rtype: float
"""
max_len = len(self.max_h)
min_len = len(self.min_h)
if max_len == min_len: #有两个候选中位数
return (self.min_h[0] + -self.max_h[0]) / 2.
else:#小顶堆的size 一定 >= 大顶堆的size,所以答案就是小顶堆的堆顶
return self.min_h[0] / 1.
def insert(self, val):
heappush(self.minh, val)
heappush(self.maxh, -heappop(self.minh))
if len(self.maxh) > len(self.minh):
heappush(self.minh, -heappop(self.maxh))

# Your MedianFinder object will be instantiated and called as such:
# obj = MedianFinder()
# obj.addNum(num)
# param_2 = obj.findMedian()
def findMedian(self):
if len(self.maxh) == len(self.minh):
return (self.minh[0] - self.maxh[0]) / 2.0
return self.minh[0]/1.0


class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
Expand All @@ -49,9 +25,11 @@ def findMedianSortedArrays(self, nums1, nums2):
:type nums2: List[int]
:rtype: float
"""
mf = MedianFinder()
dh = DoubleHeap()
for num in nums1:
mf.addNum(num)
dh.insert(num)

for num in nums2:
mf.addNum(num)
return mf.findMedian()
dh.insert(num)

return dh.findMedian()
36 changes: 21 additions & 15 deletions 0005.最长回文子串/0005-最长回文子串.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@ def longestPalindrome(self, s):
:type s: str
:rtype: str
"""
max_l = 0
res = ""
for i in range(0, len(s)):
#以s[i] 为中心向左右扩散
left, right = 0, 0
res, string = 0, ""
for i in range(len(s)):
left, right = i, i
while(left >= 0 and right < len(s) and s[left] == s[right]):
if max_l < right - left + 1:
max_l = right - left + 1
res = s[left:right + 1]
left -= 1
right += 1

#以s[i],s[i+1]为中心向左右扩散
left, right = i, i + 1
left += 1
right -= 1

if right - left + 1 > res:
res = right - left + 1
string = s[left:right + 1]

for i in range(1, len(s)):
left, right = i - 1, i
while(left >= 0 and right < len(s) and s[left] == s[right]):
if max_l < right - left + 1:
max_l = right - left + 1
res = s[left:right + 1]
left -= 1
right += 1
return res
right += 1
left += 1
right -= 1

if right - left + 1 > res:
res = right - left + 1
string = s[left:right + 1]
return string

51 changes: 28 additions & 23 deletions 0006.Z字形变换/0006-Z字形变换.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
class Solution(object):
def convert(self, s, n):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
#ÕÒ¹æÂÉ
if n <= 1:
#第一行和最后一行都是相差 2 * (n - 1)
#对于直角在上面的直角三角形, 相差 2 * (n - 1 - i)
#对于直角在下面的直角三角形, 相差 2 * i
if not s or numRows == 1:
return s
l = len(s)
res = ""
for i in range(n):
tmp, index = "", i
if i in [0, n - 1]:
while(index < l):

tmp += s[index]
index += 2 * (n - 1)
for idx in range(numRows):
if idx < len(s):
res += s[idx]

if idx in [0, numRows - 1]:
tmp = idx + 2 *(numRows - 1)
while tmp < len(s):
res += s[tmp]
tmp += 2 *(numRows - 1)
else:
state = "down"
while(index < l):
tmp += s[index]
if state == "down":
state = "up"
index += 2 * (n - 1 - i)
else:
state = "down"
index += 2 * i
res += tmp

tmp = idx + 2 * (numRows - 1 - idx)
tri = "down"
while tmp < len(s):
res += s[tmp]
if tri == "up":
tmp += 2 * (numRows - 1 - idx)
tri = "down"
else:
tmp += 2 * idx
tri = "up"
return res






19 changes: 8 additions & 11 deletions 0007.整数反转/0007-整数反转.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ def reverse(self, x):
:type x: int
:rtype: int
"""

flag = 0
INT_MIN = -2 **31
INT_MAX = 2 ** 31 - 1
op = 1
if x < 0:
flag = 1
if flag:
op = -1
s = str(x)[1:]
s = s[::-1]
x = -1 *int(s)
else:
s = str(x)
s = s[::-1]
x = int(s)

if x < -1 * 2 **31 or x > 2** 31 -1:
return 0
return x
res = op * int(s[::-1])
return res if INT_MIN <= res <= INT_MAX else 0


Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,34 @@ def myAtoi(self, s):
:rtype: int
"""
s = s.strip(" ")
if not len(s): #排除空
return 0
if s[0] not in ["+", "-"] and not s[0].isdigit(): #排除第一个非空字符不是数字
# print s
if not s or (s[0] not in ["+", "-"] and not s[0].isdigit()):
return 0

op = 1
res = ""
tmp = ""
for i, char in enumerate(s):
if i == 0 :
if i == 0:
if char == "-":
op = -1
continue
elif char == "+":
continue
if char == " " or not char.isdigit():
pass
continue
if char.isdigit():
tmp += char
else:
break
res += char
# print res, op
if len(res) > 0:
res = op * int(res)
# print tmp
if tmp:
res = op * int(tmp)
else:
return 0
INT_MIN = -2 **31
res = 0
INT_MAX = 2 **31 - 1
INT_MIN = -2 **31
if res > INT_MAX:
return INT_MAX
elif res < INT_MIN:
return INT_MIN
return res

else:
return res
22 changes: 14 additions & 8 deletions 0009.回文数/0009-回文数.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
#2019.6.1
xx = x
if x < 0:
return False

s = str(x)
if len(s) <= 1:
return True
print len(s) / 2
for count in range(0, len(s)/2):
if s[count] != s[len(s)-1 - count]:
return False
return True
reverse = 0
while x > 0:
x, tmp = divmod(x, 10)
reverse = reverse * 10 + tmp

return reverse == xx
Loading

0 comments on commit 04d1899

Please sign in to comment.