-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring-to-integer-atoi.py
More file actions
41 lines (34 loc) · 988 Bytes
/
string-to-integer-atoi.py
File metadata and controls
41 lines (34 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Leetcode 8. String to Integer (atoi)
#
# Link: https://leetcode.com/problems/string-to-integer-atoi/
# Difficulty: Medium
# Complexity:
# O(N^2) time | where N represent the number of elements in the input array
# O(1) space
class Solution:
def myAtoi(self, s: str) -> int:
MAX_INT = 2 ** 31 - 1
MIN_INT = -2 ** 31
i = 0
result = 0
sign = 1
# Check whitespaces
while i < len(s) and s[i] == ' ':
i += 1
# Check for +/- symbols
if i < len(s) and s[i] == '-':
i += 1
sign = -1
elif i < len(s) and s[i] == '+':
i += 1
# Check digits
checker = set('0123456789')
while i < len(s) and s[i] in checker:
result = result * 10 + int(s[i])
i += 1
# Check the MIN/MAX
result = sign * result
if result < 0:
return max(result, MIN_INT)
else:
return min(result, MAX_INT)