vault backup: 2025-09-10 22:51:26
This commit is contained in:
@@ -34,3 +34,27 @@ def right_bound(nums, target):
|
||||
return right if right >= 0 and nums[right] == target else -1
|
||||
|
||||
```
|
||||
## 滑动窗口
|
||||
```python
|
||||
def lengthOfLongestSubstring(s: str) -> int:
|
||||
left = 0
|
||||
window = {}
|
||||
result = 0
|
||||
|
||||
for right in range(len(s)):
|
||||
char_right = s[right]
|
||||
window[char_right] = window.get(char_right, 0) + 1
|
||||
|
||||
# 当窗口中有重复字符时,收缩窗口
|
||||
while window[char_right] > 1:
|
||||
char_left = s[left]
|
||||
window[char_left] -= 1
|
||||
if window[char_left] == 0:
|
||||
del window[char_left]
|
||||
left += 1
|
||||
|
||||
result = max(result, right - left + 1)
|
||||
|
||||
return result
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user