Class Solution
java.lang.Object
g2201_2300.s2239_find_closest_number_to_zero.Solution
2239 - Find Closest Number to Zero.<p>Easy</p>
<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><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [-4,-2,1,4,8]</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The distance from -4 to 0 is |-4| = 4.</p>
<p>The distance from -2 to 0 is |-2| = 2.</p>
<p>The distance from 1 to 0 is |1| = 1. The distance from 4 to 0 is |4| = 4.</p>
<p>The distance from 8 to 0 is |8| = 8.</p>
<p>Thus, the closest number to 0 in the array is 1.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [2,-1,1]</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> 1 and -1 are both the closest numbers to 0, so 1 being larger is returned.</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>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
findClosestNumber
public int findClosestNumber(int[] nums)
-