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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
class Solution {
private static Map<State, Map<InputType, State>> transferRule;
static{ transferRule = new HashMap<>();
Map<InputType, State> map = new HashMap<>(); map.put( InputType.BLANK, State.START_BANK ); map.put( InputType.SIGN, State.NUMBER_SIGN ); map.put( InputType.NUMBER, State.INTEGER ); map.put( InputType.DOT, State.DOT_WITHOUT_INTEGER ); transferRule.put( State.START_BANK, map );
map = new HashMap<>(); map.put( InputType.NUMBER, State.INTEGER ); map.put( InputType.DOT, State.DOT_WITHOUT_INTEGER ); transferRule.put( State.NUMBER_SIGN, map );
map = new HashMap<>(); map.put( InputType.NUMBER, State.INTEGER ); map.put( InputType.DOT, State.DOT_WITH_INTEGER ); map.put( InputType.POWER, State.POWER_LOGO ); map.put( InputType.BLANK, State.END_BLANK ); transferRule.put( State.INTEGER, map );
map = new HashMap<>(); map.put( InputType.BLANK, State.END_BLANK ); map.put( InputType.POWER, State.POWER_LOGO ); map.put( InputType.NUMBER, State.FLOAT ); transferRule.put( State.DOT_WITH_INTEGER, map );
map = new HashMap<>(); map.put( InputType.NUMBER, State.FLOAT ); transferRule.put( State.DOT_WITHOUT_INTEGER, map );
map = new HashMap<>(); map.put( InputType.NUMBER, State.FLOAT ); map.put( InputType.POWER, State.POWER_LOGO ); map.put( InputType.BLANK, State.END_BLANK ); transferRule.put( State.FLOAT, map );
map = new HashMap<>(); map.put( InputType.SIGN, State.POWER_SIGN ); map.put( InputType.NUMBER, State.POWER_NUMBER ); transferRule.put( State.POWER_LOGO, map );
map = new HashMap<>(); map.put( InputType.NUMBER, State.POWER_NUMBER ); transferRule.put( State.POWER_SIGN, map );
map = new HashMap<>(); map.put( InputType.BLANK, State.END_BLANK ); map.put( InputType.NUMBER, State.POWER_NUMBER ); transferRule.put( State.POWER_NUMBER, map );
map = new HashMap<>(); map.put( InputType.BLANK, State.END_BLANK ); transferRule.put( State.END_BLANK, map ); }
public boolean isNumber(String s) { if( s==null || s.length()==0) { return false; }
char[] chars = s.toCharArray(); State state = State.START_BANK; for (char ch : chars) { InputType inputType = InputType.calcInputType(ch); Map<InputType, State> map = transferRule.get(state); State nextState = map.get(inputType); if( nextState==null ) { return false; } state = nextState; }
return state.isAccept(); } }
enum State { START_BANK("起始空格", false), NUMBER_SIGN("数字的符号位", false), INTEGER("整数部分", true), DOT_WITH_INTEGER("左侧有整数的小数点", true), DOT_WITHOUT_INTEGER("左侧无整数的小数点", false), FLOAT("小数部分", true), POWER_LOGO("指数符号", false), POWER_SIGN("指数的符号位", false), POWER_NUMBER("指数部分的整数", true), END_BLANK( "结束空格", true), ;
private String name;
private boolean accept;
State(String name, boolean accept) { this.name = name; this.accept = accept; }
public boolean isAccept() { return accept; } }
enum InputType { OTHER("非法输入"), BLANK("空格"), NUMBER("数字"), SIGN("符号位"), DOT("小数点"), POWER("指数符号"), ;
private String name;
InputType(String name) { this.name = name; }
public static InputType calcInputType(char ch) { if ( ch==' ' ) { return BLANK; } else if( ch>='0' && ch<='9' ) { return NUMBER; } else if ( ch=='+' || ch=='-' ) { return SIGN; } else if ( ch=='.' ) { return DOT; } else if ( ch=='e' || ch=='E' ) { return POWER; } return OTHER; } }
|