diff options
| author | Mistivia <i@mistivia.com> | 2025-09-01 21:27:04 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-09-01 21:27:04 +0800 |
| commit | 6e4397623758ad5417a6b30858cefc223d51d282 (patch) | |
| tree | 4ac04fa1dcd24659bd2619fa64b896e2f1962b30 /rust/src/bin/p0003.rs | |
| parent | fff78815bae72051159a4a4ae5da34bea027317c (diff) | |
move
Diffstat (limited to 'rust/src/bin/p0003.rs')
| -rw-r--r-- | rust/src/bin/p0003.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/rust/src/bin/p0003.rs b/rust/src/bin/p0003.rs new file mode 100644 index 0000000..7293c13 --- /dev/null +++ b/rust/src/bin/p0003.rs @@ -0,0 +1,34 @@ +struct Solution {} + +impl Solution { + pub fn length_of_longest_substring(s: String) -> i32 { + let mut set: [u8;256] = [0;256]; + let mut i1: usize = 0; + let mut i2: usize = 0; + let mut max: i32 = 0; + loop { + if i1 >= s.len() { break; } + if set[s.as_bytes()[i1] as usize] == 0 { + set[s.as_bytes()[i1] as usize] = 1; + i1 = i1 + 1; + if i1 as i32 - i2 as i32 > max { max = i1 as i32 - i2 as i32 } + continue; + } else { + loop { + set[s.as_bytes()[i2] as usize] = 0; + i2 = i2 + 1; + if set[s.as_bytes()[i1] as usize] == 0 { + break; + } else { + continue; + } + } + } + } + max + } +} + +fn main() { + println!("{:?}", Solution::length_of_longest_substring("abcabcbb".to_string())); +}
\ No newline at end of file |
