[leetcode] 65. Valid Number 解题报告
来源:互联网 发布:怎样安装t3软件 编辑:程序博客网 时间:2024/06/06 00:40
题目链接:https://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.
思路:这种字符串处理的题目烦的很,corner case 太多而且题目说的也不清楚,需要注意的是下面几种case (.54343)这种数据是合法的,(.)这个数据是不合法的,323e这种数据是不合法的,e后面需要跟数字,并且还可能有符号.开头结尾可能有空格.
我们可以设置两个计数器,一个用来测试字符串是否能够按照规定的字符走完,另一个用于标记是否出现了非法的case,比如e后面没有跟数据,或者e不能出现在首位,因为比如:1.2e+12这种是科学计数法,表示1.2*(10^12),所以e之前必须有数据.
代码如下:
class Solution {public: bool isNumber(string s) { int i =0, k =0, len = s.size(); while(s[i]==' ') i++; if(s[i]=='+' || s[i] == '-') i++; while(isdigit(s[i])) i++, k++; if(s[i] == '.') i++; while(isdigit(s[i])) i++, k++; if(k ==0) return false; if(s[i] == 'e') { i++, k = 0; if(s[i]=='+' || s[i] == '-') i++; while(isdigit(s[i])) i++, k++; if(k == 0) return false; } while(s[i] == ' ') i++; return i == len; }};
参考:https://leetcode.com/discuss/88730/rainbow-concise-clearer-implementaiton-detailed-comments
0 0
- [leetcode] 65. Valid Number 解题报告
- [Leetcode] 65. Valid Number 解题报告
- 【LeetCode】Valid Number 解题报告
- Leetcode 65. Valid Number 验证数字 解题报告
- 【LeetCode】611.Valid Triangle Number解题报告
- LeetCode解题报告--Valid Parentheses
- [LeetCode] Valid Parentheses 解题报告
- [LeetCode] Valid Sudoku 解题报告
- 【LeetCode】Valid Anagram 解题报告
- [LeetCode] Valid Parentheses 解题报告
- 【LeetCode】Valid Parentheses 解题报告
- Leetcode Valid Parentheses 解题报告
- [LeetCode]Palindrome Number,解题报告
- LeetCode 解题报告 Single Number
- 【LeetCode】Largest Number 解题报告
- 【LeetCode】Largest Number 解题报告
- 【LeetCode】Largest Number 解题报告
- [LeetCode]Largest Number, 解题报告
- Fibonacci
- hdu 1087 递增子序列和
- java构造器小结
- 【图文详解】scrapy爬虫与动态页面——爬取拉勾网职位信息(2)
- activiti modeler在线编辑器关闭跳转路劲自定义
- [leetcode] 65. Valid Number 解题报告
- Leetcode: Roman to integer
- 51nod 1134 最长递增子序列 nlogn lis
- js判断IE版本
- 系统属性调用评估报告
- 第10、11周项目3 警察和厨师(1)
- 什么是Node.js
- 231. Power of Two
- Spark入门实战系列--5.Hive(上)--Hive介绍及部署