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.
Solution1:
1. null or empty string 2. white spaces 3. +/- sign 4. handle min and max 5. calculate real valueRun time complexity is O(n).
public class Solution { public int atoi(String str) { if(str == null || str.length() == 0) return 0; //deal with string only contains space str = str.trim(); if(str.length() == 0) return 0; //deal with sign boolean isNeg = false; int i = 0; if(str.charAt(0) == '-' || str.charAt(0) == '+') { i++; if(str.charAt(0) == '-') { isNeg = true; } } double res = 0; while(i < str.length()) { if(str.charAt(i) < '0' || str.charAt(i) > '9') { //deal with special characters break; } double digit = str.charAt(i) - '0'; if(isNeg && res > (-(Integer.MIN_VALUE + digit) / 10)) //deal with MIN_VALUE return Integer.MIN_VALUE; if(!isNeg && res > ((Integer.MAX_VALUE - digit) / 10)) //deal with MAX_VALUE return Integer.MAX_VALUE; res = res * 10 + digit; //calculate value i++; } if(isNeg) return (int)-res; return (int)res; } }
Solution2:
In each step we are appending a digit to the number by doing a multiplication and addition. If the current number is greater than 214748364, we know it is going to overflow. On the other hand, if the current number is equal to 214748364, we know that it will overflow only when the current digit is greater than or equal to 8. Remember to also consider edge case for the smallest number, –2147483648 (–231).
public class Solution { private static final int maxDiv10 = Integer.MAX_VALUE / 10; public int atoi(String str) { int i = 0, n = str.length(); while(i < n && Character.isWhitespace(str.charAt(i))) { i++; } int sign = 1; if(i < n && str.charAt(i) == '-') { sign = -1; i++; }else if(i < n && str.charAt(i) == '+') { i++; } int result = 0; while(i < n && Character.isDigit(str.charAt(i))) { int digit = Character.getNumericValue(str.charAt(i)); if(result > maxDiv10 || result == maxDiv10 && digit >= 8) { return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; } result = result * 10 + digit; i++; } return result * sign; } }
No comments:
Post a Comment