0%

## Problem

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.

Note:

Only the space character ‘ ‘ is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: $$[−2^{31}, 2^{31} − 1]$$. If the numerical value is out of the range of representable values, INT_MAX $$2^{31}-1$$ or INT_MIN $$-2^{31}$$ is returned.

### Examples:

Input:“42”
Output:**42
**Input:
“ -42”
Output:-42
Input:“words and 987”
**Output:**987

## Solutions

• 没啥思维量，就是简单的遍历每个字符，然后设置两个flag，一个用于判断有没有到数字部分，第二个判断是不是负数，如果没到数字部分，就要分情况讨论，如果到了数字部分，判断到没到非数字，到了就退出，没到就每个数字加到结果上

## 总结

• 要注意各种情况的判断
• 就算用了long long作为中转，在循环中也要判断是否已经超过int表示范围，提前break，可能出现longlong都无法表示的测试
• 注意空串
• 注意前面的符号
• 不应该进行的操作及时continue掉
• 还是会出现’+’号的