8 String to Integer
Implement atoi to convert a string to an integer.
Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
首先得去掉leading space,然后把符号记录好。然后才开始算具体数字。这里算数字的方法跟calculater的差不多,如果看到不是数字的字符,我们不继续下去,把之前process好的部分返回就好了。如果我们遇到超出范围的数字时,我们就返回最大/最小值。感觉这个判断方法跟reverse Integer的有点相似。T:O(n),S:O(1)
Last updated