LeetCode #65 Valid Number
2015-08-02 20:06
274 查看
Validate if a given string is numeric.
Some examples:
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
leetcode将本题的 Difficulty 定义为Hard,其实本题在Leetcode中并不能称上是难题,但本题确实需要注意很多细节,所以代码很容易出错。顺便说下,直到现在,本题在leetcode上的通过率也还是最低的(如下)。
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.
leetcode将本题的 Difficulty 定义为Hard,其实本题在Leetcode中并不能称上是难题,但本题确实需要注意很多细节,所以代码很容易出错。顺便说下,直到现在,本题在leetcode上的通过率也还是最低的(如下)。
<pre name="code" class="cpp">class Solution { public: bool isNumber(string s) { if(s.size() == 0) return false; int index = 0, num_cnt = 0, dot_cnt = 0; while(s[index] == ' ') index++; if(s[index] == '+' || s[index] == '-') index++; while(isdigit(s[index]) || s[index] == '.') s[index++] == '.' ? dot_cnt++ : num_cnt++; if(dot_cnt > 1 || num_cnt < 1) return false; if(s[index] == 'e'){ index++; num_cnt = 0; dot_cnt = 0; if(s[index] == '+' || s[index] == '-') index++; while(isdigit(s[index]) || s[index] == '.') s[index++] == '.' ? dot_cnt++ : num_cnt++; if(dot_cnt > 0 || num_cnt < 1) return false; } while(index < s.size()){ if(s[index++] != ' ') return false; } return true; } };
相关文章推荐
- gtk+修改控件文本字体一例
- 手机模板修改
- STM32F030 WWDG使用结论
- 欢迎使用CSDN-markdown编辑器
- 【暑假集训】之被ACM金牌大神虐的第三天之树形dp篇
- HDU 2187 悼念512汶川大地震遇难同胞——老人是真饿了【贪心】
- Java内存管理原理及内存区域详解
- java中的异常详解
- 解决Sublime Text 2中文显示乱码的问题
- leetcode 62: Unique Paths
- 小白鼠与毒药解题过程分析
- Java语言程序设计进阶----第四周作业
- 写一个函数,模拟strstr()函数
- 【STL源码剖析】令人困惑的语法
- hdoj5327
- Windows 10 安装 .Net Framework 3.5 的方法
- SRM 664 Div2 Hard: BearSortsDiv2(归并排序)
- Javascript-基础知识(5)
- 设计模式1-单例模式
- 3DMax模型导入CE3