-
Notifications
You must be signed in to change notification settings - Fork 0
/
contains_duplicate.rs
114 lines (98 loc) · 2.11 KB
/
contains_duplicate.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#![allow(dead_code)]
/*
#217 Contains Duplicate
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Constraints:
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
*/
use std::collections::HashSet;
/*
Naive solution
T: O(N^2)
S: O(1)
*/
pub fn solution_a<N>(nums: N) -> bool
where
N: IntoIterator<Item = i32> + Copy,
{
for (i, x) in nums.into_iter().enumerate() {
for (j, y) in nums.into_iter().enumerate() {
if i == j {
continue;
}
if x == y {
return true;
}
}
}
false
}
/*
Set solution
T: O(N)
S: O(N)
*/
pub fn solution_b<N>(nums: N) -> bool
where
N: IntoIterator<Item = i32> + Copy,
{
let mut set: HashSet<i32> = HashSet::new();
for i in nums.into_iter() {
if !set.insert(i) {
return true;
}
}
false
}
/*
Sorted solution in place
T: O(NlogN)
S: O(1)
*/
pub fn solution_c<N>(_nums: N) -> bool
where
N: IntoIterator<Item = i32> + Copy,
{
todo!();
}
/*
Sorted solution cloned
T: O(NlogN)
S: O(N) -> if unable to sort in place
*/
pub fn solution_d<N>(_nums: &N) -> bool
where
N: IntoIterator<Item = i32> + Copy,
{
todo!();
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::TestCase;
const TC_0: TestCase<[i32; 4], bool> = TestCase {
input: [1, 2, 3, 1],
expected: true,
};
const TC_1: TestCase<[i32; 4], bool> = TestCase {
input: [1, 2, 3, 4],
expected: false,
};
const TC_2: TestCase<[i32; 10], bool> = TestCase {
input: [1, 1, 1, 3, 3, 4, 3, 2, 4, 2],
expected: true,
};
#[test]
fn a() {
assert_eq!(solution_a(TC_0.input), TC_0.expected);
assert_eq!(solution_a(TC_1.input), TC_1.expected);
assert_eq!(solution_a(TC_2.input), TC_2.expected);
}
#[test]
fn b() {
assert_eq!(solution_b(TC_0.input), TC_0.expected);
assert_eq!(solution_b(TC_1.input), TC_1.expected);
assert_eq!(solution_b(TC_2.input), TC_2.expected);
}
}