2239. Find Closest Number to Zero

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2239

class Solution:
    def findClosestNumber(self, nums: List[int]) -> int:
        res, d = 0, inf
        for x in nums:
            if (y := abs(x)) < d or (y == d and x > res):
                res, d = x, y
        return res