【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 ...
随机推荐
- Loader Generator---loading图片生成器
if(公司配有专业的设计师) return; Recommend("http://loadergenerator.com/");
- TextBox 英文文档
TextBox Extend from $.fn.validatebox.defaults. Override defaults with $.fn.textbox.defaults. The Tex ...
- android自定义控件(2)-拖拽实现开关切换
在这里,我们的主要工作就是在原有代码的基础上,增加一个重写的onTouchEvent方法,刚添加上来的时候是这个样子的: @Override public boolean onTouchEvent(M ...
- Java Programming Test Question 4
What will be the boolean flag value to reach the finally block? public class JPTQuestion4 { public s ...
- Sublime Text 3初阶
本文主要介绍一些Sublime Text3的初级阶段,主要从最初的安装,到插件,还有主题这三个方面介绍,还会提到一些关于使用ST3的一些小小经验... 一:安装 首先进入sublime的官方地址去下载 ...
- IOS表情存入MYSQL数据库失败
从 MySQL 5.5.3 开始,MySQL 支持一种 utf8mb4 的字符集,这个字符集能够支持 4 字节的 UTF8 编码的字符. utf8mb4 字符集能够完美地向下兼容 utf8 字符串.在 ...
- 连接到kali linux服务器上的MySQL服务器错误
前言:想把数据库什么的都放在虚拟机kali Linux里,但无奈出了好多错误. 首先:可以参照上一篇文章开启kali服务器端的远程连接功能,上一篇文章 然后:使用window端的sqlyog(MySQ ...
- 知识梳理HTML篇
HTML 浏览器内核: IE:trident Firefox : gecko Safari/chrome : webkit Opera : presto(新 ...
- [Storm] 内部消息缓存
这篇文件翻译自 http://www.michael-noll.com/blog/2013/06/21/understanding-storm-internal-message-buffers/ 当进 ...
- POJ 1019 Number Sequence
找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...