05 August 2008
Implement ‘atoi’ which converts a string to an integer.
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:
1
' '
is considered as whitespace character.Example 1:
1
2
Input: "42"
Output: 42
Example 2:
1
2
3
4
Input: " -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
Then take as many numerical digits as possible, which gets 42.
Example 3:
1
2
3
Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
Example 4:
1
2
3
4
Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.
Example 5:
1
2
3
4
Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.
实现一个atoi函数(AscII to Interger),主要考虑各种边界条件,包括正负号,越界,空格,精度等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public int myAtoi(String s) {
int index = 0; // 指针从左到右
int len = s.length();
int sign = 1; // 标记正数或负数
int result = 0;
// Step 1: Skip leading whitespace characters
while (index < len && s.charAt(index) == ' ') {
index++;
}
// Step 2: Check for the optional sign (+ or -)
if (index < len && (s.charAt(index) == '+' || s.charAt(index) == '-')) {
sign = (s.charAt(index) == '+') ? 1 : -1;
index++;
}
// Step 3: Process the digits and convert them to an integer
while (index < len && Character.isDigit(s.charAt(index))) {
int digit = s.charAt(index) - '0'; // 字符转为数字
// Check for integer overflow
if (result > Integer.MAX_VALUE / 10 ||
(result == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10)) {
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + digit;
index++;
}
// Step 4: Apply the sign to the result and return it
return sign * result;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public int myAtoi(String s) {
int index = 0;
int total = 0;
int sign = 1;
// Check if the input string is empty
if(s == null || s.length() == 0) return 0;
// Remove whitespaces from the beginning of the string
while(index < s.length() && s.charAt(index) == ' ') {
index++;
}
// Check if the string is still valid after removing spaces
if (index == s.length()) return 0;
// Check if the current character is a '+' or '-' sign
if (s.charAt(index) == '+' || s.charAt(index) == '-') {
sign = s.charAt(index) == '+' ? 1 : -1;
index++;
}
// Convert number and avoid overflow
while (index < s.length()) {
int digit = s.charAt(index) - '0';
// Check if the current character is a digit
if (digit < 0 || digit > 9) break;
// Check for overflow conditions before multiplying by 10
if (Integer.MAX_VALUE / 10 < total ||
Integer.MAX_VALUE / 10 == total && Integer.MAX_VALUE % 10 < digit)
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
total = 10 * total + digit;
index++;
}
return total * sign;
}
}