[Leetcode] Valid Number
2014-04-29 16:27
323 查看
Validate if a given string is numeric.
Some examples:
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
就看能不能将各种情况都考虑周到了。
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.
就看能不能将各种情况都考虑周到了。
class Solution { public: bool isNumber(const char *s) { while (*s == ' ') ++s; while (*s == '+' || *s == '-') ++s; bool exp = false, space = false, point = false; bool number = false; while (*s != '\0') { if (isdigit(*s)) { if (space) return false; else number = true; } else if (*s == '.') { if (!point && !space && !exp) { point = true; } else { return false; } } else if (*s == 'e') { if (!exp && number && !space) { exp = true; number = false; while (*(s+1) == '+' || *(s+1) == '-') ++s; } else { return false; } } else if (*s == ' ') { if (!space) { space = true; } } else { return false; } ++s; } return number; } };
相关文章推荐
- leetcode -- Valid Number -- Math难题,要用自动机,可以先不看
- Leetcode: Valid Number
- LeetCode —— Valid Number
- LeetCode | Valid Number
- Leetcode - Valid Number
- LeetCode-Valid Number
- leetcode8:Valid Number
- 【LeetCode】Valid Number
- leetcode_065 Valid Number
- leetcode-Valid Number
- [LeetCode 65] Valid Number (通过率最低的一题)
- LeetCode|Valid Number
- LeetCode 65 - Valid Number
- 【LeetCode】Valid Number 解题报告
- LeetCode-Valid Number-合法数字-状态机
- [Leetcode] Valid Number (Java)
- leetcode第一刷_Valid Number
- leetcode---Valid Number
- [LeetCode] Valid Number
- LeetCode(65) Valid Number