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

Rust: Two Pointers (two_sum, 3sum))#501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tomijaga merged 1 commit intoneetcode-gh:mainfromtomijaga:main
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletionsrust/01_arrays_&_hashing/217_contains_duplicates.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,15 +4,15 @@ impl Solution {
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
let mut map = HashSet::new();

for n in nums.iter(){
for&n in nums.iter(){

if map.contains(n){
if map.contains(&n){
return true;
}

map.insert(n);
};

false
false
}
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,7 +11,7 @@ impl Solution {

let mut freq: Vec<(i32, i32)> = map.into_iter().collect();

let res = if k ==arr.len() as i32{
let res = if k ==freq.len() as i32{
&freq
}else{
quick_select(&mut freq, k)
Expand Down
6 changes: 3 additions & 3 deletionsrust/01_arrays_&_hashing/49_group_anagrams.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,10 @@ use std::collections::HashMap;

impl Solution {
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
let mut map: HashMap<[u16; 26], Vec<String>> = HashMap::new();
let mut map: HashMap<[u8; 26], Vec<String>> = HashMap::new();

for s in strs{
let mut key = [0_u16; 26];
let mut key = [0_u8; 26];

for c in s.chars(){
key[c as usize - 'a' as usize] += 1;
Expand All@@ -18,6 +18,6 @@ impl Solution {
}
}

map.into_values().collect::<Vec<Vec<String>>>()
map.into_values().collect()
}
}
2 changes: 1 addition & 1 deletionrust/02_two_pointers/11_container_with_most_water.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,7 +15,7 @@ impl Solution {
max = area;
}

if rh< lh{
if rh< lh{
while r > 0 && height[r] <= rh{
r-=1;
}
Expand Down
44 changes: 44 additions & 0 deletionsrust/02_two_pointers/15_3sum.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
impl Solution {
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
nums.sort_unstable();

let mut res = vec![];

for i in 0..nums.len() - 1{
let n1 = nums[i];

if i > 0 && n1 == nums[i - 1]{
continue;
}

let (mut l, mut r) = (i + 1, nums.len() - 1);

while l < r{
let n2 = nums[l];
let n3 = nums[r];

let sum = n1 + n2 + n3;

if sum > 0{
r-=1;
}else if sum < 0{
l+=1;
}else{
res.push(vec![n1, n2, n3]);

while l < r && n2 == nums[l + 1]{
l+=1;
}
l+=1;

while l < r && n3 == nums[r - 1]{
r-=1;
}
r-=1;
}
}
}

res
}
}
19 changes: 19 additions & 0 deletionsrust/02_two_pointers/167_two_sum_II.rs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
impl Solution {
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
let (mut l, mut r) = (0, numbers.len() - 1);

while l < r{
let sum = numbers[l] + numbers[r];

if sum > target{
r-=1;
}else if sum < target{
l+=1;
}else{
return vec![(l + 1) as i32, (r + 1) as i32];
}
}

unreachable!()
}
}

[8]ページ先頭

©2009-2025 Movatter.jp