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
| class Solution { public boolean isNumber(String s) { boolean isNum = false, isDot = false, iseOrE = false; char[] cs = s.trim().toCharArray(); for (int i = 0; i < cs.length; ++i) { if (cs[i] >= '0' && cs[i] <= '9') { isNum = true; } else if (cs[i] == '.') { if (isDot || iseOrE) return false; isDot = true; } else if (cs[i] == 'e' || cs[i] == 'E') { if (!isNum || iseOrE) return false; iseOrE = true; isNum = false; } else if (cs[i] == '+' || cs[i] == '-') { if (i > 0 && cs[i - 1] != 'e' && cs[i - 1] != 'E') return false; } else { return false; } } return isNum; } }
|