20240512 Finished
This commit is contained in:
		| @@ -129,3 +129,4 @@ mod p242_valid_anagram; | ||||
| mod p49_group_anagrams; | ||||
| mod p202_happy_number; | ||||
| mod p219_contains_duplicate_ii; | ||||
| mod p128_longest_consecutive_sequence; | ||||
							
								
								
									
										38
									
								
								src/problem/p128_longest_consecutive_sequence.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/problem/p128_longest_consecutive_sequence.rs
									
									
									
									
									
										Normal 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])); | ||||
|     } | ||||
| } | ||||
| @@ -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() | ||||
|             ]) | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user