(self, s: str)
| 1 | class 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 |