-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob8.java
More file actions
52 lines (52 loc) · 1.51 KB
/
prob8.java
File metadata and controls
52 lines (52 loc) · 1.51 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
class Solution {
public int myAtoi(String str) {
long n=0l;
n=str.length();
char ch=str.charAt(0);
int idx=0;
while(idx<n && ch==' '){
idx++;
if(idx<n)
ch=str.charAt(idx);
}
if(idx <n && (ch=='+' || ch=='-' || ((ch-'0')>=0 && (ch-'0')<=9))){
idx++;
long no=0l;
if(idx==n && (ch=='+' || ch=='-'))
System.out.println(0);
else if(idx<n){
char ch1=str.charAt(idx);
int c=0;
while((ch1-'0')>=0 && (ch1-'0')<=9){
idx++;
ch=str.charAt(idx);
no=no*10+(ch-'0');
c++;
}
if(c>0){
no=(ch-'0')*10 + no;
if(no>=0){
if(no<=Integer.MAX_VALUE)
System.out.println(no);
else
System.out.println(Integer.MAX_VALUE);
}
else{
if(no>=Integer.MIN_VALUE)
System.out.println(no);
else
System.out.println(Integer.MIN_VALUE);
}
}
else{
System.out.println(0);
}
}
else
System.out.println(ch);
}
else{
System.out.println(0);
}
}
}