diff options
Diffstat (limited to 'Computer_Science/leetcode/65-valid_number.c')
| -rw-r--r-- | Computer_Science/leetcode/65-valid_number.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Computer_Science/leetcode/65-valid_number.c b/Computer_Science/leetcode/65-valid_number.c new file mode 100644 index 0000000..b8ee227 --- /dev/null +++ b/Computer_Science/leetcode/65-valid_number.c @@ -0,0 +1,47 @@ +#include <string.h> + +bool isNumber(char* s) { + char *p; + int dot_flag = 0; + int e_flag = 0; + + for(; *s == ' '; s++) + ; + if(*s == '+' || *s == '-') + s++; + + p = s; + + /* trim tail */ + for(; *p != '\0'; p++) + ; + for(p--; p != s && *p == ' '; p--) + ; + *(p + 1) = '\0'; + + p = s; + if(*p == '\0') return false; + + for(; *p != '\0'; p++) { + if(*p <= '9' && *p >= '0') + continue; + else if(*p == '.') { + if(dot_flag == 1) + return false; + if((s == p && *(p + 1) == '\0') + || *(p - 1) == 'e') + return false; + dot_flag = 1; + continue; + } else if(*p == 'e') { + if(e_flag == 1) + return false; + if(s == p || *(p + 1) == '\0' || (*(p - 1) == '.' && p - 1 == s)) + return false; + e_flag = 1; + } else + return false; + } + + return true; +} |
