Skip to content

Commit 1bf8234

Browse files
committed
feat: 🎸 git add leetcode solution 1. 49. 128. 217. 238
1 parent 8c2738c commit 1bf8234

File tree

12 files changed

+282
-0
lines changed

12 files changed

+282
-0
lines changed

leet code/1. Two sum/solution.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::collections::HashMap;
2+
3+
struct Solution;
4+
5+
impl Solution {
6+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
7+
let mut map = HashMap::new();
8+
for (index, num) in nums.iter().enumerate() {
9+
if let Some(pair) = map.get(&(target - num)) {
10+
return vec![*pair as i32, index as i32];
11+
}
12+
map.insert(num, index as i32);
13+
}
14+
unreachable!();
15+
}
16+
}
17+
18+
fn main() {
19+
let nums = vec![2, 7, 11, 15];
20+
let target = 9;
21+
let result = Solution::two_sum(nums, target);
22+
println!("{:?}", result);
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn test_two_sum() {
31+
assert_eq!(Solution::two_sum(vec![2, 7, 11, 15], 9), vec![0, 1]);
32+
assert_eq!(Solution::two_sum(vec![3, 2, 4], 6), vec![1, 2]);
33+
assert_eq!(Solution::two_sum(vec![3, 3], 6), vec![0, 1]);
34+
}
35+
36+
#[test]
37+
#[should_panic]
38+
fn test_no_solution() {
39+
Solution::two_sum(vec![1, 2, 3], 7);
40+
}
41+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::collections::HashSet;
2+
use std::cmp;
3+
4+
struct Solution;
5+
6+
impl Solution {
7+
pub fn longest_consecutive(nums: Vec<i32>) -> i32 {
8+
//1. use HashSet remove duplicate value O(n)
9+
let mut set = HashSet::new();
10+
let mut max_len = 0;
11+
for i in nums {
12+
set.insert(i);
13+
}
14+
//2. calculate consecutive num length
15+
for &val in set.iter() {
16+
// 如果当前值是序列的起始值
17+
if !set.contains(&(val - 1)) {
18+
let mut n = val;
19+
let mut len = 1;
20+
while set.contains(&(n + 1)) {
21+
len += 1;
22+
n += 1;
23+
}
24+
max_len = cmp::max(max_len, len);
25+
}
26+
}
27+
//3. return max length
28+
max_len
29+
}
30+
}
31+
32+
fn main() {
33+
let nums = vec![100, 4, 200, 1, 3, 2];
34+
let result = Solution::longest_consecutive(nums);
35+
println!("Longest consecutive sequence length: {}", result);
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
42+
#[test]
43+
fn test_longest_consecutive() {
44+
assert_eq!(Solution::longest_consecutive(vec![100, 4, 200, 1, 3, 2]), 4);
45+
assert_eq!(Solution::longest_consecutive(vec![0, -1, 1, 2, -2, -3]), 6);
46+
assert_eq!(Solution::longest_consecutive(vec![]), 0);
47+
assert_eq!(Solution::longest_consecutive(vec![1, 2, 0, 1]), 3);
48+
assert_eq!(Solution::longest_consecutive(vec![1]), 1);
49+
}
50+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::collections::HashSet;
2+
3+
struct Solution;
4+
5+
impl Solution {
6+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
7+
// HashSet time: O(n) space: O(n)
8+
let mut set = HashSet::new();
9+
for i in 0..nums.len() {
10+
match set.contains(&nums[i]){
11+
true => return true,
12+
false => set.insert(nums[i]),
13+
};
14+
}
15+
false
16+
}
17+
}
18+
19+
fn main() {
20+
let nums = vec![1, 2, 3, 1];
21+
let result = Solution::contains_duplicate(nums);
22+
println!("Contains duplicate: {}", result);
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn test_contains_duplicate() {
31+
assert_eq!(Solution::contains_duplicate(vec![1, 2, 3, 1]), true);
32+
assert_eq!(Solution::contains_duplicate(vec![1, 2, 3, 4]), false);
33+
assert_eq!(Solution::contains_duplicate(vec![1, 1, 1, 3, 3, 4, 3, 2, 4, 2]), true);
34+
assert_eq!(Solution::contains_duplicate(vec![]), false);
35+
}
36+
37+
#[test]
38+
fn test_contains_duplicate_single_element() {
39+
assert_eq!(Solution::contains_duplicate(vec![1]), false);
40+
}
41+
}
550 KB
Binary file not shown.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn is_anagram(s: String, t: String) -> bool {
5+
// 1. compare two strings after sorting,O(nlogn)
6+
let mut vec_s: Vec<char> = s.chars().collect();
7+
let mut vec_t: Vec<char> = t.chars().collect();
8+
vec_s.sort();
9+
vec_t.sort();
10+
return vec_s==vec_t
11+
}
12+
}
13+
14+
fn main() {
15+
let s = String::from("anagram");
16+
let t = String::from("nagaram");
17+
let result = Solution::is_anagram(s, t);
18+
println!("Is anagram: {}", result);
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
25+
#[test]
26+
fn test_is_anagram() {
27+
assert_eq!(Solution::is_anagram(String::from("anagram"), String::from("nagaram")), true);
28+
assert_eq!(Solution::is_anagram(String::from("rat"), String::from("car")), false);
29+
assert_eq!(Solution::is_anagram(String::from(""), String::from("")), true);
30+
assert_eq!(Solution::is_anagram(String::from("a"), String::from("a")), true);
31+
assert_eq!(Solution::is_anagram(String::from("a"), String::from("b")), false);
32+
}
33+
34+
#[test]
35+
fn test_is_anagram_case_sensitive() {
36+
assert_eq!(Solution::is_anagram(String::from("Hello"), String::from("hello")), false);
37+
}
38+
}
1.18 MB
Binary file not shown.
582 KB
Binary file not shown.
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::collections::HashMap;
2+
3+
struct Solution;
4+
5+
impl Solution {
6+
pub fn is_anagram(s: String, t: String) -> bool {
7+
// create two HashMap and compare O(n)
8+
let mut map_s = HashMap::new();
9+
let mut map_t = HashMap::new();
10+
11+
for i in s.chars() {
12+
map_s.entry(i).and_modify(|v| *v += 1).or_insert(1);
13+
}
14+
for i in t.chars() {
15+
map_t.entry(i).and_modify(|v| *v += 1).or_insert(1);
16+
}
17+
map_s == map_t
18+
}
19+
}
20+
21+
fn main() {
22+
let s = String::from("anagram");
23+
let t = String::from("nagaram");
24+
let result = Solution::is_anagram(s, t);
25+
println!("Is anagram: {}", result);
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
fn test_is_anagram() {
34+
assert_eq!(Solution::is_anagram(String::from("anagram"), String::from("nagaram")), true);
35+
assert_eq!(Solution::is_anagram(String::from("rat"), String::from("car")), false);
36+
assert_eq!(Solution::is_anagram(String::from(""), String::from("")), true);
37+
assert_eq!(Solution::is_anagram(String::from("a"), String::from("a")), true);
38+
assert_eq!(Solution::is_anagram(String::from("a"), String::from("b")), false);
39+
}
40+
41+
#[test]
42+
fn test_is_anagram_case_sensitive() {
43+
assert_eq!(Solution::is_anagram(String::from("Hello"), String::from("hello")), false);
44+
}
45+
}
1.2 MB
Binary file not shown.
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use std::collections::HashMap;
2+
3+
struct Solution;
4+
5+
impl Solution {
6+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
7+
// HashMap key=[1,0,0,0,0,1,.....]:Vec[26] value vec!["eat","ate","tea"]
8+
// O(KN)
9+
let mut map: HashMap<Vec<i32>, Vec<String>> = HashMap::new();
10+
for i in 0..strs.len() {
11+
// create key 1,0,0,0,0,1,.....]
12+
let mut list = vec![0; 26];
13+
for &c_ascii in strs[i].as_bytes() {
14+
list[c_ascii as usize - b'a' as usize] += 1;
15+
}
16+
map.entry(list).or_insert_with(Vec::new).push(strs[i].clone());
17+
}
18+
map.into_values().collect()
19+
}
20+
}
21+
22+
fn main() {
23+
let strs = vec![
24+
"eat".to_string(),
25+
"tea".to_string(),
26+
"tan".to_string(),
27+
"ate".to_string(),
28+
"nat".to_string(),
29+
"bat".to_string(),
30+
];
31+
let result = Solution::group_anagrams(strs);
32+
for group in result {
33+
println!("{:?}", group);
34+
}
35+
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
41+
#[test]
42+
fn test_group_anagrams() {
43+
let input = vec![
44+
"eat".to_string(),
45+
"tea".to_string(),
46+
"tan".to_string(),
47+
"ate".to_string(),
48+
"nat".to_string(),
49+
"bat".to_string(),
50+
];
51+
let mut result = Solution::group_anagrams(input);
52+
for group in result.iter_mut() {
53+
group.sort(); // Sort to ensure the order does not affect the test
54+
}
55+
result.sort(); // Sort the outer vector for the same reason
56+
let mut expected = vec![
57+
vec!["bat".to_string()],
58+
vec!["nat".to_string(), "tan".to_string()],
59+
vec!["ate".to_string(), "eat".to_string(), "tea".to_string()],
60+
];
61+
for group in expected.iter_mut() {
62+
group.sort();
63+
}
64+
expected.sort();
65+
assert_eq!(result, expected);
66+
}
67+
}

0 commit comments

Comments
 (0)