id 1399197705 Time: 136 ms MemUsage: 2.1 MB
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
use std::ops::ControlFlow;
|
||||
use std::collections::HashSet;
|
||||
|
||||
impl Solution {
|
||||
pub fn length_of_longest_substring(s: String) -> i32 {
|
||||
let string_length = s.len();
|
||||
|
||||
match (0..string_length).try_fold(0, |max_length, i| {
|
||||
match max_length < string_length - i {
|
||||
true => {
|
||||
match s[i..].bytes().try_fold((0, 0, HashSet::new()), |(max_length, length, mut counter), x| {
|
||||
match (counter.contains(&x), length > max_length){
|
||||
(true, true) => ControlFlow::Break(length),
|
||||
(true, false) => ControlFlow::Break(max_length),
|
||||
(false, _) => {
|
||||
counter.insert(x);
|
||||
ControlFlow::Continue((max_length, length + 1, counter))
|
||||
},
|
||||
}
|
||||
}){
|
||||
ControlFlow::Break(length) => ControlFlow::Continue(max_length.max(length)),
|
||||
ControlFlow::Continue((ml, l, _)) => ControlFlow::Continue(max_length.max(ml.max(l))),
|
||||
}
|
||||
},
|
||||
false => ControlFlow::Break(max_length),
|
||||
}
|
||||
}){
|
||||
ControlFlow::Continue(length) => length as i32,
|
||||
ControlFlow::Break(length) => length as i32,
|
||||
_ => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user