Friday, January 8, 2021

LeetCode: String to Integer (atoi)

 The problem statement is copy pasted from Leetcode as it is:


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:

  • Only the space character ' ' is considered a whitespace character.
  • Assume we are dealing with an environment that 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, 231 − 1 or −231 is returned.

My solution:

package madwani.sushil.leetcode.Amazon;

public class StringToInteger_ATOI_Solution {
public static void main(String[] args) {
System.out.println(myAtoi("21474836460")); // 2147483647
System.out.println(myAtoi(" 12")); // 12
System.out.println(myAtoi("-21474836460")); // -2147483647
System.out.println(myAtoi("-12")); //12
System.out.println(myAtoi("4193 with words")); //4193
System.out.println(myAtoi("words and 987")); // 0
}

public static int myAtoi(String s) {
if ( s== null || s.trim().length() == 0)
return 0;

int sign = 1;
int result = 0;
int start_index = 0;
s = s.trim();
if (s.startsWith("+") || s.startsWith("-")) {
sign = s.startsWith("+") ? 1 : -1;
start_index++;
}
while (start_index < s.length() && Character.isDigit(s.charAt(start_index))) {
/*
If the integer is positive, for 32 bit signed integer,
INT_MAX is 2147483647 (2^{31}-1) = 2147483647
To avoid integer overflow, we must ensure that it doesn't exceed this value.
This condition needs to be handled when the result is greater than or equal to INT_MAX/10 = 214748364

Case 1).If result = INT_MAX/10
​ It would result in integer overflow if next integer character is greater than 7.
7 in this case is last digit of INT_MAX 2147483647. We can use INT_MAX%10 to generically represent the last digit.

Case 2).If result > INT_MAX/10
​ we are sure that adding next number would result in integer overflow.

This holds for negative integer as well. In the 32-bit integer, INT_MIN value is -2147483648 (-2^31)
As the last digit is greater than 7, integer underflow can also be handled using the above approach.
*/
if ( (Integer.MAX_VALUE/10 < result) || (
Integer.MAX_VALUE/10 == result && Character.getNumericValue(s.charAt(start_index)) > Integer.MAX_VALUE%10)) {
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + Character.getNumericValue(s.charAt(start_index++));
}
return result*sign;
}
}

No comments: