Skip to content

feat: add rust solution to lc problem: No.1865 #4550

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 46 additions & 0 deletions solution/1800-1899/1865.Finding Pairs With a Certain Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,52 @@ class FindSumPairs {
*/
```

#### Rust

```rust
use std::collections::HashMap;

struct FindSumPairs {
nums1: Vec<i32>,
nums2: Vec<i32>,
cnt: HashMap<i32, i32>,
}

impl FindSumPairs {
fn new(nums1: Vec<i32>, nums2: Vec<i32>) -> Self {
let mut cnt = HashMap::new();
for &x in &nums2 {
*cnt.entry(x).or_insert(0) += 1;
}
Self { nums1, nums2, cnt }
}

fn add(&mut self, index: i32, val: i32) {
let i = index as usize;
let old_val = self.nums2[i];
*self.cnt.entry(old_val).or_insert(0) -= 1;
if self.cnt[&old_val] == 0 {
self.cnt.remove(&old_val);
}

self.nums2[i] += val;
let new_val = self.nums2[i];
*self.cnt.entry(new_val).or_insert(0) += 1;
}

fn count(&self, tot: i32) -> i32 {
let mut ans = 0;
for &x in &self.nums1 {
let target = tot - x;
if let Some(&c) = self.cnt.get(&target) {
ans += c;
}
}
ans
}
}
```

#### JavaScript

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,52 @@ class FindSumPairs {
*/
```

#### Rust

```rust
use std::collections::HashMap;

struct FindSumPairs {
nums1: Vec<i32>,
nums2: Vec<i32>,
cnt: HashMap<i32, i32>,
}

impl FindSumPairs {
fn new(nums1: Vec<i32>, nums2: Vec<i32>) -> Self {
let mut cnt = HashMap::new();
for &x in &nums2 {
*cnt.entry(x).or_insert(0) += 1;
}
Self { nums1, nums2, cnt }
}

fn add(&mut self, index: i32, val: i32) {
let i = index as usize;
let old_val = self.nums2[i];
*self.cnt.entry(old_val).or_insert(0) -= 1;
if self.cnt[&old_val] == 0 {
self.cnt.remove(&old_val);
}

self.nums2[i] += val;
let new_val = self.nums2[i];
*self.cnt.entry(new_val).or_insert(0) += 1;
}

fn count(&self, tot: i32) -> i32 {
let mut ans = 0;
for &x in &self.nums1 {
let target = tot - x;
if let Some(&c) = self.cnt.get(&target) {
ans += c;
}
}
ans
}
}
```

#### JavaScript

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::collections::HashMap;

struct FindSumPairs {
nums1: Vec<i32>,
nums2: Vec<i32>,
cnt: HashMap<i32, i32>,
}

impl FindSumPairs {
fn new(nums1: Vec<i32>, nums2: Vec<i32>) -> Self {
let mut cnt = HashMap::new();
for &x in &nums2 {
*cnt.entry(x).or_insert(0) += 1;
}
Self { nums1, nums2, cnt }
}

fn add(&mut self, index: i32, val: i32) {
let i = index as usize;
let old_val = self.nums2[i];
*self.cnt.entry(old_val).or_insert(0) -= 1;
if self.cnt[&old_val] == 0 {
self.cnt.remove(&old_val);
}

self.nums2[i] += val;
let new_val = self.nums2[i];
*self.cnt.entry(new_val).or_insert(0) += 1;
}

fn count(&self, tot: i32) -> i32 {
let mut ans = 0;
for &x in &self.nums1 {
let target = tot - x;
if let Some(&c) = self.cnt.get(&target) {
ans += c;
}
}
ans
}
}