File tree Expand file tree Collapse file tree 3 files changed +133
-0
lines changed
solution/1800-1899/1865.Finding Pairs With a Certain Sum Expand file tree Collapse file tree 3 files changed +133
-0
lines changed Original file line number Diff line number Diff line change @@ -273,6 +273,52 @@ class FindSumPairs {
273273 */
274274```
275275
276+ #### Rust
277+
278+ ``` rust
279+ use std :: collections :: HashMap ;
280+
281+ struct FindSumPairs {
282+ nums1 : Vec <i32 >,
283+ nums2 : Vec <i32 >,
284+ cnt : HashMap <i32 , i32 >,
285+ }
286+
287+ impl FindSumPairs {
288+ fn new (nums1 : Vec <i32 >, nums2 : Vec <i32 >) -> Self {
289+ let mut cnt = HashMap :: new ();
290+ for & x in & nums2 {
291+ * cnt . entry (x ). or_insert (0 ) += 1 ;
292+ }
293+ Self { nums1 , nums2 , cnt }
294+ }
295+
296+ fn add (& mut self , index : i32 , val : i32 ) {
297+ let i = index as usize ;
298+ let old_val = self . nums2[i ];
299+ * self . cnt. entry (old_val ). or_insert (0 ) -= 1 ;
300+ if self . cnt[& old_val ] == 0 {
301+ self . cnt. remove (& old_val );
302+ }
303+
304+ self . nums2[i ] += val ;
305+ let new_val = self . nums2[i ];
306+ * self . cnt. entry (new_val ). or_insert (0 ) += 1 ;
307+ }
308+
309+ fn count (& self , tot : i32 ) -> i32 {
310+ let mut ans = 0 ;
311+ for & x in & self . nums1 {
312+ let target = tot - x ;
313+ if let Some (& c ) = self . cnt. get (& target ) {
314+ ans += c ;
315+ }
316+ }
317+ ans
318+ }
319+ }
320+ ```
321+
276322#### JavaScript
277323
278324``` js
Original file line number Diff line number Diff line change @@ -271,6 +271,52 @@ class FindSumPairs {
271271 */
272272```
273273
274+ #### Rust
275+
276+ ``` rust
277+ use std :: collections :: HashMap ;
278+
279+ struct FindSumPairs {
280+ nums1 : Vec <i32 >,
281+ nums2 : Vec <i32 >,
282+ cnt : HashMap <i32 , i32 >,
283+ }
284+
285+ impl FindSumPairs {
286+ fn new (nums1 : Vec <i32 >, nums2 : Vec <i32 >) -> Self {
287+ let mut cnt = HashMap :: new ();
288+ for & x in & nums2 {
289+ * cnt . entry (x ). or_insert (0 ) += 1 ;
290+ }
291+ Self { nums1 , nums2 , cnt }
292+ }
293+
294+ fn add (& mut self , index : i32 , val : i32 ) {
295+ let i = index as usize ;
296+ let old_val = self . nums2[i ];
297+ * self . cnt. entry (old_val ). or_insert (0 ) -= 1 ;
298+ if self . cnt[& old_val ] == 0 {
299+ self . cnt. remove (& old_val );
300+ }
301+
302+ self . nums2[i ] += val ;
303+ let new_val = self . nums2[i ];
304+ * self . cnt. entry (new_val ). or_insert (0 ) += 1 ;
305+ }
306+
307+ fn count (& self , tot : i32 ) -> i32 {
308+ let mut ans = 0 ;
309+ for & x in & self . nums1 {
310+ let target = tot - x ;
311+ if let Some (& c ) = self . cnt. get (& target ) {
312+ ans += c ;
313+ }
314+ }
315+ ans
316+ }
317+ }
318+ ```
319+
274320#### JavaScript
275321
276322``` js
Original file line number Diff line number Diff line change 1+ use std:: collections:: HashMap ;
2+
3+ struct FindSumPairs {
4+ nums1 : Vec < i32 > ,
5+ nums2 : Vec < i32 > ,
6+ cnt : HashMap < i32 , i32 > ,
7+ }
8+
9+ impl FindSumPairs {
10+ fn new ( nums1 : Vec < i32 > , nums2 : Vec < i32 > ) -> Self {
11+ let mut cnt = HashMap :: new ( ) ;
12+ for & x in & nums2 {
13+ * cnt. entry ( x) . or_insert ( 0 ) += 1 ;
14+ }
15+ Self { nums1, nums2, cnt }
16+ }
17+
18+ fn add ( & mut self , index : i32 , val : i32 ) {
19+ let i = index as usize ;
20+ let old_val = self . nums2 [ i] ;
21+ * self . cnt . entry ( old_val) . or_insert ( 0 ) -= 1 ;
22+ if self . cnt [ & old_val] == 0 {
23+ self . cnt . remove ( & old_val) ;
24+ }
25+
26+ self . nums2 [ i] += val;
27+ let new_val = self . nums2 [ i] ;
28+ * self . cnt . entry ( new_val) . or_insert ( 0 ) += 1 ;
29+ }
30+
31+ fn count ( & self , tot : i32 ) -> i32 {
32+ let mut ans = 0 ;
33+ for & x in & self . nums1 {
34+ let target = tot - x;
35+ if let Some ( & c) = self . cnt . get ( & target) {
36+ ans += c;
37+ }
38+ }
39+ ans
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments