class Solution {
public int myAtoi ( String str) {
Automaton automaton = new Automaton ( ) ;
int length = str. length ( ) ;
for ( int i = 0 ; i < length; ++ i) {
automaton. get ( str. charAt ( i) ) ;
}
return ( int ) ( automaton. sign * automaton. ans) ;
}
}
class Automaton {
public int sign = 1 ;
public long ans = 0 ;
private String state = "start" ;
private Map< String, String[ ] > table = new HashMap < String, String[ ] > ( ) { {
put ( "start" , new String [ ] { "start" , "signed" , "in_number" , "end" } ) ;
put ( "signed" , new String [ ] { "end" , "end" , "in_number" , "end" } ) ;
put ( "in_number" , new String [ ] { "end" , "end" , "in_number" , "end" } ) ;
put ( "end" , new String [ ] { "end" , "end" , "end" , "end" } ) ;
} } ;
public void get ( char c) {
state = table. get ( state) [ get_col ( c) ] ;
if ( "in_number" . equals ( state) ) {
ans = ans * 10 + c - '0' ;
ans = sign == 1 ? Math. min ( ans, ( long ) Integer. MAX_VALUE) : Math. min ( ans, - ( long ) Integer. MIN_VALUE) ;
} else if ( "signed" . equals ( state) ) {
sign = c == '+' ? 1 : - 1 ;
}
}
private int get_col ( char c) {
if ( c == ' ' ) {
return 0 ;
}
if ( c == '+' || c == '-' ) {
return 1 ;
}
if ( Character. isDigit ( c) ) {
return 2 ;
}
return 3 ;
}
}
class Solution {
public boolean isPalindrome ( int x) {
if ( x < 0 || ( x % 10 == 0 && x != 0 ) ) {
return false ;
}
int revertedNumber = 0 ;
while ( x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10 ;
x /= 10 ;
}
return x == revertedNumber || x == revertedNumber / 10 ;
}
}
class Solution {
public int reverse ( int x) {
int rev = 0 ;
while ( x != 0 ) {
int pop = x % 10 ;
x /= 10 ;
if ( rev > Integer. MAX_VALUE/ 10 || ( rev == Integer. MAX_VALUE / 10 && pop > 7 ) ) return 0 ;
if ( rev < Integer. MIN_VALUE/ 10 || ( rev == Integer. MIN_VALUE / 10 && pop < - 8 ) ) return 0 ;
rev = rev * 10 + pop;
}
return rev;
}
}