Skip to content

Commit 7f79dab

Browse files
authored
Create Find Closest Number to Zero - Leetcode 2239.py
1 parent b161479 commit 7f79dab

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findClosestNumber(self, nums: List[int]) -> int:
3+
closest = nums[0]
4+
for x in nums:
5+
if abs(x) < abs(closest):
6+
closest = x
7+
8+
if closest < 0 and abs(closest) in nums:
9+
return abs(closest)
10+
else:
11+
return closest
12+
13+
# Time: O(n)
14+
# Space: O(1)

0 commit comments

Comments
 (0)