diff options
| author | Steve Lee <me@xiangyangli.com> | 2017-12-26 01:33:40 +0800 |
|---|---|---|
| committer | Steve Lee <me@xiangyangli.com> | 2017-12-26 01:33:40 +0800 |
| commit | 79a9c52fa923fc78074d88463449a8b7f95ca3ef (patch) | |
| tree | 80c2b596a7c41124845771dca99abd364e89d4c4 /Computer_Science/leetcode/65-valid_number.c | |
| parent | 2e0e0f39d49296f0ffb99aea533a527174521d61 (diff) | |
| download | 42-79a9c52fa923fc78074d88463449a8b7f95ca3ef.tar.xz 42-79a9c52fa923fc78074d88463449a8b7f95ca3ef.zip | |
update leetcode solution
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; +} |
