Skip to content

Commit 4a9f3d0

Browse files
committed
2019-07-16
1 parent bd0b701 commit 4a9f3d0

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution(object):
2+
def isOneEditDistance(self, s, t):
3+
distance = len(s) - len(t)
4+
if abs(distance) > 1: #长度都差了不止一位,肯定不对
5+
return False
6+
if not s or not t: #两者有一个为空返回真,两个都为空返回假
7+
return s != t
8+
9+
edit = 0 #代表编辑次数
10+
i, j = 0, 0
11+
while i < len(s) and j < len(t):
12+
if s[i] == t[j]: #无需编辑
13+
i += 1 #两个指针都顺序后延一位
14+
j += 1
15+
else:
16+
if edit: #唯一的机会已经用完了
17+
return False
18+
if distance == 1: #删除
19+
i += 1
20+
elif distance == -1:#插入
21+
j += 1
22+
else: #替换
23+
i += 1
24+
j += 1
25+
edit += 1
26+
27+
if i < len(s): #如果t没了,s还多了一位,取决于edit还是不是0
28+
return edit == 0
29+
if j < len(t): #如果s没了,t还多了一位,取决于edit还是不是0
30+
return edit == 0
31+
32+
return i == len(s) and j == len(t) and edit == 1
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def maximumGap(self, nums):
3+
if len(nums) < 2:
4+
return 0
5+
min_val, max_val = min(nums), max(nums)
6+
if min_val == max_val:
7+
return 0
8+
9+
n = len(nums) + 1 # 桶的个数
10+
step = (max_val - min_val) // n
11+
12+
exist = [0 for _ in range(n + 1)] #表示桶是否为空
13+
max_num = [0 for _ in range(n + 1)]#表示桶里元素的最大值
14+
min_num = [0 for _ in range(n + 1)]#表示桶离元素的最小值
15+
16+
for num in nums: #把所有的数入桶
17+
idx = self.findBucketIndex(num, min_val, max_val, n)
18+
max_num[idx] = num if not exist[idx] else max(num, max_num[idx])
19+
min_num[idx] = num if not exist[idx] else min(num, min_num[idx])
20+
exist[idx] = 1
21+
res = 0
22+
pre = max_num[0]
23+
for i in range(1, n + 1):
24+
if exist[i]:
25+
res = max(res, min_num[i] - pre)
26+
pre = max_num[i]
27+
return res
28+
29+
def findBucketIndex(self, num, min_val, max_val, n):
30+
return int((num - min_val) * n / (max_val - min_val))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution(object):
2+
def compareVersion(self, version1, version2):
3+
"""
4+
:type version1: str
5+
:type version2: str
6+
:rtype: int
7+
"""
8+
l1 = version1.split(".")
9+
l2 = version2.split(".")
10+
11+
i, j = 0, 0
12+
while i < len(l1) and j < len(l2): # 逐位比大小
13+
num1, num2 = int(l1[i]), int(l2[j])
14+
if num1 < num2:
15+
return -1
16+
elif num1 > num2:
17+
return 1
18+
i += 1
19+
j += 1
20+
21+
if len(l1) == len(l2) and num1 == num2: #长度相等而且最后一位也相等
22+
return 0
23+
24+
if len(l1) > len(l2): #如果l1还在而l2结束了
25+
while i < len(l1) and int(l1[i]) == 0: #判断l1后面是不是全是0
26+
i += 1
27+
if i == len(l1): #如果全是0,则还相等
28+
return 0
29+
else:
30+
return 1
31+
32+
if len(l2) > len(l1):
33+
while j < len(l2) and int(l2[j]) == 0: #判断l2后面是不是全是0
34+
j += 1
35+
if j == len(l2): #如果全是0,则还相等
36+
return 0
37+
else:
38+
return -1
39+
return -1

5119.删点成林/5119-删点成林.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# self.left = None
66
# self.right = None
77

8+
89
class Solution(object):
910
def delNodes(self, root, to_delete):
1011
"""
@@ -30,7 +31,7 @@ def delNodes(self, root, to_delete):
3031
def dfs(node):
3132
if not node:
3233
return
33-
34+
3435
if node.left and node.left.val in to_delete: #左节点要删
3536
dfs(node.left)
3637
if node.left.left: #左节点的左节点还在,不需要删
@@ -48,11 +49,9 @@ def dfs(node):
4849
self.roots += [node.right.right]
4950

5051
node.right = None
51-
52+
5253
dfs(node.left)
5354
dfs(node.right)
5455

5556
dfs(root)
5657
return self.roots
57-
58-

0 commit comments

Comments
 (0)