summaryrefslogtreecommitdiff
path: root/rust/src/length_of_longest_substring.rs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-23 21:07:19 +0800
committercrupest <crupest@outlook.com>2021-02-23 21:07:19 +0800
commitd8f3b40085619cb680c8f227c65a1f5acc393223 (patch)
tree6a38e3a6c79276fc396259ef962d17236dbed569 /rust/src/length_of_longest_substring.rs
parentb0162802ad9723c678e495f29ca2f0fc0af2eff1 (diff)
downloadsolutions-d8f3b40085619cb680c8f227c65a1f5acc393223.tar.gz
solutions-d8f3b40085619cb680c8f227c65a1f5acc393223.tar.bz2
solutions-d8f3b40085619cb680c8f227c65a1f5acc393223.zip
Move leetcode solutions to subdir.
Diffstat (limited to 'rust/src/length_of_longest_substring.rs')
-rw-r--r--rust/src/length_of_longest_substring.rs47
1 files changed, 0 insertions, 47 deletions
diff --git a/rust/src/length_of_longest_substring.rs b/rust/src/length_of_longest_substring.rs
deleted file mode 100644
index cbd5e14..0000000
--- a/rust/src/length_of_longest_substring.rs
+++ /dev/null
@@ -1,47 +0,0 @@
-use super::Solution;
-
-impl Solution {
- pub fn length_of_longest_substring(s: String) -> i32 {
- let mut map: [i32; std::u8::MAX as usize] = [-1; std::u8::MAX as usize];
- let mut last_index: i32 = 0;
- let mut result: i32 = 0;
- let bytes = s.as_bytes();
- for (i, c) in bytes.iter().enumerate() {
- let i = i as i32;
- let c = *c as usize;
- let li = map[c];
- if li >= last_index {
- last_index = li + 1;
- map[c] = i;
- } else {
- map[c] = i;
- let length = i - last_index + 1;
- if length > result {
- result = length;
- }
- }
- }
- result
- }
-}
-
-#[cfg(test)]
-mod tests {
- use super::Solution;
-
- #[test]
- fn test() {
- assert_eq!(
- Solution::length_of_longest_substring("abcabcbb".to_string()),
- 3
- );
- assert_eq!(
- Solution::length_of_longest_substring("bbbbb".to_string()),
- 1
- );
- assert_eq!(
- Solution::length_of_longest_substring("pwwkew".to_string()),
- 3
- );
- }
-}