Skip to content

Commit 0c36294

Browse files
author
xingdali
committed
添加题567,438,3
1 parent 2dab8e8 commit 0c36294

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

‎算法思维/滑动窗口思想.md‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,146 @@ function minWindow(s: string, t: string): string{
8383
};
8484
```
8585

86+
##### [567. 字符串的排列](https://leetcode-cn.com/problems/permutation-in-string/)
87+
88+
给定两个字符串 `s1``s2`,写一个函数来判断 `s2` 是否包含 `s1` 的排列。换句话说,第一个字符串的排列之一是第二个字符串的 **子串**
89+
90+
```tsx
91+
function checkInclusion(s1:string, s2:string):boolean{
92+
if (s2.length<s1.length){
93+
returnfalse
94+
}
95+
const s1arr:number[] =newArray(26).fill(0)
96+
const s2arr:number[] =newArray(26).fill(0)
97+
for (let i =0; i<s1.length; i++){
98+
s1arr[s1.charCodeAt(i) -97]++
99+
s2arr[s2.charCodeAt(i) -97]++
100+
}
101+
if (s1arr.toString() ===s2arr.toString()){
102+
returntrue
103+
}
104+
for (let j =s1.length; j<s2.length; j++){
105+
s2arr[s2.charCodeAt(j) -97]++
106+
s2arr[s2.charCodeAt(j-s1.length) -97]--
107+
if (s1arr.toString() ===s2arr.toString()){
108+
returntrue
109+
}
110+
}
111+
returnfalse
112+
};
113+
```
114+
115+
```tsx
116+
function checkInclusion(s1:string, s2:string):boolean{
117+
const win =newMap<string, number>()
118+
const need =newMap<string, number>()
119+
for (let i =0; i<s1.length; i++){
120+
need.set(s1[i], need.has(s1[i]) ?need.get(s1[i]) +1:1)
121+
}
122+
let left:number=0, right:number=0, match:number=0
123+
while (right<s2.length){
124+
const c:string=s2[right]
125+
right++
126+
if (need.has(c)){
127+
win.set(c, win.has(c) ?win.get(c) +1:1)
128+
if (win.get(c) ===need.get(c)){
129+
match++
130+
}
131+
}
132+
while (right-left>=s1.length){
133+
if (match===need.size){
134+
returntrue
135+
}
136+
const d:string=s2[left]
137+
left++
138+
if (need.has(d)){
139+
if (win.get(d) ===need.get(d)){
140+
match--
141+
}
142+
win.set(d, win.get(d) -1)
143+
}
144+
}
145+
}
146+
returnfalse
147+
};
148+
```
149+
150+
##### [438. 找到字符串中所有字母异位词](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/)
151+
152+
给定一个字符串 **s** 和一个非空字符串 **p**,找到 **s** 中所有是 **p** 的字母异位词的子串,返回这些子串的起始索引。
153+
154+
```tsx
155+
function findAnagrams(s:string, p:string):number[]{
156+
const win =newMap<string, number>()
157+
const need =newMap<string, number>()
158+
const ans:number[] = []
159+
for (let i =0; i<p.length; i++){
160+
need.set(p[i], need.has(p[i]) ?need.get(p[i]) +1:1)
161+
}
162+
let left:number=0, right:number=0, match:number=0
163+
while (right<s.length){
164+
const c:string=s[right]
165+
right++
166+
if (need.has(c)){
167+
win.set(c, win.has(c) ?win.get(c) +1:1)
168+
if (win.get(c) ===need.get(c)){
169+
match++
170+
}
171+
}
172+
while (right-left>=p.length){
173+
if (match===need.size){
174+
ans.push(left)
175+
}
176+
const d:string=s[left]
177+
left++
178+
if (need.has(d)){
179+
if (win.get(d) ===need.get(d)){
180+
match--
181+
}
182+
win.set(d, win.get(d) -1)
183+
}
184+
}
185+
}
186+
returnans
187+
};
188+
```
189+
190+
##### [3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)
191+
192+
给定一个字符串,请你找出其中不含有重复字符的 **最长子串** 的长度。
193+
194+
```tsx
195+
function lengthOfLongestSubstring(s:string):number{
196+
const win =newSet<string>()
197+
let left:number=0, right:number=0, ans:number=0
198+
while (right<s.length){
199+
let c:string=s[right]
200+
if (win.has(c)){
201+
c=s[left]
202+
win.delete(c)
203+
left++
204+
} else{
205+
win.add(c)
206+
right++
207+
}
208+
ans=Math.max(ans, win.size)
209+
}
210+
returnans
211+
};
212+
```
213+
214+
## 总结
215+
216+
- 和双指针题目类似,更像双指针的升级版,滑动窗口核心点是维护一个窗口集,根据窗口集来进行处理
217+
- 核心步骤
218+
- right 右移
219+
- 收缩
220+
- left 右移
221+
- 求结果
222+
223+
## 练习
224+
225+
-[minimum-window-substring](https://leetcode-cn.com/problems/minimum-window-substring/)
226+
-[permutation-in-string](https://leetcode-cn.com/problems/permutation-in-string/)
227+
-[find-all-anagrams-in-a-string](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/)
228+
-[longest-substring-without-repeating-characters](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/)

0 commit comments

Comments
(0)