Valid Number
2015-09-09 22:01
393 查看
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 a const char * argument, please click the reload button to reset your code definition.
思路:这道题目就是可能的情况太多了,数字的格式是:
【空白】【符号(+-)】【数字】【小数点】【数字】【e】【符号】【数字】【空白】
其中不能出现一下情况:
(1)全是空白
(2)【e】【符号】【数字】【空白】(e前面的系数不存在)
(3)【小数点】【e】【符号】【数字】【空白】(e前面的系数只是小数点)
(4)【空白】【符号】【小数点】【空白】(小数点前后没有数字)
(5)【空白】【符号(+-)】【数字】【小数点】【数字】【e】【符号】(e后面没有数字)
代码如下:
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 a const char * argument, please click the reload button to reset your code definition.
思路:这道题目就是可能的情况太多了,数字的格式是:
【空白】【符号(+-)】【数字】【小数点】【数字】【e】【符号】【数字】【空白】
其中不能出现一下情况:
(1)全是空白
(2)【e】【符号】【数字】【空白】(e前面的系数不存在)
(3)【小数点】【e】【符号】【数字】【空白】(e前面的系数只是小数点)
(4)【空白】【符号】【小数点】【空白】(小数点前后没有数字)
(5)【空白】【符号(+-)】【数字】【小数点】【数字】【e】【符号】(e后面没有数字)
代码如下:
class Solution { public: bool isAllBlank(string s){//s是否全是空白 for(int i = 0; i < s.length(); ++i) if(!isblank(s[i])) return false; return true; } bool faceE(string s){//碰到e的处理 int i = 0; int len = s.length(); if(s[0] == '-' || s[0] == '+') i++; if(isdigit(s[i])){ i++; while(i < len && isdigit(s[i])) i++; return isAllBlank(s.substr(i)); } return false; } bool faceDot(bool hasDigit, string s){//碰到小数点的处理,hasDigit表示小数点之前是否出现数字 int i = 0, len = s.length(); if(isdigit(s[i])){ i++; while(i < len && isdigit(s[i])) i++; if(i == len)//全是数字 return true; else if(s[i] == 'e' || s[i] == 'E') return faceE(s.substr(i+1)); else if(isblank(s[i])){ return isAllBlank(s.substr(i+1)); } else return false; } else if(s[i] == 'e' || s[i] == 'E') return hasDigit && faceE(s.substr(i+1)); else if(isblank(s[i])){ return isAllBlank(s.substr(i+1)); } else return false; } bool isNumber(string s) { int i = 0, countDot = 0, len = s.length(); bool hasDigit = false; while(i < len && isblank(s[i])) i++; if(i == len)//过滤空白 return false; if(s[i] == '-' || s[i] == '+') i++; while(i < len && isdigit(s[i])){//过滤数字 i++; hasDigit = true; } if(i == len) return true; if(s[i] == '.'){//碰到小数点 if(!hasDigit && isAllBlank(s.substr(i+1))) return false; else if(hasDigit && isAllBlank(s.substr(i+1))) return true; else return faceDot(hasDigit, s.substr(i+1)); } else if(hasDigit && (s[i] == 'E' || s[i] == 'e')){//碰到e return faceE(s.substr(i+1)); } else if(isblank(s[i])){ return isAllBlank(s.substr(i+1)); } else return false; } };
相关文章推荐
- hdu1281(Hungary模板 + 枚举)简单
- java job 资源
- <mvc:annotation-driven /><context:annotation-config/><context:component-scan/>
- 多CPU与单CPU的spin_lock使用上的区别
- -force_load (加载静态库崩溃)
- FindBugs 入门——帮你减少代码中的bug数
- mac常用shell命令
- Apache Spark 1.5新特性介绍
- Android实例-操作sqlite数据库之Grid显示图片(XE8+小米2)
- nyoj 1185 最大最小值【线段树最大值最小值维护】
- Adobe Photoshop CS6学习要点
- poj1083
- Android—软键盘弹出时关于布局的问题
- OC08_字典Dictionary
- 双目相机的一个例子
- 代码检查、代码走查与评审
- Openstack部署总结:“部署过程Error: Local ip for ovs agent must be set when tunneling is enabled”问题
- js中SetInterval与setTimeout用法
- Spark 1.4新特性概述
- lintcode-最小路径和-110