MCPcopy Create free account
hub / github.com/Hsinha11/Leetcode-solutions / myAtoi

Method myAtoi

8-String-to-Integer/string-to-integer.py:2–24  ·  view source on GitHub ↗
(self, s: str)

Source from the content-addressed store, hash-verified

1class Solution:
2 def myAtoi(self, s: str) -> int:
3 i, n = 0, len(s)
4 f = 1
5 num = 0
6
7 while i < n and s[i] == ' ':
8 i += 1
9
10 if i < n and s[i] == '-':
11 f = -1
12 i += 1
13 elif i < n and s[i] == '+':
14 i += 1
15
16 while i < n and s[i].isdigit():
17 num = num * 10 + int(s[i])
18 if num > 2**31 - 1 and f == 1:
19 return 2**31 - 1
20 elif num > 2**31 and f == -1:
21 return -2**31
22 i += 1
23
24 return f * num

Callers

nothing calls this directly

Calls 1

isdigitMethod · 0.80

Tested by

no test coverage detected