Ivan's Cool Obsidian Vault

Home

❯

006 Main Notes

❯

Leetcode

❯

Arrays and Hashing

❯

Find Closest Number to Zero

Find Closest Number to Zero

Sep 15, 20251 min read

  • child
  • arrays
  • strings
  • leetcode
  • leetcode-easy

2025-08-11 13:51

Status: child

Tags: arrays strings leetcode leetcode-easy Leetcode

Find Closest Number to Zero

Code

class Solution(object):
    def findClosestNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        closest = nums[0]
        
        for x in nums:
            if abs(x) < abs(closest):
                closest = x
        
        if closest < 0 and abs(closest) in nums:
            return abs(closest)
        else:
            return closest

References

AlgoMap


Graph View

  • Find Closest Number to Zero
  • Code
  • References

Created with Quartz v4.5.1 © 2025

  • GitHub
  • Discord Community