String to Integer (atoi)
Question
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.
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.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
Answer
solution:
Knowledge:
这道题目预想到了思考部分的空间(原位操作)及时间(单指针),但是在编程环节,没想出怎么叠加数字以及遇到任何怪字符就跳开的方法,方法为循环跳出:在for循环中加入break,一旦遇到不符合条件的就结束循环:
python中去除string中空格的方法有两种:第一种去除首末空格,用a.strip();第二种去除所有空格,用a.replace(" ","")。
python中int有一定的范围,超出其表达范围会溢出。为了便于操作,直接调用sys.maxint或sys.maxint * -1(表示负数)即可,也可以直接设置(1 << 31) - 1,不用管是32位还是64位。
python中左移运算符<<:
python中十进制转换二进制的方法如下,
0b
是表示是二进制的意思,以用于查看十进制的二进制是多少:
Last updated
Was this helpful?