-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrInt.java
More file actions
35 lines (31 loc) · 897 Bytes
/
StrInt.java
File metadata and controls
35 lines (31 loc) · 897 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
32
33
34
35
public class StrInt {
public static void main(String[] args) {
String s = "-91283472332";
int n = s.length();
int i = 0;
int sign = 1;
int num = 0;
s = s.trim();
if(i < n && (s.charAt(i) == '-' || s.charAt(i) == '+')){
if(s.charAt(i) == '-'){
sign = -1;
}else{
sign = 1;
}
i++;
}
while(i < n && (Character.isDigit(s.charAt(i)))){
num = num * 10 + (s.charAt(i) - '0');
if(num * sign > Integer.MAX_VALUE){
System.out.println(Integer.MAX_VALUE);
return;
}
if(num * sign < Integer.MIN_VALUE){
System.out.println(Integer.MIN_VALUE);
return;
}
i++;
}
System.out.println(sign * num);
}
}