class Solution { int i = 0; public boolean isNumber(String s) { s = s.trim(); int n = s.length(); if(n == 0) return false; boolean b = scanInteger(s); if(i<n&& s.charAt(i) == '.'){ i++; b = scanUnsignedInteger(s) || b; } if(i<n && s.charAt(i) == 'e'){ i++; b = b&&scanInteger(s); } return b && i == n; } private boolean scanInteger(String s) { if(i<s.length() &&(s.charAt(i) =='+' || s.charAt(i) == '-')){ i++; } return scanUnsignedInteger(s); } private boolean scanUnsignedInteger(String s) { int k = i; while (i<s.length() && s.charAt(i)>='0' && s.charAt(i)<='9'){ i++; } return i>k; } }
剑指 Offer 20. 表示数值的字符串
我的前方是万里征途,星辰大海!!