true"" 0.1="" "=">true"abc"=>false"1" a"=">false"2e10"=>trueNote:It" is="" intended="" for="" the="" problem="" stat..."="">
标签:style blog http java color strong
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.
就看能不能将各种情况都考虑周到了。
1 class Solution { 2 public: 3 bool isNumber(const char *s) { 4 while (*s == ‘ ‘) ++s; 5 while (*s == ‘+‘ || *s == ‘-‘) ++s; 6 bool exp = false, space = false, point = false; 7 bool number = false; 8 while (*s != ‘\0‘) { 9 if (isdigit(*s)) { 10 if (space) return false; 11 else number = true; 12 } else if (*s == ‘.‘) { 13 if (!point && !space && !exp) { 14 point = true; 15 } else { 16 return false; 17 } 18 } else if (*s == ‘e‘) { 19 if (!exp && number && !space) { 20 exp = true; 21 number = false; 22 while (*(s+1) == ‘+‘ || *(s+1) == ‘-‘) ++s; 23 } else { 24 return false; 25 } 26 } else if (*s == ‘ ‘) { 27 if (!space) { 28 space = true; 29 } 30 } else { 31 return false; 32 } 33 ++s; 34 } 35 return number; 36 } 37 };
[Leetcode] Valid Number,码迷,mamicode.com
标签:style blog http java color strong
原文地址:http://www.cnblogs.com/easonliu/p/3699525.html