本帖最后由 Miss_Allsunday 于 2017-6-17 08:24 编辑
8. String to Integer (atoi)
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. Requirements for atoi: 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.
实现 atoi函数,把一个字符串转换成整数。
提示:注意考虑到所有的输入可能。如果你想要一个挑战,请不要看下面的要求并且自己想什么是可能的输入情况。
atoi的要求:
这函数首先丢弃掉所有的空白字符直到找到第一个非空字符。然后从这个字符开始,取一个可选的正号或者负号以及后面接着的尽可能多的数字,并且用数字值的形式解释它们。
这个字符串可能包含额外的字符在这些数字后面,这些需要被忽视并且对函数不造成影响。
如果第一序列的非空字符是字符串并不是合格的整数,或者假如没有合格的整数存在由于字符串是空的或者里面仅仅包含空白符,那么不实现任何转换。
如果没有合格的转换可以实现,一个0的值被返回。如果正确的值超过了可展现的整数范围,那么INT_MAX (2147483647) 或者INT_MIN (-2147483648) 被返回。
你需要完成以下的函数:int myAtoi(char* str) {
}
这道题目网址是:“https://leetcode.com/problems/string-to-integer-atoi/#/description”
|