forked from codednepal/hacktober2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringtoInteger(atoi).java
29 lines (21 loc) · 1.01 KB
/
StringtoInteger(atoi).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
class Solution {
public int myAtoi(String s) {
if(s==null) return 0;
s = s.trim(); //removes leading spaces.
if(s.length()==0) return 0;
int sign = 1;
long ans = 0; //Take long here because ans might exceed MAX/MIN.
int MAX = Integer.MAX_VALUE;
int MIN = Integer.MIN_VALUE;
if(s.charAt(0)=='-') sign = -1;
int i = (s.charAt(0)=='+' || s.charAt(0)=='-') ? 1 : 0; //skips an index if sign is there.
while(i<s.length()){
if(!Character.isDigit(s.charAt(i))) break; //simply returns 0 if first index is not a digit.
ans = ans * 10 + (s.charAt(i)-'0');
if(sign == -1 && ans*-1 < MIN) return MIN; //if ans exceeds MIN then simply return MIN according to the question.
if(sign == 1 && ans > MAX) return MAX; //likewise.
i++;
}
return (int)ans*sign; //convert ans to integer and return.
}
}