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 cfcc7e0

Browse files
authored
feat: add rust solution to lc problem: No.0076 (doocs#1652)
No.0076.Minimum Window Substring
1 parent 072739c commit cfcc7e0

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

‎solution/0000-0099/0076.Minimum Window Substring/README.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,44 @@ public class Solution {
268268
}
269269
```
270270

271+
### **Rust**
272+
273+
```rust
274+
impl Solution {
275+
pub fn min_window(s: String, t: String) -> String {
276+
let (mut need, mut window, mut cnt) = ([0; 256], [0; 256], 0);
277+
for c in t.chars() {
278+
need[c as usize] += 1;
279+
}
280+
let (mut j, mut k, mut mi) = (0, -1, 1 << 31);
281+
for (i, c) in s.chars().enumerate() {
282+
window[c as usize] += 1;
283+
if need[c as usize] >= window[c as usize] {
284+
cnt += 1;
285+
}
286+
287+
while cnt == t.len() {
288+
if i - j + 1 < mi {
289+
k = j as i32;
290+
mi = i - j + 1;
291+
}
292+
let l = s.chars().nth(j).unwrap() as usize;
293+
if need[l] >= window[l] {
294+
cnt -= 1;
295+
}
296+
window[l] -= 1;
297+
j += 1;
298+
}
299+
}
300+
if k < 0 {
301+
return "".to_string();
302+
}
303+
let k = k as usize;
304+
s[k..k + mi].to_string()
305+
}
306+
}
307+
```
308+
271309
### **...**
272310

273311
```

‎solution/0000-0099/0076.Minimum Window Substring/README_EN.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,44 @@ public class Solution {
252252
}
253253
```
254254

255+
### **Rust**
256+
257+
```rust
258+
impl Solution {
259+
pub fn min_window(s: String, t: String) -> String {
260+
let (mut need, mut window, mut cnt) = ([0; 256], [0; 256], 0);
261+
for c in t.chars() {
262+
need[c as usize] += 1;
263+
}
264+
let (mut j, mut k, mut mi) = (0, -1, 1 << 31);
265+
for (i, c) in s.chars().enumerate() {
266+
window[c as usize] += 1;
267+
if need[c as usize] >= window[c as usize] {
268+
cnt += 1;
269+
}
270+
271+
while cnt == t.len() {
272+
if i - j + 1 < mi {
273+
k = j as i32;
274+
mi = i - j + 1;
275+
}
276+
let l = s.chars().nth(j).unwrap() as usize;
277+
if need[l] >= window[l] {
278+
cnt -= 1;
279+
}
280+
window[l] -= 1;
281+
j += 1;
282+
}
283+
}
284+
if k < 0 {
285+
return "".to_string();
286+
}
287+
let k = k as usize;
288+
s[k..k + mi].to_string()
289+
}
290+
}
291+
```
292+
255293
### **...**
256294

257295
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn min_window(s: String, t: String) -> String {
3+
let (mut need, mut window, mut cnt) = ([0; 256], [0; 256], 0);
4+
for c in t.chars() {
5+
need[c as usize] += 1;
6+
}
7+
let (mut j, mut k, mut mi) = (0, -1, 1 << 31);
8+
for (i, c) in s.chars().enumerate() {
9+
window[c as usize] += 1;
10+
if need[c as usize] >= window[c as usize] {
11+
cnt += 1;
12+
}
13+
14+
while cnt == t.len() {
15+
if i - j + 1 < mi {
16+
k = j as i32;
17+
mi = i - j + 1;
18+
}
19+
let l = s.chars().nth(j).unwrap() as usize;
20+
if need[l] >= window[l] {
21+
cnt -= 1;
22+
}
23+
window[l] -= 1;
24+
j += 1;
25+
}
26+
}
27+
if k < 0 {
28+
return "".to_string();
29+
}
30+
let k = k as usize;
31+
s[k..k + mi].to_string()
32+
}
33+
}

0 commit comments

Comments
(0)

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