Skip to content

Commit 04d1899

Browse files
committed
2019-09-08
1 parent 4688dc3 commit 04d1899

File tree

178 files changed

+2893
-2212
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+2893
-2212
lines changed
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def twoSum(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
hashmap = {}
9+
for i, x in enumerate(nums):
10+
if hashmap.has_key(target - x):
11+
return [hashmap[target - x], i]
12+
else:
13+
hashmap[x] = i
14+
15+
return []
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def addTwoNumbers(self, l1, l2):
9+
"""
10+
:type l1: ListNode
11+
:type l2: ListNode
12+
:rtype: ListNode
13+
"""
14+
if self.getLength(l1) < self.getLength(l2):
15+
l1, l2 = l2, l1
16+
head = l1
17+
while(l2):
18+
l1.val += l2.val
19+
l1 = l1.next
20+
l2 = l2.next
21+
22+
p = head
23+
while(p):
24+
if p.val > 9:
25+
p.val -= 10
26+
if p.next:
27+
p.next.val += 1
28+
else:
29+
p.next = ListNode(1)
30+
p = p.next
31+
return head
32+
33+
34+
def getLength(self, l):
35+
tmp = 0
36+
while(l):
37+
tmp += 1
38+
l = l.next
39+
return tmp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def lengthOfLongestSubstring(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
record = {}
8+
start, res = 0, 0
9+
for end in range(len(s)):
10+
if s[end] in record:
11+
start = max(start, record[s[end]] + 1)
12+
13+
record[s[end]] = end
14+
res = max(res, end - start + 1)
15+
16+
return res
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,22 @@
11
from heapq import *
2-
class MedianFinder(object):
3-
# 维护两个堆,一个大顶堆,一个小顶堆,小顶堆里的数比大顶堆里的数都要大,
4-
# 如果有两个潜在的中位数(两个堆size相同),数据流的中位数就是两个堆顶之和除以2
5-
# 如果只有一个中位数,就看size更小的那个堆的堆顶
6-
# 如果新进来的数比小顶堆的数要小,就把它插入大顶堆
7-
# 如果新进来的数比小顶堆的数要大,就把它插入小顶堆
8-
# 调整两个堆,使得size 差最大为1
2+
class DoubleHeap(object):
93
def __init__(self):
10-
"""
11-
initialize your data structure here.
12-
"""
13-
self.max_h = list()
14-
self.min_h = list()
15-
heapify(self.max_h)
16-
heapify(self.min_h)
4+
self.maxh = []
5+
self.minh = []
6+
heapify(self.maxh)
7+
heapify(self.minh)
178

18-
19-
def addNum(self, num):
20-
"""
21-
:type num: int
22-
:rtype: None
23-
"""
24-
heappush(self.min_h, num)
25-
heappush(self.max_h, -heappop(self.min_h))
26-
if len(self.max_h) > len(self.min_h):
27-
heappush(self.min_h, -heappop(self.max_h))
28-
29-
def findMedian(self):
30-
"""
31-
:rtype: float
32-
"""
33-
max_len = len(self.max_h)
34-
min_len = len(self.min_h)
35-
if max_len == min_len: #有两个候选中位数
36-
return (self.min_h[0] + -self.max_h[0]) / 2.
37-
else:#小顶堆的size 一定 >= 大顶堆的size,所以答案就是小顶堆的堆顶
38-
return self.min_h[0] / 1.
9+
def insert(self, val):
10+
heappush(self.minh, val)
11+
heappush(self.maxh, -heappop(self.minh))
12+
if len(self.maxh) > len(self.minh):
13+
heappush(self.minh, -heappop(self.maxh))
3914

40-
# Your MedianFinder object will be instantiated and called as such:
41-
# obj = MedianFinder()
42-
# obj.addNum(num)
43-
# param_2 = obj.findMedian()
15+
def findMedian(self):
16+
if len(self.maxh) == len(self.minh):
17+
return (self.minh[0] - self.maxh[0]) / 2.0
18+
return self.minh[0]/1.0
19+
4420

4521
class Solution(object):
4622
def findMedianSortedArrays(self, nums1, nums2):
@@ -49,9 +25,11 @@ def findMedianSortedArrays(self, nums1, nums2):
4925
:type nums2: List[int]
5026
:rtype: float
5127
"""
52-
mf = MedianFinder()
28+
dh = DoubleHeap()
5329
for num in nums1:
54-
mf.addNum(num)
30+
dh.insert(num)
31+
5532
for num in nums2:
56-
mf.addNum(num)
57-
return mf.findMedian()
33+
dh.insert(num)
34+
35+
return dh.findMedian()

0005.最长回文子串/0005-最长回文子串.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@ def longestPalindrome(self, s):
44
:type s: str
55
:rtype: str
66
"""
7-
max_l = 0
8-
res = ""
9-
for i in range(0, len(s)):
10-
#以s[i] 为中心向左右扩散
7+
left, right = 0, 0
8+
res, string = 0, ""
9+
for i in range(len(s)):
1110
left, right = i, i
1211
while(left >= 0 and right < len(s) and s[left] == s[right]):
13-
if max_l < right - left + 1:
14-
max_l = right - left + 1
15-
res = s[left:right + 1]
1612
left -= 1
1713
right += 1
18-
19-
#以s[i],s[i+1]为中心向左右扩散
20-
left, right = i, i + 1
14+
left += 1
15+
right -= 1
16+
17+
if right - left + 1 > res:
18+
res = right - left + 1
19+
string = s[left:right + 1]
20+
21+
for i in range(1, len(s)):
22+
left, right = i - 1, i
2123
while(left >= 0 and right < len(s) and s[left] == s[right]):
22-
if max_l < right - left + 1:
23-
max_l = right - left + 1
24-
res = s[left:right + 1]
2524
left -= 1
26-
right += 1
27-
return res
25+
right += 1
26+
left += 1
27+
right -= 1
28+
29+
if right - left + 1 > res:
30+
res = right - left + 1
31+
string = s[left:right + 1]
32+
return string
33+
+28-23
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
class Solution(object):
2-
def convert(self, s, n):
2+
def convert(self, s, numRows):
33
"""
44
:type s: str
55
:type numRows: int
66
:rtype: str
77
"""
8-
#ÕÒ¹æÂÉ
9-
if n <= 1:
8+
#第一行和最后一行都是相差 2 * (n - 1)
9+
#对于直角在上面的直角三角形, 相差 2 * (n - 1 - i)
10+
#对于直角在下面的直角三角形, 相差 2 * i
11+
if not s or numRows == 1:
1012
return s
11-
l = len(s)
1213
res = ""
13-
for i in range(n):
14-
tmp, index = "", i
15-
if i in [0, n - 1]:
16-
while(index < l):
17-
18-
tmp += s[index]
19-
index += 2 * (n - 1)
14+
for idx in range(numRows):
15+
if idx < len(s):
16+
res += s[idx]
17+
18+
if idx in [0, numRows - 1]:
19+
tmp = idx + 2 *(numRows - 1)
20+
while tmp < len(s):
21+
res += s[tmp]
22+
tmp += 2 *(numRows - 1)
2023
else:
21-
state = "down"
22-
while(index < l):
23-
tmp += s[index]
24-
if state == "down":
25-
state = "up"
26-
index += 2 * (n - 1 - i)
27-
else:
28-
state = "down"
29-
index += 2 * i
30-
res += tmp
31-
24+
tmp = idx + 2 * (numRows - 1 - idx)
25+
tri = "down"
26+
while tmp < len(s):
27+
res += s[tmp]
28+
if tri == "up":
29+
tmp += 2 * (numRows - 1 - idx)
30+
tri = "down"
31+
else:
32+
tmp += 2 * idx
33+
tri = "up"
34+
3235
return res
3336

34-
37+
38+
39+
3540

0007.整数反转/0007-整数反转.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ def reverse(self, x):
44
:type x: int
55
:rtype: int
66
"""
7-
8-
flag = 0
7+
INT_MIN = -2 **31
8+
INT_MAX = 2 ** 31 - 1
9+
op = 1
910
if x < 0:
10-
flag = 1
11-
if flag:
11+
op = -1
1212
s = str(x)[1:]
13-
s = s[::-1]
14-
x = -1 *int(s)
1513
else:
1614
s = str(x)
17-
s = s[::-1]
18-
x = int(s)
1915

20-
if x < -1 * 2 **31 or x > 2** 31 -1:
21-
return 0
22-
return x
16+
res = op * int(s[::-1])
17+
return res if INT_MIN <= res <= INT_MAX else 0
18+
19+

0008.字符串转换整数(atoi)/0008-字符串转换整数(atoi).py

+17-15
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,34 @@ def myAtoi(self, s):
55
:rtype: int
66
"""
77
s = s.strip(" ")
8-
if not len(s): #排除空
9-
return 0
10-
if s[0] not in ["+", "-"] and not s[0].isdigit(): #排除第一个非空字符不是数字
8+
# print s
9+
if not s or (s[0] not in ["+", "-"] and not s[0].isdigit()):
1110
return 0
11+
1212
op = 1
13-
res = ""
13+
tmp = ""
1414
for i, char in enumerate(s):
15-
if i == 0 :
15+
if i == 0:
1616
if char == "-":
1717
op = -1
1818
continue
1919
elif char == "+":
20-
continue
21-
if char == " " or not char.isdigit():
20+
pass
21+
continue
22+
if char.isdigit():
23+
tmp += char
24+
else:
2225
break
23-
res += char
24-
# print res, op
25-
if len(res) > 0:
26-
res = op * int(res)
26+
# print tmp
27+
if tmp:
28+
res = op * int(tmp)
2729
else:
28-
return 0
29-
INT_MIN = -2 **31
30+
res = 0
3031
INT_MAX = 2 **31 - 1
32+
INT_MIN = -2 **31
3133
if res > INT_MAX:
3234
return INT_MAX
3335
elif res < INT_MIN:
3436
return INT_MIN
35-
return res
36-
37+
else:
38+
return res

0009.回文数/0009-回文数.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
class Solution(object):
22
def isPalindrome(self, x):
3+
"""
4+
:type x: int
5+
:rtype: bool
6+
"""
7+
#2019.6.1
8+
xx = x
9+
if x < 0:
10+
return False
311

4-
s = str(x)
5-
if len(s) <= 1:
6-
return True
7-
print len(s) / 2
8-
for count in range(0, len(s)/2):
9-
if s[count] != s[len(s)-1 - count]:
10-
return False
11-
return True
12+
reverse = 0
13+
while x > 0:
14+
x, tmp = divmod(x, 10)
15+
reverse = reverse * 10 + tmp
16+
17+
return reverse == xx

0 commit comments

Comments
 (0)