-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
63 lines (59 loc) · 1.56 KB
/
main.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
fn main() {
Solution::three_sum_closest(vec![-1, 2, 1, -4], 1);
}
struct Solution {}
impl Solution {
pub fn three_sum_closest(nums: Vec<i32>, target: i32) -> i32 {
let mut nums = nums.clone();
nums.sort();
let mut ret = nums[0] + nums[1] + nums[2];
let mut diff = i32::abs(ret - target);
for i in 0..nums.len()-2 {
let mut l = i + 1;
let mut r = nums.len() - 1;
while l < r {
let (a, b, c) = (nums[i], nums[l], nums[r]);
let sum = a + b + c;
if i32::abs(sum - target) < diff {
diff = i32::abs(sum - target);
ret = sum;
}
if a + b + c < target {
l += 1;
} else if a + b + c > target {
r -= 1;
} else {
return target;
}
}
}
ret
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(
Solution::three_sum_closest(vec![-1, 2, 1, -4], 1),
2
);
assert_eq!(
Solution::three_sum_closest(vec![-1, 2, 1, -4], 2),
2
);
assert_eq!(
Solution::three_sum_closest(vec![-1, 2, 1, -4], 3),
3
);
assert_eq!(
Solution::three_sum_closest(vec![1, 1, 1, 1], 5),
3
);
assert_eq!(
Solution::three_sum_closest(vec![1, 1, 1], 5),
3
);
}
}