-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_to_integer.java
More file actions
36 lines (30 loc) · 872 Bytes
/
Copy pathString_to_integer.java
File metadata and controls
36 lines (30 loc) · 872 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
36
public class string_to_integer{
public int myAtoi(String s) {
int count =0;
s = s.trim();
if(s.isEmpty()) return 0;
boolean negative = false;
int i =0;
if(s.charAt(i) == '-' || s.charAt(i) =='+'){
negative = (s.charAt(i)=='-');
i++;
}
for(;i<s.length();i++){
if(!Character.isDigit(s.charAt(i))){
break;
}
else{
if(count>(Integer.MAX_VALUE - (s.charAt(i) - '0'))/10){
return (negative == true)?Integer.MIN_VALUE:Integer.MAX_VALUE;
}
count = count*10 + (s.charAt(i) - '0');
}
}
if(negative){
return -count;
}
else{
return count;
}
}
}