字符串转整数
思路
思路很简单
第一步,想到,转整数用int()或者ASCII码-‘0’,然后 num = num * 10 + int(c)
就成
第二步,开始考虑边界情况,比如
- 为空或者为None,并且与0的区别
- 字符串含有’0’-'9’以外的字符
- 加减号
- 数字越界的情况
- 等等
这里大部分都可以if else来解决,不过在为空(‘’") 为None,并且与0的区别这一条这里,我们需要一个东西来区分是返回0还是返回一个“报错”。想到用全局变量控制,这样上面提到的情况都可以用这个全局变量来返回“错误”
第一版代码
第一版本的代码,默认是valid,但特殊情况很多,也就是invalid的情况很多,valid的情况反而很少,代码需要写出这样的三行或两行
global_status = Status.INVALID
num = 0
break
所以我因该直接设置为invalid作为初始,这样就少写点。
同时这个代码没有解决+ -的invliad问题,不过解决起来也很简单,因为这个问题出现在判断玩+ -号后,我们的s会被去掉第一个元素,这个时候可能后面是空的,所以我们需要先判断一下s是否存在 就像判断+ -前判断一下
if s
from typing import Callable
class Status:
VALID = 0
INVALID = 1
global_status = Status.VALID
class Solution:
def myAtoi(self, s: str) -> int:
global global_status
# 确保每次调用都初始化
# 因为我希望 global_status 每次调用 myAtoi 方法时都被重置为 Status.VALID
global_status = Status.VALID
# print(global_status)
flag = 1
num = 0
if s:
# Handle optional sign
if s[0] in ('+', '-'):
if s[0] == '-':
flag = -1
s = s[1:]
# Convert characters to integer
# '' None
else:
global_status = Status.INVALID
return num
for c in s:
if '0' <= c <= '9':
num = num * 10 + int(c)
if flag == 1 and num > 2**31 - 1:
global_status = Status.INVALID
num = 0
break
if flag == -1 and num > 2**31:
global_status = Status.INVALID
num = 0
break
else:
global_status = Status.INVALID
num = 0
break
return num * flag
def Test(string):
s = Solution()
result = s.myAtoi(string)
if result == 0 and global_status == Status.INVALID:
print("the input {} is invalid.".format(string))
else:
print("number for {} is: {}.".format(string, result))
def main():
Test(None)
Test("")
Test("123")
Test("+123")
Test("-123")
Test("1a33")
Test("+0")
Test("-0")
Test("+2147483647")
Test("-2147483647")
Test("+2147483648")
Test("-2147483648")
Test("+2147483649")
Test("-2147483649")
Test("+")
Test("-")
if __name__ == "__main__":
main()
the input None is invalid.
the input is invalid.
number for 123 is: 123.
number for +123 is: 123.
number for -123 is: -123.
the input 1a33 is invalid.
number for +0 is: 0.
number for -0 is: 0.
number for +2147483647 is: 2147483647.
number for -2147483647 is: -2147483647.
the input +2147483648 is invalid.
number for -2147483648 is: -2147483648.
the input +2147483649 is invalid.
the input -2147483649 is invalid.
number for + is: 0.
number for - is: 0.
最终版代码
当然,本质上区别不大。不过写错误的部分,直接return 0,然后不需要反复改Status状态了
from typing import Callable
class Status:
VALID = 0
INVALID = 1
global_status = Status.VALID
class Solution:
def myAtoi(self, s: str) -> int:
global global_status
# 确保每次调用都初始化
# 因为我希望 global_status 每次调用 myAtoi 方法时都被重置为 Status.VALID
global_status = Status.INVALID
# print(global_status)
flag = 1
num = 0
if s:
# Handle optional sign
if s[0] in ('+', '-'):
if s[0] == '-':
flag = -1
s = s[1:]
# Convert characters to integer
# '' None
else:
return num
# 或者 if s == '':
if not s:
return num
for c in s:
if '0' <= c <= '9':
num = num * 10 + int(c)
if flag == 1 and num > 2**31 - 1:
return 0
if flag == -1 and num > 2**31:
return 0
else:
return 0
global_status = Status.VALID
return num * flag
def Test(string):
s = Solution()
result = s.myAtoi(string)
if result == 0 and global_status == Status.INVALID:
print("the input {} is invalid.".format(string))
else:
print("number for {} is: {}.".format(string, result))
def main():
Test(None)
Test("")
Test("123")
Test("+123")
Test("-123")
Test("1a33")
Test("+0")
Test("-0")
Test("+2147483647")
Test("-2147483647")
Test("+2147483648")
Test("-2147483648")
Test("+2147483649")
Test("-2147483649")
Test("+")
Test("-")
if __name__ == "__main__":
main()