Skip to content

Commit d90fea8

Browse files
authored
Create Find Closest Number to Zero.java
1 parent e9f848c commit d90fea8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Easy/Find Closest Number to Zero.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int findClosestNumber(int[] nums) {
3+
int[] result = {Integer.MAX_VALUE, Integer.MIN_VALUE};
4+
for (int num : nums) {
5+
int distance = Math.abs(num);
6+
if (distance <= result[0]) {
7+
if (distance < result[0]) {
8+
result[0] = distance;
9+
result[1] = num;
10+
} else {
11+
result[1] = Math.max(num, result[1]);
12+
}
13+
}
14+
}
15+
return result[1];
16+
}
17+
}

0 commit comments

Comments
 (0)