【leetcode】Valid Number
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.
其他测试用例:
- class Solution {
- public:
- enum TYPE
- {
- INVALID,
- SPACE,
- SIGN,
- DIGIT,
- DOT,
- EXP
- };
- bool isNumber(const char *s) {
- while(*s!='\0'&&*s==' ') s++;
- if(*s=='+'||*s=='-') s++;
- if(strlen(s)==) return false;
- bool hasSign=false;
- bool hasDigit=false;
- bool hasDot=false;
- bool hasExp=false;
- TYPE preType;
- TYPE type;
- while(*s!='\0')
- {
- type=getType(s);
- if(type==INVALID) return false;
- if(preType==SIGN &&(type!=DIGIT&&type!=DOT)) return false;
- if(preType==DOT&&(type!=DIGIT&&type!=SPACE&&type!=EXP))return false;
- if(preType==EXP&&(type!=DIGIT&&type!=SIGN))return false;
- if(preType==SPACE&&type!=SPACE)return false;
- switch(type)
- {
- case SPACE:
- preType=SPACE;
- break;
- case SIGN:
- if(hasSign) return false;
- else
- {
- if(preType!=EXP) return false;
- hasSign=true;
- preType=SIGN;
- }
- break;
- case DIGIT:
- hasDigit=true;
- preType=DIGIT;
- break;
- case DOT:
- if(hasDot||hasExp) return false;
- else
- {
- hasDot=true;
- preType=DOT;
- }
- break;
- case EXP:
- if(hasExp||!hasDigit) return false;
- else
- {
- hasExp=true;
- preType=EXP;
- }
- break;
- }
- s++;
- }
- if(preType==SIGN||preType==EXP||(!hasDigit&&hasDot)) return false;
- return true;
- }
- TYPE getType(const char *s)
- {
- if(*s==' ') return SPACE;
- else if(*s=='+'||*s=='-') return SIGN;
- else if(isdigit(*s))return DIGIT;
- else if(*s=='.')return DOT;
- else if(*s=='e')return EXP;
- else return INVALID;
- }
- };
【leetcode】Valid Number的更多相关文章
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【leetcode】1178. Number of Valid Words for Each Puzzle
题目如下: With respect to a given puzzle string, a word is valid if both the following conditions are sa ...
- 【leetcode】Valid Triangle Number
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- 【Leetcode】【Hard】Valid Number
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
随机推荐
- GoLang之基础
GoLang之基础 Go是一种并发的.带垃圾回收的.快速编译的语言. 经典的"hello world"入门: package main import "fmt" ...
- css1-css3的那些模糊点
css很重要, 但也不是万能的, 也不能抛弃dom 元素和 元素的属性!! 很多时候, dom "元素" 的 "属性" 也很重要 也很实用! 要结合属性来写 包 ...
- Java字节流:ByteArrayInputStream ByteArrayOutputStream
----------------------------------------------------------------------------------- ByteArrayInputSt ...
- Junit初级编码(二)探索JUnit核心
序,Junit测试是单元测试的一个框架,提供了很多方法,供我们快速开展单元测试.现在就让我们慢慢学习Junit单元测试框架 一.Junit的三个核心概念测试类.测试集.测试运行器 1 测试类 公共的, ...
- mysql 总结二(自定义存储过程)
mysql执行流程: sql命令--->mysql引擎-----(分析)---->语法正确-----(编译)--->可识别命令----(执行)---->执行结果---(返回)- ...
- activti表结构
1.结构设计 1.1. 逻辑结构设计 Activiti使用到的表都是ACT_开头的. ACT_RE_*: ’RE’表示repository(存储),RepositoryService接口所操作的 ...
- POJ 3071 Football
很久以前就见过的...最基本的概率DP...除法配合位运算可以很容易的判断下一场要和谁比. from——Dinic算法 Football Time ...
- Spotlight on oracle
Spotlight on Oracle 能让你迅速发现任何性能瓶颈,无论是实时还是历史查询.Spotlight 能鉴别和诊断几千种性能问题,无论是特定用户问题.集中资源SQL事务. I/O瓶颈.锁定等 ...
- 个人建了一个APPCAN移动前端开发交流QQ群258213194
QQ群号:258213194,欢迎有兴趣的同志加一下. 二维码如下:
- centos安装
转:http://www.cnblogs.com/Johness/archive/2012/12/03/2800126.html 在已经安装了Win7的系统下安装CentOS 注意:1.由于涉及到对硬 ...