2239. Find Closest Number to Zero
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 47% Topics: Array
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# array
class Solution(object):
def findClosestNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return max(nums, key=lambda x:(-abs(x), x))
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions