Skip to content

Commit 8e66950

Browse files
authored
Merge pull request #17 from jtr109/349-intersection-of-two-arrays
349 intersection of two arrays
2 parents 277bd0a + 709795d commit 8e66950

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ members = [
88
"two_sum",
99
"implement_queue_using_stacks",
1010
"merge_sorted_array",
11+
"intersection_of_two_arrays",
1112
]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Resolving problems of LeetCode in RustLang.
1414
* [145. Binary Tree Postorder Traversal](./binary_tree_postorder_traversal/src/lib.rs)
1515
* [215. Kth Largest Element in an Array](./kth_largest/src/lib.rs)
1616
* [232. Implement Queue using Stacks](./implement_queue_using_stacks/src/lib.rs)
17+
* [349. Intersection of Two Arrays](./intersection_of_two_arrays/src/lib.rs)
1718

1819
## Document
1920

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "intersection_of_two_arrays"
3+
version = "0.1.0"
4+
authors = ["Ryan Li <conbas2019@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*!
2+
* # 349. Intersection of Two Arrays
3+
*
4+
* * [Problem link](https://leetcode.com/problems/intersection-of-two-arrays/)
5+
*/
6+
7+
#![allow(dead_code)]
8+
9+
struct Solution {}
10+
11+
// ----------------------------------------------------------------------------
12+
13+
impl Solution {
14+
pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
15+
let mut sorted1 = nums1;
16+
let mut sorted2 = nums2;
17+
sorted1.sort();
18+
sorted2.sort();
19+
let (mut i, mut j) = (0, 0);
20+
let mut inter = vec![];
21+
while i < sorted1.len() && j < sorted2.len() {
22+
let n1 = sorted1[i];
23+
let n2 = sorted2[j];
24+
if n1 < n2 {
25+
i += 1;
26+
} else if n1 > n2 {
27+
j += 1;
28+
} else {
29+
inter.push(n1);
30+
i += 1;
31+
j += 1;
32+
while i < sorted1.len() && sorted1[i] == n1 {
33+
i += 1;
34+
}
35+
while j < sorted2.len() && sorted2[j] == n1 {
36+
j += 1;
37+
}
38+
}
39+
}
40+
inter
41+
}
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
use std::collections::HashSet;
48+
49+
fn assert_equal(left: Vec<i32>, right: Vec<i32>) {
50+
let l = left.iter().cloned().collect::<HashSet<i32>>();
51+
let r = right.iter().cloned().collect::<HashSet<i32>>();
52+
assert_eq!(l, r);
53+
}
54+
55+
#[test]
56+
fn test_example1() {
57+
let nums1 = vec![1, 2, 2, 1];
58+
let nums2 = vec![2, 2];
59+
let expected = vec![2];
60+
assert_equal(Solution::intersection(nums1, nums2), expected);
61+
}
62+
63+
#[test]
64+
fn test_example2() {
65+
let nums1 = vec![4, 9, 5];
66+
let nums2 = vec![9, 4, 9, 8, 4];
67+
let expected = vec![9, 4];
68+
assert_equal(Solution::intersection(nums1, nums2), expected);
69+
}
70+
}

0 commit comments

Comments
 (0)