8000 idr · jhideki/rust_leetcode@f08a2db · GitHub
[go: up one dir, main page]

Skip to content

Commit f08a2db

Browse files
committed
idr
1 parent 46535af commit f08a2db

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/longest_substring.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::{cmp::max, collections::HashSet};
2+
3+
pub struct Solution {}
4+
impl Solution {
5+
pub fn length_of_longest_substring(s: String) -> i32 {
6+
let mut length = 0;
7+
let mut lp = 0;
8+
let c: Vec<char> = s.chars().collect();
9+
let mut set: HashSet<char> = HashSet::new();
10+
println!("{}", c.len());
11+
for i in 0..c.len() {
12+
while set.contains(&c[i]) {
13+
set.remove(&c[lp]);
14+
lp += 1;
15+
}
16+
length = max(i - lp + 1, length);
17+
set.insert(c[i]);
18+
}
19+
length as i32
20+
}
21+
}

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
mod buy_and_sell_stock;
2-
use buy_and_sell_stock::Solution;
1+
mod longest_substring;
2+
use longest_substring::Solution;
33
fn main() {
4-
let nums1 = vec![7, 1, 5, 3, 6, 4];
5-
let result = Solution::max_profit(nums1);
4+
let s = "au".to_string();
5+
let result = Solution::length_of_longest_substring(s);
66
print!("{:?}", result);
77
}

0 commit comments

Comments
 (0)
0