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
| class Solution { public boolean isNumber(String s) { Map[] states = { new HashMap<>() {{ put(' ', 0); put('s', 1); put('d', 2); put('.', 4); }}, new HashMap<>() {{ put('d', 2); put('.', 4); }}, new HashMap<>() {{ put('d', 2); put('.', 3); put('e', 5); put(' ', 8); }}, new HashMap<>() {{ put('d', 3); put('e', 5); put(' ', 8); }}, new HashMap<>() {{ put('d', 3); }}, new HashMap<>() {{ put('s', 6); put('d', 7); }}, new HashMap<>() {{ put('d', 7); }}, new HashMap<>() {{ put('d', 7); put(' ', 8); }}, new HashMap<>() {{ put(' ', 8); }} }; int p = 0; char t; for(char c : s.toCharArray()) { if(c >= '0' && c <= '9') t = 'd'; else if(c == '+' || c == '-') t = 's'; else if(c == 'e' || c == 'E') t = 'e'; else if(c == '.' || c == ' ') t = c; else t = '?'; if(!states[p].containsKey(t)) return false; p = (int)states[p].get(t); } return p == 2 || p == 3 || p == 7 || p == 8; } }
作者:jyd 链接:https: 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|