码迷,mamicode.com
首页 > 其他好文 > 详细

[Leetcode] Valid Number

时间:2014-04-29 16:45:45      阅读:398      评论:0      收藏:0      [点我收藏+]

标签: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.

就看能不能将各种情况都考虑周到了。

mamicode.com,码迷
 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 };
mamicode.com,码迷

 

 

[Leetcode] Valid Number,码迷,mamicode.com

[Leetcode] Valid Number

标签:style   blog   http   java   color   strong   

原文地址:http://www.cnblogs.com/easonliu/p/3699525.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!