-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindClosestNumberToZero.java
55 lines (52 loc) · 1.38 KB
/
FindClosestNumberToZero.java
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
/*https://leetcode.com/problems/find-closest-number-to-zero/*/
class Solution {
public int findClosestNumber(int[] nums) {
int i = 0, n = nums.length;
Arrays.sort(nums);
for (; i < n; ++i)
if (nums[i] > 0) break;
int neg = i-1;
if (neg == -1) return nums[0];
if (i == n) return nums[n-1];
return nums[i] > -nums[i-1] ? nums[i-1] : nums[i];
}
}
class Solution {
public int findClosestNumber(int[] nums) {
int n = nums.length, i = n;
Arrays.sort(nums);
int low = 0, high = n-1, mid;
while (low <= high)
{
mid = low+(high-low)/2;
if (nums[mid] > 0)
{
i = mid;
high = mid-1;
}
else low = mid+1;
}
int neg = i-1;
if (neg == -1) return nums[0];
if (i == n) return nums[n-1];
return nums[i] > -nums[i-1] ? nums[i-1] : nums[i];
}
}
class Solution {
public int findClosestNumber(int[] nums) {
int minDiff = Integer.MAX_VALUE;
int ans = 0;
for (int x: nums)
{
int diff = Math.abs(0-x);
if (diff < minDiff)
{
minDiff = diff;
ans = x;
}
else if (diff == minDiff)
ans = Math.max(ans,x);
}
return ans;
}
}