Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ddfc8e5

Browse files
author
hj.tian
committed
feat: add leetcode75 day29
feat: add leetcode75 day29
1 parent 4ee7fc6 commit ddfc8e5

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

‎leetcode75/day29_16.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def threeSumClosest(self, nums: List[int], target: int) -> int:
6+
"""
7+
1. i,j,k 暴力破解
8+
2. 双指针
9+
-4, -1, 1, 2
10+
i s-> <-e
11+
=, return
12+
>, s+1
13+
<, e-1
14+
><需要考虑记录最接近的值(N*N)
15+
"""
16+
lens = len(nums)
17+
if lens < 3:
18+
raise KeyError
19+
nums.sort()
20+
ans = 0
21+
diff_mins = 100000
22+
for i in range(lens - 2):
23+
start = i + 1
24+
end = lens - 1
25+
while start < end:
26+
three_sum = nums[i] + nums[start] + nums[end]
27+
if three_sum == target:
28+
return target
29+
else:
30+
if three_sum < target:
31+
start += 1
32+
else:
33+
end -= 1
34+
diff = abs(three_sum - target)
35+
if diff < diff_mins:
36+
diff_mins = diff
37+
ans = three_sum
38+
return ans
39+
40+
41+
if __name__ == "__main__":
42+
assert Solution().threeSumClosest(nums=[-1, 2, 1, -4], target=1) == 2
43+
assert Solution().threeSumClosest(nums=[0, 0, 0], target=1) == 0

‎leetcode75/day29_3.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
def lengthOfLongestSubstring(self, s: str) -> int:
6+
"""滑动窗口
7+
s s s s s s
8+
pwwkew pwwkew pwwkew pwwkew pwwkew pwwkew
9+
e e e e e e
10+
11+
max 1 2 1 2 3 3
12+
=> 3
13+
"""
14+
maxs = start = end = 0
15+
counter = Counter()
16+
while end < len(s):
17+
if counter[s[end]] <= 0:
18+
counter[s[end]] += 1
19+
else:
20+
while s[start] != s[end]:
21+
counter[s[start]] -= 1
22+
start += 1
23+
start += 1
24+
maxs = max(maxs, end - start + 1)
25+
end += 1
26+
return maxs
27+
28+
29+
if __name__ == "__main__":
30+
assert Solution().lengthOfLongestSubstring("abcabcbb") == 3
31+
assert Solution().lengthOfLongestSubstring("bbbb") == 1
32+
assert Solution().lengthOfLongestSubstring("pwwkew") == 3
33+
assert Solution().lengthOfLongestSubstring("tmmzuxt") == 5

‎leetcode75/day29_76.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
def minWindow(self, s: str, t: str) -> str:
6+
"""
7+
3. 优化双指针
8+
9+
s
10+
ABCBAC, ABC
11+
e
12+
"""
13+
target = Counter(t)
14+
record = Counter()
15+
source = [(char, index) for index, char in enumerate(s) if char in t]
16+
min_start, min_end = 0, len(s) + 1
17+
start = end = 0
18+
while end < len(source) and start + len(t) <= len(source):
19+
record[source[end][0]] += 1
20+
# 满足了条件,覆盖全部t的位置
21+
if record >= target:
22+
# 移动 start, 直到不满点
23+
while record >= target:
24+
record[source[start][0]] -= 1
25+
start += 1
26+
if source[end][1] - source[start - 1][1] < min_end - min_start:
27+
min_start, min_end = source[start - 1][1], source[end][1]
28+
end += 1
29+
if min_end == len(s) + 1:
30+
return ""
31+
return s[min_start : min_end + 1]
32+
33+
34+
# class Solution:
35+
# def minWindow(self, s: str, t: str) -> str:
36+
# """
37+
# 2. 双指针
38+
39+
# s
40+
# ADOBECODEBANC
41+
# e
42+
43+
# s
44+
# ADOBECODEBANC # e走到覆盖全部t的位置
45+
# e
46+
47+
# s
48+
# ADOBECODEBANC # s走到不覆盖全部t的位置, 计算长度并更新最小长度
49+
# e
50+
# s s
51+
# ADOBECODEBANC -> ADOBECODEBANC
52+
# e e
53+
# (0,5) -> (5, 10) -> (9, 12)
54+
# => s[9:12+1]
55+
# """
56+
# target = Counter(t)
57+
# record = Counter()
58+
# min_start, min_end = 0, len(s) + 1
59+
# start = end = 0
60+
# while end < len(s) and start + len(t) <= len(s):
61+
# if s[end] in t:
62+
# record[s[end]] += 1
63+
# # 满足了条件,覆盖全部t的位置
64+
# if all(record[char] >= target[char] for char in target):
65+
# # 移动 start, 直到不满点
66+
# while all(record[char] >= target[char] for char in target):
67+
# record[s[start]] -= 1
68+
# start += 1
69+
# if end - (start - 1) < min_end - min_start:
70+
# min_start, min_end = start - 1, end
71+
# end += 1
72+
# if min_end == len(s) + 1:
73+
# return ""
74+
# return s[min_start : min_end + 1]
75+
76+
77+
# class Solution:
78+
# def minWindow(self, s: str, t: str) -> str:
79+
# """
80+
# 1. 暴力枚举
81+
# a. 找到起始点,只要字母在t中都可以成为起始点
82+
# b. 往右走,直到覆盖t, 更新最小长度
83+
# """
84+
# # 构建起始点
85+
# starts = []
86+
# for i in range(len(s)):
87+
# if s[i] in t and i + len(t) <= len(s):
88+
# starts.append(i)
89+
# mins = len(s) + 1
90+
# mins_start = mins_end = 0
91+
# flag = False
92+
# for start in starts:
93+
# end = start
94+
# target = Counter(t)
95+
# record = Counter()
96+
# while end < len(s):
97+
# if s[end] in t:
98+
# record[s[end]] += 1
99+
# if all(record[char] >= target[char] for char in target.keys()) and end - start + 1 < mins:
100+
# # 记录搜索到了,更新最小长度
101+
# mins = end - start + 1
102+
# mins_start, mins_end = start, end
103+
# flag = True
104+
# break
105+
# end += 1
106+
# if flag is False:
107+
# return ""
108+
# return s[mins_start : mins_end + 1]
109+
110+
111+
if __name__ == "__main__":
112+
assert Solution().minWindow(s="ADOBECODEBANC", t="ABC") == "BANC"
113+
assert Solution().minWindow(s="a", t="a") == "a"
114+
assert Solution().minWindow(s="a", t="aa") == ""
115+
assert Solution().minWindow(s="a", t="b") == ""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /