-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
MyAtoi.java
65 lines (55 loc) · 2.15 KB
/
MyAtoi.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.thealgorithms.strings;
/**
* A utility class that provides a method to convert a string to a 32-bit signed integer (similar to C/C++'s atoi function).
*/
public final class MyAtoi {
private MyAtoi() {
}
/**
* Converts the given string to a 32-bit signed integer.
* The conversion discards any leading whitespace characters until the first non-whitespace character is found.
* 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.
* 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 number is out of the range of a 32-bit signed integer:
* - Returns {@code Integer.MAX_VALUE} if the value exceeds {@code Integer.MAX_VALUE}.
* - Returns {@code Integer.MIN_VALUE} if the value is less than {@code Integer.MIN_VALUE}.
*
* If no valid conversion could be performed, a zero is returned.
*
* @param s the string to convert
* @return the converted integer, or 0 if the string cannot be converted to a valid integer
*/
public static int myAtoi(String s) {
if (s == null || s.isEmpty()) {
return 0;
}
s = s.trim();
int length = s.length();
if (length == 0) {
return 0;
}
int index = 0;
boolean negative = false;
// Check for the sign
if (s.charAt(index) == '-' || s.charAt(index) == '+') {
negative = s.charAt(index) == '-';
index++;
}
int number = 0;
while (index < length) {
char ch = s.charAt(index);
if (!Character.isDigit(ch)) {
break;
}
int digit = ch - '0';
// Check for overflow
if (number > (Integer.MAX_VALUE - digit) / 10) {
return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
}
number = number * 10 + digit;
index++;
}
return negative ? -number : number;
}
}