Validate if a given string is numeric.
Some Examples:
"0"=true
" 0.1 "=true
"abc"=false
"1 a"=false
"2e10"=true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
Problem Solving Ideas:
The boundary conditions are quite good "1", ".", "1.1e+.1" is also correct. The solution is to trim it first, then divide it according to E, and then make a judgment about E before and after the Java implementation is as follows:
public boolean isnumber (String s) {s = S.trim (); string[] Splitarr = S.split ("e"); if (s.length () = = 0 | | S.charat (0) = = ' E ' | | s.charat (S.LENGTH ()-1) = = ' E ' | | splitarr.l Ength > 2) return false;for (int k = 0; k < splitarr.length; k++) {String str = Splitarr[k];boolean Isdecimal = False if (Str.charat (0) = = '-' | | Str.charat (0) = = ' + ') str = str.substring (1); if (str.length () = = 0) return false;for (int i = 0 ; I < str.length (); i++) {if (' 0 ' <= str.charat (i) && Str.charat (i) <= ' 9 ') Continue;else if (Str.charat (i) = = '. ' &&!is Decimal) {if (k = = 0 && str.length () > 1) isdecimal = True;elsereturn false;} Elsereturn false;}} return true;}
Java for Leetcode 065 Valid number