|
1 | 1 | # -*- coding:utf-8 -*-
|
2 | 2 |
|
3 | 3 |
|
4 |
| -# Implement atoi which converts a string to an integer. |
| 4 | +# Implement atoi which converts a string to an integer. |
5 | 5 | #
|
6 | 6 | # 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.
|
7 | 7 | #
|
|
12 | 12 | # If no valid conversion could be performed, a zero value is returned.
|
13 | 13 | #
|
14 | 14 | # Note:
|
15 |
| -# Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. |
| 15 | +# Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned. |
16 | 16 | #
|
17 | 17 | # Example 1:
|
18 | 18 | #
|
19 | 19 | #
|
20 |
| -# Input: "42" |
| 20 | +# Input: "42" |
21 | 21 | # Output: 42
|
22 | 22 | #
|
23 | 23 | #
|
24 | 24 | # Example 2:
|
25 | 25 | #
|
26 | 26 | #
|
27 |
| -# Input: " -42" |
| 27 | +# Input: " -42" |
28 | 28 | # Output: -42
|
29 |
| -# Explanation: The first non-whitespace character is '-', which is the minus sign. |
30 |
| -# Then take as many numerical digits as possible, which gets 42. |
| 29 | +# Explanation: The first non-whitespace character is '-', which is the minus sign. |
| 30 | +# Then take as many numerical digits as possible, which gets 42. |
31 | 31 | #
|
32 | 32 | #
|
33 | 33 | # Example 3:
|
34 | 34 | #
|
35 | 35 | #
|
36 |
| -# Input: "4193 with words" |
| 36 | +# Input: "4193 with words" |
37 | 37 | # Output: 4193
|
38 |
| -# Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. |
| 38 | +# Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. |
39 | 39 | #
|
40 | 40 | #
|
41 | 41 | # Example 4:
|
42 | 42 | #
|
43 | 43 | #
|
44 |
| -# Input: "words and 987" |
| 44 | +# Input: "words and 987" |
45 | 45 | # Output: 0
|
46 |
| -# Explanation: The first non-whitespace character is 'w', which is not a numerical |
47 |
| -# digit or a +/- sign. Therefore no valid conversion could be performed. |
| 46 | +# Explanation: The first non-whitespace character is 'w', which is not a numerical |
| 47 | +# digit or a +/- sign. Therefore no valid conversion could be performed. |
48 | 48 | #
|
49 | 49 | # Example 5:
|
50 | 50 | #
|
51 | 51 | #
|
52 |
| -# Input: "-91283472332" |
| 52 | +# Input: "-91283472332" |
53 | 53 | # Output: -2147483648
|
54 |
| -# Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. |
55 |
| -# Thefore INT_MIN (−231) is returned. |
| 54 | +# Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. |
| 55 | +# Thefore INT_MIN (−231) is returned. |
56 | 56 | #
|
57 | 57 |
|
58 | 58 |
|
|
0 commit comments