-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
string-to-integer-atoi.java
65 lines (49 loc) · 1.85 KB
/
string-to-integer-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
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
62
63
64
65
package com.bst.myexamples;
/**
* Solution accepted on Leetcode with 7ms runtime and 39.2MB memory
*
* String to Integer (atoi)
* Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).
* Approach
* 1.Prepare String that is having only +- sign and number value
* Convert to BigInteger to compare value with 32bit signed Integer range and clamp if goes out of range
* return output with 0 if no string number found or non number value found before any number value
*/
import java.math.*;
class StringToIntegerATOI {
public static void main(String[] args){
/* StringToIntegerATOI sta = new StringToIntegerATOI();
System.out.println(sta.myAtoi("-20000000000000"));
*/
}
public int myAtoi(String s) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<s.length(); i++){
if(sb.length() == 0){
if(Character.isWhitespace(s.charAt(i)))
continue;
if('-' == s.charAt(i)){
sb.append("-");
continue;
}
if('+' == s.charAt(i)){
sb.append("+");
continue;
}
}
if(Character.isDigit(s.charAt(i)))
sb.append(s.charAt(i));
else
break;
}
String val = sb.toString().replace("+","");
if(val.length() == 0 || val.equals("-"))
val="0";
BigInteger lvar = new BigInteger(val);
if(lvar.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
lvar = BigInteger.valueOf(Integer.MAX_VALUE);
else if(lvar.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0)
lvar = BigInteger.valueOf(Integer.MIN_VALUE);
return lvar.intValue();
}
}