Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit98da5e6

Browse files
authored
Merge pull requestneetcode-gh#501 from tomijaga/main
Rust: Two Pointers (two_sum, 3sum))
2 parents3a8d6d4 +3d812ba commit98da5e6

File tree

6 files changed

+72
-9
lines changed

6 files changed

+72
-9
lines changed

‎rust/01_arrays_&_hashing/217_contains_duplicates.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ impl Solution {
44
pubfncontains_duplicate(nums:Vec<i32>) ->bool{
55
letmut map =HashSet::new();
66

7-
for nin nums.iter(){
7+
for&nin nums.iter(){
88

9-
if map.contains(n){
9+
if map.contains(&n){
1010
returntrue;
1111
}
1212

1313
map.insert(n);
1414
};
1515

16-
false
16+
false
1717
}
18-
}
18+
}

‎rust/01_arrays_&_hashing/347_top_k_frequent_elements.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl Solution {
1111

1212
letmut freq:Vec<(i32,i32)> = map.into_iter().collect();
1313

14-
let res =if k ==arr.len()asi32{
14+
let res =if k ==freq.len()asi32{
1515
&freq
1616
}else{
1717
quick_select(&mut freq, k)

‎rust/01_arrays_&_hashing/49_group_anagrams.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use std::collections::HashMap;
22

33
implSolution{
44
pubfngroup_anagrams(strs:Vec<String>) ->Vec<Vec<String>>{
5-
letmut map:HashMap<[u16;26],Vec<String>> =HashMap::new();
5+
letmut map:HashMap<[u8;26],Vec<String>> =HashMap::new();
66

77
for sin strs{
8-
letmut key =[0_u16;26];
8+
letmut key =[0_u8;26];
99

1010
for cin s.chars(){
1111
key[casusize -'a'asusize] +=1;
@@ -18,6 +18,6 @@ impl Solution {
1818
}
1919
}
2020

21-
map.into_values().collect::<Vec<Vec<String>>>()
21+
map.into_values().collect()
2222
}
2323
}

‎rust/02_two_pointers/11_container_with_most_water.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Solution {
1515
max = area;
1616
}
1717

18-
if rh< lh{
18+
if rh< lh{
1919
while r >0 && height[r] <= rh{
2020
r-=1;
2121
}

‎rust/02_two_pointers/15_3sum.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
implSolution{
2+
pubfnthree_sum(mutnums:Vec<i32>) ->Vec<Vec<i32>>{
3+
nums.sort_unstable();
4+
5+
letmut res =vec![];
6+
7+
for iin0..nums.len() -1{
8+
let n1 = nums[i];
9+
10+
if i >0 && n1 == nums[i -1]{
11+
continue;
12+
}
13+
14+
let(mut l,mut r) =(i +1, nums.len() -1);
15+
16+
while l < r{
17+
let n2 = nums[l];
18+
let n3 = nums[r];
19+
20+
let sum = n1 + n2 + n3;
21+
22+
if sum >0{
23+
r-=1;
24+
}elseif sum <0{
25+
l+=1;
26+
}else{
27+
res.push(vec![n1, n2, n3]);
28+
29+
while l < r && n2 == nums[l +1]{
30+
l+=1;
31+
}
32+
l+=1;
33+
34+
while l < r && n3 == nums[r -1]{
35+
r-=1;
36+
}
37+
r-=1;
38+
}
39+
}
40+
}
41+
42+
res
43+
}
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
implSolution{
2+
pubfntwo_sum(numbers:Vec<i32>,target:i32) ->Vec<i32>{
3+
let(mut l,mut r) =(0, numbers.len() -1);
4+
5+
while l < r{
6+
let sum = numbers[l] + numbers[r];
7+
8+
if sum > target{
9+
r-=1;
10+
}elseif sum < target{
11+
l+=1;
12+
}else{
13+
returnvec![(l +1)asi32,(r +1)asi32];
14+
}
15+
}
16+
17+
unreachable!()
18+
}
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp