Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
string-to-integer-atoi.py 802 Bytes
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-12 01:33 +08:00 . remove sensitive question description
# Time: O(n)
# Space: O(1)
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
INT_MAX = 2147483647
INT_MIN = -2147483648
result = 0
if not str:
return result
i = 0
while i < len(str) and str[i].isspace():
i += 1
if len(str) == i:
return result
sign = 1
if str[i] == "+":
i += 1
elif str[i] == "-":
sign = -1
i += 1
while i < len(str) and '0' <= str[i] <= '9':
if result > (INT_MAX - int(str[i])) / 10:
return INT_MAX if sign > 0 else INT_MIN
result = result * 10 + int(str[i])
i += 1
return sign * result
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助