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.
public class Solution {
public int atoi(String str) {
if (str == null || str.length() < 1) {
return 0;
}
str = str.trim();
if(str.isEmpty()) {
return 0;
}
boolean neg = false;
if(str.charAt(0)=='-') {
neg = true;
str = str.substring(1);
} else if(str.charAt(0)=='+') {
str = str.substring(1);
}
double result = 0;
int i = 0;
while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
result = result * 10 + (str.charAt(i) - '0');
i++;
}
if (neg)
result = -result;
if (result > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (result < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
return (int) result;
}
}
==========
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() == 0) return 0;
int i = 0;
int n = str.length();
int sign = 1;
while (i < n && Character.isWhitespace(str.charAt(i))) {
i++;
}
if (str.charAt(i) == '-') {
i++;
sign = -1;
} else if (str.charAt(i) == '+') {
sign = 1;
i++;
}
int total = 0;
while (i < n) {
int digit = str.charAt(i) - '0';
if (digit < 0 || digit >= 10) {
break;
}
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 = total * 10 + digit;
i++;
}
return total * sign;
}
}
=============
public class Solution {
public int myAtoi(String str) {
if (str == null) {
return 0;
}
int res = 0;
int sign = 1;
str = str.trim();
int i = 0;
int n = str.length();
if (i < n && (str.charAt(i) == '+' || str.charAt(i) == '-')) {
if (str.charAt(i) == '-') sign = -1;
i++;
}
while (i < n && Character.isDigit(str.charAt(i))) {
int digit = str.charAt(i) - '0';
if (Integer.MAX_VALUE / 10 < res || (Integer.MAX_VALUE / 10 == res && Integer.MAX_VALUE % 10 < digit)) {
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
res *= 10;
res += digit;
i++;
}
return res * sign;
}
}
没有评论:
发表评论