20240512 Finished

This commit is contained in:
jackfiled 2024-05-12 10:48:36 +08:00
parent 3c1784fa83
commit b6a9384603
3 changed files with 40 additions and 16 deletions

View File

@ -128,4 +128,5 @@ mod p205_isomorphic_strings;
mod p242_valid_anagram;
mod p49_group_anagrams;
mod p202_happy_number;
mod p219_contains_duplicate_ii;
mod p219_contains_duplicate_ii;
mod p128_longest_consecutive_sequence;

View File

@ -0,0 +1,38 @@
/**
* [128] Longest Consecutive Sequence
*/
pub struct Solution {}
// submission codes start here
use std::collections::HashSet;
impl Solution {
pub fn longest_consecutive(nums: Vec<i32>) -> i32 {
let d: HashSet<i32> = nums.iter().map(|&i| i).collect();
nums.into_iter()
.map(|mut i| {
let mut t = 1;
if !d.contains(&(i - 1)) {
while d.contains(&(i + 1)) {
t += 1;
i += 1;
}
}
t
})
.max()
.unwrap_or(0)
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_128() {
assert_eq!(4, Solution::longest_consecutive(vec![100, 1, 200, 4, 3, 2]));
}
}

View File

@ -88,20 +88,5 @@ mod tests {
characters: HashMap::from([('a', 1), ('t', 1), ('e', 1)])
}
);
assert_eq!(
vec![
vec!["bat".to_owned()],
vec!["nat".to_owned(), "tan".to_owned()],
vec!["ate".to_owned(), "eat".to_owned(), "tea".to_owned()]
],
Solution::group_anagrams(vec![
"eat".to_owned(),
"tea".to_owned(),
"tan".to_owned(),
"ate".to_owned(),
"nat".to_owned(),
"bat".to_owned()
])
);
}
}