2022-04-24 17:05:32 +08:00
< p > Given an integer array < code > nums< / code > of size < code > n< / code > , return < em > the number with the value < strong > closest< / strong > to < / em > < code > 0< / code > < em > in < / em > < code > nums< / code > . If there are multiple answers, return < em > the number with the < strong > largest< / strong > value< / em > .< / p >
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-04-24 17:05:32 +08:00
< pre >
< strong > Input:< / strong > nums = [-4,-2,1,4,8]
< strong > Output:< / strong > 1
< strong > Explanation:< / strong >
The distance from -4 to 0 is |-4| = 4.
The distance from -2 to 0 is |-2| = 2.
The distance from 1 to 0 is |1| = 1.
The distance from 4 to 0 is |4| = 4.
The distance from 8 to 0 is |8| = 8.
Thus, the closest number to 0 in the array is 1.
< / pre >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-04-24 17:05:32 +08:00
< pre >
< strong > Input:< / strong > nums = [2,-1,1]
< strong > Output:< / strong > 1
< strong > Explanation:< / strong > 1 and -1 are both the closest numbers to 0, so 1 being larger is returned.
< / pre >
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = n < = 1000< / code > < / li >
< li > < code > -10< sup > 5< / sup > < = nums[i] < = 10< sup > 5< / sup > < / code > < / li >
< / ul >