-
Notifications
You must be signed in to change notification settings - Fork 0
/
AtoISolution.java
48 lines (48 loc) · 1.31 KB
/
AtoISolution.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
public class AtoISolution {
/**
* @param str: A string
* @return An integer
*/
public static int atoi(String str) {
// write your code here
if(str == null || str.length() == 0){
return 0;
}
str = str.trim();
int symbol;
int index = 0;
double num = 0;
int poi = str.length()-1;
switch(str.charAt(0)){
case '-':
symbol = -1;
break;
default :
symbol = 1;
break;
}
for(int i = 0 ; i < str.length() ; i++){
if(str.charAt(i) == '.'){
poi = i;
break;
}
}
System.out.println(poi);
//Pattern pattern=Pattern.compile("[0-9]");
for(int i = poi ; i > -1 ; i--){
//Matcher m=pattern.matcher(str.charAt(i));
if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
num = num + (str.charAt(i)-'0') * Math.pow(10, index);
index++;
}
}
System.out.println(num);
num = num > 2147483647 ? 2147483647 :num;
num = symbol * num;
return (int)num;
}
public static void main(String args[]){
String a = "10";
System.out.println(atoi(a));
}
}