Skip to content

Commit b2356a5

Browse files
committed
Create Find Closest Number to Zero - Leetcode 2239.rs
1 parent ee6f841 commit b2356a5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fn abs(x: i32) -> i32 {
2+
if x < 0 {
3+
return -x;
4+
}
5+
x
6+
}
7+
8+
fn find_closest(nums: Vec<i32>) -> i32 {
9+
let mut closest = nums[0];
10+
for i in 0..nums.len() {
11+
if abs(nums[i]) < abs(closest) {
12+
closest = nums[i];
13+
} else if abs(nums[i]) == abs(closest) && nums[i] > closest {
14+
closest = nums[i];
15+
}
16+
}
17+
closest
18+
}

0 commit comments

Comments
 (0)