LeetCode 2239: Find Closest Number to Zero (Absolute Distance + Positive Tie-Break)
LeetCode 2239ArrayMathTie-BreakToday we solve LeetCode 2239 - Find Closest Number to Zero.
Source: https://leetcode.com/problems/find-closest-number-to-zero/
English
Problem Summary
Given an integer array nums, return the value whose absolute value is minimum. If two values are equally close to zero, return the positive one.
Key Insight
Track the current best answer while scanning once. For each number x:
- If |x| < |best|, update best = x.
- If |x| == |best|, update only when x > best (this guarantees positive wins in ties).
Algorithm
1) Initialize best = nums[0].
2) Iterate each x in nums and apply comparison rules.
3) Return best.
Complexity Analysis
Time: O(n).
Space: O(1).
Common Pitfalls
- Forgetting the tie-break requirement (must return the positive value).
- Sorting the array unnecessarily.
- Comparing raw values instead of absolute distances.
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int findClosestNumber(int[] nums) {
int best = nums[0];
for (int x : nums) {
int ax = Math.abs(x), ab = Math.abs(best);
if (ax < ab || (ax == ab && x > best)) {
best = x;
}
}
return best;
}
}func findClosestNumber(nums []int) int {
best := nums[0]
abs := func(x int) int {
if x < 0 {
return -x
}
return x
}
for _, x := range nums {
ax, ab := abs(x), abs(best)
if ax < ab || (ax == ab && x > best) {
best = x
}
}
return best
}class Solution {
public:
int findClosestNumber(vector<int>& nums) {
int best = nums[0];
for (int x : nums) {
int ax = abs(x), ab = abs(best);
if (ax < ab || (ax == ab && x > best)) {
best = x;
}
}
return best;
}
};class Solution:
def findClosestNumber(self, nums: List[int]) -> int:
best = nums[0]
for x in nums:
if abs(x) < abs(best) or (abs(x) == abs(best) and x > best):
best = x
return bestvar findClosestNumber = function(nums) {
let best = nums[0];
for (const x of nums) {
const ax = Math.abs(x), ab = Math.abs(best);
if (ax < ab || (ax === ab && x > best)) {
best = x;
}
}
return best;
};中文
题目概述
给定整数数组 nums,返回绝对值最小的数字;如果有两个数字到 0 的距离相同,返回较大的那个(即正数优先)。
核心思路
一次遍历维护当前最优答案 best。对每个 x:
- 若 |x| < |best|,直接更新。
- 若 |x| == |best|,仅当 x > best 时更新(保证同距离时选正数)。
算法步骤
1)初始化 best = nums[0]。
2)遍历数组并按规则比较更新。
3)返回 best。
复杂度分析
时间复杂度:O(n)。
空间复杂度:O(1)。
常见陷阱
- 忽略“距离相同取正数”的细节。
- 为了这个题去排序,导致多余的 O(n log n)。
- 使用原值比较而非绝对值比较。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int findClosestNumber(int[] nums) {
int best = nums[0];
for (int x : nums) {
int ax = Math.abs(x), ab = Math.abs(best);
if (ax < ab || (ax == ab && x > best)) {
best = x;
}
}
return best;
}
}func findClosestNumber(nums []int) int {
best := nums[0]
abs := func(x int) int {
if x < 0 {
return -x
}
return x
}
for _, x := range nums {
ax, ab := abs(x), abs(best)
if ax < ab || (ax == ab && x > best) {
best = x
}
}
return best
}class Solution {
public:
int findClosestNumber(vector<int>& nums) {
int best = nums[0];
for (int x : nums) {
int ax = abs(x), ab = abs(best);
if (ax < ab || (ax == ab && x > best)) {
best = x;
}
}
return best;
}
};class Solution:
def findClosestNumber(self, nums: List[int]) -> int:
best = nums[0]
for x in nums:
if abs(x) < abs(best) or (abs(x) == abs(best) and x > best):
best = x
return bestvar findClosestNumber = function(nums) {
let best = nums[0];
for (const x of nums) {
const ax = Math.abs(x), ab = Math.abs(best);
if (ax < ab || (ax === ab && x > best)) {
best = x;
}
}
return best;
};
Comments