Valid number
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.
Update (2015-02-10):
The signature of the C + + function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your co De definition.
Idea: This problem is really difficult, and most importantly, I do not know the rules of a valid number ... Not even the rules do not know how to play a good game, so decisive hanging 10 times 8 times, and finally did not fully correct. Temporarily put someone else's code out below, there is time to do a good study:
public class Solution {public Boolean isnumber (String s) {//Start typing your Java solution below//D O not write main () function/* "0" = "0.1" + true "abc" = FAL Se "1 a" + = false "+-2e10" = True//*///check input. if (S==null | | s.length () ==0) return false; int sz = s.length (); int i=0; while (I<sz && s.charat (i) = = ") ++i; Boolean space = false; Boolean exp = false; Boolean dot = false; Boolean number = false; Boolean neg = false; for (; i<sz; i++) {char c = s.charat (i); if (c== ") {space = true; } else if (Space==true) {return false; } else if ((c== ' e ' | | c== ' e ') && exp==false && number==true) {exp = true; Number = false; Dot =True Neg = false; } else if (c== '. ' && dot==false) {dot = true; Neg = true; Number = false; } else if (c>= ' 0 ' && c<= ' 9 ') {number = true; } else if ((c== ' + ' | | c== '-') && neg==false && number==false) {neg = true; } else {return false; }} return number; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode 65.Valid Number (valid numbers)