-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion28.java
More file actions
31 lines (24 loc) · 912 Bytes
/
question28.java
File metadata and controls
31 lines (24 loc) · 912 Bytes
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
public class question28 {
public static int myAtoi(String sc) {
if (sc == null || sc.length() == 0) return 0;
sc = sc.trim();
if (sc.length() == 0) return 0;
int sign = 1, i = 0;
long result = 0;
if (sc.charAt(i) == '+' || sc.charAt(i) == '-') {
sign = (sc.charAt(i) == '-') ? -1 : 1;
i++;
}
while (i < sc.length() && Character.isDigit(sc.charAt(i))) {
result = result * 10 + (sc.charAt(i) - '0');
if (sign * result > Integer.MAX_VALUE) return Integer.MAX_VALUE;
if (sign * result < Integer.MIN_VALUE) return Integer.MIN_VALUE;
i++;
}
return (int)(sign * result);
}
public static void main(String[] args) {
String input = "-24";
System.out.println("Converted Integer: " + myAtoi(input));
}
}