065 - Valid Number
来源:互联网 发布:mac没有千牛吗 编辑:程序博客网 时间:2024/06/17 08:03
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.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts aconst char *
argument, please click the reload button to reset your code definition.
enum numtype {NOTNUM = 0,INTNUM,POINUM,};enum numtype checknum(char *numstr){int len, i, ret = INTNUM;if (*numstr == '-' || *numstr == '+')numstr++;len = strlen(numstr);if (!len)return NOTNUM;for (i = 0; i < len; i++) {if (!isdigit(numstr[i]) && numstr[i] != '.')return NOTNUM;if (numstr[i] == '.') {if (ret == POINUM || !i && !isdigit(numstr[i + 1]))return NOTNUM;elseret = POINUM;}}return ret;}bool isNumber(char* s){int slen = strlen(s);char *end = s + slen - 1;char *begin = s;while (end - s > 0 && *end == ' ')*end-- = '\0';while (*begin == ' ')*begin++ = '\0';if (!*begin)return 0;s = begin;char *ep = strchr(s, 'e');if (!ep)return checknum(s) == NOTNUM ? 0 : 1;*ep = '\0';if (checknum(s) != NOTNUM && checknum(ep + 1) == INTNUM)return 1;return 0;}
test data
12.12-13.2312.12e812.12e8.1-13.23e-8.1 (true)3. (true). 0
0 0
- LeetCode 065 Valid Number
- 065 - Valid Number
- LeetCode 065 Valid Number
- leetcode_c++:Valid Number(065)
- Valid Number
- Valid Number
- Valid Number
- Valid Number
- Valid Number
- Valid Number
- Valid Number
- Valid Number
- Valid Number
- Valid Number
- Valid Number
- Valid Number
- Valid number
- Valid Number
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- MySQL优化:使用慢查询日志定位效率较低的SQL语句
- JS判断字符串是否为空
- 一次android错误日志的分析问题
- 数据结构:递归
- 065 - Valid Number
- 使用Spinner实现下拉列表
- Apache+Tomcat+Memcached简单总结
- Mysql数据库乱码问题
- POJ-2010Moo University - Financial Aid
- hud1114 完全背包
- Android LayoutAnimation 与 LayoutTransition
- 研究-弱点在线事务(OLTP)方面的性能问题。
- 二叉树的遍历