-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatoi.java
More file actions
61 lines (45 loc) · 1.39 KB
/
Copy pathatoi.java
File metadata and controls
61 lines (45 loc) · 1.39 KB
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
package LeetCode;
public class atoi {
public static int myAtoi(String str) {
int flag =0;
if(!str.contains("0") && !str.contains("1") && !str.contains("2") && !str.contains("3") && !str.contains("4")&&
!str.contains("5") && !str.contains("6") && !str.contains("7") && !str.contains("8") && !str.contains("9")){
return 0;
}
if(str.contains(".")){
str = str.substring(0, str.indexOf("."));
}
if(str.charAt(0)== '-')
flag =1;
String strNew = "" ;
for(int i=0;i<str.length();i++){
if(str.charAt(i)>=48 && str.charAt(i)<=59){
strNew = strNew + str.charAt(i);
}
}
if(strNew.length() >10){
return 0;
}
if(flag == 1)
strNew = '-' + strNew;
//System.out.println(str);
//System.out.println("strNew " + strNew);
long num1 = Long.parseLong(strNew);
int intNum = (int)num1;
if(intNum > Integer.MAX_VALUE || intNum < Integer.MIN_VALUE){
return 0;
}
return intNum;
}
public static void main(String[] args) {
//int res = myAtoi("0001235.4333");
int res = myAtoi("-1");
System.out.println(res);
}
}
/*str = str.substring(str.lastIndexOf(" ")+1,str.length());
if(str.equals("") || str.contains("+")||str.contains("-")){
return 0;
}*/
//System.out.println(Integer.MAX_VALUE);
//int num = Integer.parseInt(str);