Leetcode_valid-number(updated c++ and python version)
来源:互联网 发布:淘宝助理外链图片 编辑:程序博客网 时间:2024/05/19 04:06
地址:http://oj.leetcode.com/problems/valid-number/
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.
参考代码:
思路:
1. 首先去除首尾空格
2. 剔除开始的'+' 或者 '-' (If exists)
3. 上述两个操作之后,可能包含的非数字字符:'e' '.' '+' '-' 并且最多只能出现一次
其中’ . ‘ 不能出现在' e '之后,
’+‘ 或 ’-‘ 应该紧接着'e' 并且'+' 或者'-'不能是除空格之外的最后一位
’e‘ 不能紧接着'.'出现,中间要有数字,比如3.7e-10
参考代码:
class Solution {public: bool isNumber(const char *s) { if(!s) return false; while(s && *s == ' ') ++s; int len = strlen(s); if(!len) return false; const char *end = s+len-1; while(end>s && *end--==' ') --len; if(*s=='-' || *s=='+') { --len; ++s; } int pos_e = -1, pos_dot = -1, pos_plus=-1, pos_minus=-1; bool has_num = false; for(int i = 0; i<len; ++i) { if(*(s+i)>='0' && *(s+i)<='9') has_num = true; else if(*(s+i)=='.') { if(pos_dot>=0) return false; pos_dot = i; if(pos_e>=0) return false; } else if(*(s+i)=='e') { if(pos_e>=0 || i==0 || i==len-1) return false; pos_e = i; if(pos_dot>=0 && pos_dot+1==pos_e && (i==len-1 || pos_dot==0)) return false; } else if(*(s+i)=='+') { if(pos_plus>=0) return false; pos_plus = i; if(pos_e==-1 || pos_plus>pos_e+1 || i==len-1) return false; } else if(*(s+i)=='-') { if(pos_minus>=0) return false; pos_minus = i; if(pos_e==-1 || pos_minus>pos_e+1 || i==len-1) return false; } else return false; } return has_num; }};
思路:用python投机取巧可以么
参考代码:
class Solution: # @param s, a string # @return a boolean def isNumber(self, s): s = s.strip() if not s: return False try: float(s) except Exception, e: return False else: return True
0 0
- Leetcode_valid-number(updated c++ and python version)
- Leetcode_pascals-triangle (updated c++ and python version)
- Leetcode_unique-paths (updated c++ and python version)
- Leetcode_path-sum (updated c++ and python version)
- Leetcode_jump-game (updated c++ and python version)
- Leetcode_multiply-strings (updated c++ and python version)
- Leetcode_maximum-depth-of-binary-tree (updated c++ and python version)
- Leetcode_binary-tree-level-order-traversal (updated c++ and python version)
- Leetcode_remove-duplicates-from-sorted-list (updated c++ and python version)
- Leetcode_merge-sorted-array(updated c++ and python version)
- Leetcode_pascals-triangle-ii (updated c++ and python version)
- Leetcode_length-of-last-word (updated c++ and python version)
- Leetcode_first-missing-positive (updated c++ and python version)
- Leetcode_trapping-rain-water(updated c++ and python version)
- *Leetcode_binary-tree-maximum-path-sum (updated c++ and python version)
- leetcode_Valid Number
- leetCode_Valid Number
- Leetcode_binary-tree-zigzag-level-order-traversal (updated c++ and python version)
- Java Thread Dumps分析
- eclipse集成Tomcat与web项目开发,oracle新建连接。
- android允许访问网络和检测网络
- 【C++专题】static_cast, dynamic_cast, const_cast探讨
- 经典白话算法之冒泡排序
- Leetcode_valid-number(updated c++ and python version)
- Android SDK Manager配置中遇到 连接被拒绝, 连接超时问题解决办法之一
- POJ 3666 Making the Grade 滚动数组+状态压缩DP
- Java 集合(Collection)
- openssl与keytool工具使用手册
- HDU3622(二分+2-SAT)
- java web,charset编码设置正确情况下出现部分中文正常,部分中文乱码的问题
- java swing 之 JScrollPane(滚动面板)的使用
- The end where I begin