Created
September 29, 2014 04:33
-
-
Save V0L0DYMYR/a9a0791207ff461ddfe5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public boolean isNumber(String s) { | |
| if (s == null) return false; | |
| s = s.trim(); | |
| if (s.length() == 0) return false; | |
| s = s+ ' '; | |
| State state = State.INIT; | |
| try { | |
| for (int i = 0; i < s.length(); i++) { | |
| char c = s.charAt(i); | |
| if (i == 0) state = State.INIT.next(c); | |
| else state = state.next(c); | |
| } | |
| } catch (RuntimeException e) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| public enum State { | |
| INIT { | |
| @Override | |
| public State next(char c) { | |
| if (c == '-' || c == '+') return SIGN; | |
| if (c == '.') return START_DOT; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return BASE_NUM; | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }, | |
| START_DOT { | |
| @Override | |
| public State next(char c) { | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return BASE_NUM_AFTER_DOT; | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }, | |
| SIGN { | |
| @Override | |
| public State next(char c) { | |
| int num = c - '0'; | |
| if (c == '.') return START_DOT; | |
| if (0 <= num && num <= 9 ) return BASE_NUM; | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }, | |
| BASE_NUM { | |
| @Override | |
| public State next(char c) { | |
| if (c == ' ') return END; | |
| if (c == '.') return DOT; | |
| if (c == 'e') return E; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return BASE_NUM; | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }, | |
| DOT { | |
| @Override | |
| public State next(char c) { | |
| if (c == ' ') return END; | |
| if (c == 'e') return E; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return BASE_NUM_AFTER_DOT; | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }, | |
| BASE_NUM_AFTER_DOT { | |
| @Override | |
| public State next(char c) { | |
| if (c == ' ') return END; | |
| if (c == 'e') return E; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return BASE_NUM_AFTER_DOT; | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }, | |
| E { | |
| @Override | |
| public State next(char c) { | |
| if (c == '-' || c == '+') return EXP_SIGN; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return EXP_NUM; | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }, | |
| EXP_SIGN { | |
| @Override | |
| public State next(char c) { | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return EXP_NUM; | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }, | |
| EXP_NUM { | |
| @Override | |
| public State next(char c) { | |
| if (c == ' ') return END; | |
| int num = c - '0'; | |
| if (0 <= num && num <= 9 ) return EXP_NUM; | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }, | |
| END { | |
| @Override | |
| public State next(char c) { | |
| throw new RuntimeException("Validation error. Wrong char '" + c + "'."); | |
| } | |
| }; | |
| public abstract State next(char c); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment