|
1 |
| -// Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer |
2 |
| -// (similar to C/C++'s atoi function). Here is my implementation |
3 |
| - |
4 | 1 | package com.thealgorithms.strings;
|
5 | 2 |
|
| 3 | +/** |
| 4 | + * A utility class that provides a method to convert a string to a 32-bit signed integer (similar to C/C++'s atoi function). |
| 5 | + */ |
6 | 6 | public final class MyAtoi {
|
7 | 7 | private MyAtoi() {
|
8 | 8 | }
|
9 |
| - public static int myAtoi(String s) { |
10 |
| - s = s.trim(); |
11 |
| - char[] char1 = s.toCharArray(); |
12 |
| - String number = ""; |
13 |
| - boolean negative = false; |
14 |
| - boolean zero = false; |
15 |
| - boolean isDigit = false; |
16 | 9 |
|
17 |
| - for (char ch : char1) { |
18 |
| - if (Character.isDigit(ch)) { |
19 |
| - if (number.length() > 1 && !isDigit) { |
20 |
| - number = "0"; |
21 |
| - break; |
22 |
| - } |
23 |
| - isDigit = true; |
24 |
| - if (zero) { |
25 |
| - number = "0"; |
26 |
| - break; |
27 |
| - } |
28 |
| - if (ch >= '0' && ch <= '9') { |
29 |
| - number += ch; |
30 |
| - } |
31 |
| - } else if (ch == '-' && !isDigit) { |
32 |
| - number += "0"; |
33 |
| - negative = true; |
34 |
| - } else if (ch == '+' && !isDigit) { |
35 |
| - number += "0"; |
36 |
| - } else if (ch == '.' && isDigit) { |
37 |
| - break; |
38 |
| - } else if (ch == '.') { |
39 |
| - zero = true; |
40 |
| - } else { |
41 |
| - if (!isDigit) { |
42 |
| - number = "0"; |
43 |
| - } |
44 |
| - break; |
45 |
| - } |
| 10 | + /** |
| 11 | + * Converts the given string to a 32-bit signed integer. |
| 12 | + * The conversion discards any leading whitespace characters until the first non-whitespace character is found. |
| 13 | + * Then, it takes an optional initial plus or minus sign followed by as many numerical digits as possible and interprets them as a numerical value. |
| 14 | + * 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. |
| 15 | + * |
| 16 | + * If the number is out of the range of a 32-bit signed integer: |
| 17 | + * - Returns {@code Integer.MAX_VALUE} if the value exceeds {@code Integer.MAX_VALUE}. |
| 18 | + * - Returns {@code Integer.MIN_VALUE} if the value is less than {@code Integer.MIN_VALUE}. |
| 19 | + * |
| 20 | + * If no valid conversion could be performed, a zero is returned. |
| 21 | + * |
| 22 | + * @param s the string to convert |
| 23 | + * @return the converted integer, or 0 if the string cannot be converted to a valid integer |
| 24 | + */ |
| 25 | + public static int myAtoi(String s) { |
| 26 | + if (s == null || s.isEmpty()) { |
| 27 | + return 0; |
46 | 28 | }
|
47 | 29 |
|
48 |
| - if (!isDigit) { |
| 30 | + s = s.trim(); |
| 31 | + int length = s.length(); |
| 32 | + if (length == 0) { |
49 | 33 | return 0;
|
50 | 34 | }
|
51 | 35 |
|
52 |
| - number = number.replaceFirst("^0+(?!$)", ""); |
| 36 | + int index = 0; |
| 37 | + boolean negative = false; |
| 38 | + |
| 39 | + // Check for the sign |
| 40 | + if (s.charAt(index) == '-' || s.charAt(index) == '+') { |
| 41 | + negative = s.charAt(index) == '-'; |
| 42 | + index++; |
| 43 | + } |
53 | 44 |
|
54 |
| - if (number.length() > 10 && negative) { |
55 |
| - return -2147483648; |
56 |
| - } else if (number.length() > 10) { |
57 |
| - return 2147483647; |
58 |
| - } else if (number.length() == 10 && negative) { |
59 |
| - double db1 = Double.parseDouble(number); |
60 |
| - if (db1 >= 2147483648d) { |
61 |
| - return -2147483648; |
| 45 | + int number = 0; |
| 46 | + while (index < length) { |
| 47 | + char ch = s.charAt(index); |
| 48 | + if (!Character.isDigit(ch)) { |
| 49 | + break; |
62 | 50 | }
|
63 |
| - } else if (number.length() == 10) { |
64 |
| - double db1 = Double.parseDouble(number); |
65 |
| - if (db1 > (2147483647)) { |
66 |
| - return 2147483647; |
| 51 | + |
| 52 | + int digit = ch - '0'; |
| 53 | + |
| 54 | + // Check for overflow |
| 55 | + if (number > (Integer.MAX_VALUE - digit) / 10) { |
| 56 | + return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE; |
67 | 57 | }
|
68 |
| - } |
69 | 58 |
|
70 |
| - if (negative) { |
71 |
| - return Integer.parseInt(number) * -1; |
| 59 | + number = number * 10 + digit; |
| 60 | + index++; |
72 | 61 | }
|
73 | 62 |
|
74 |
| - return Integer.parseInt(number); |
| 63 | + return negative ? -number : number; |
75 | 64 | }
|
76 | 65 | }
|
0 commit comments