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.

其他测试用例:

" "    false
"3."  true
"e"   false
"."    false
"3. " true
"46.e3" true
" 005047e+6" true
"6+1" false
 
 
1.符号 后面只能跟 数字,点
2.点 后面只能跟 数字,空格,指数e
3.指数 后面只能跟 数字,符号
4.空格只能位于开头或结尾,去除开头的空格后,空格后面只能接空格(必须位于末尾)
5.去除开头的符号后,符号只能位于指数e之后,且只能出现一次
6.点只能出现一次,且不能位于指数之后
7.指数只能出现一次,且前面需要有数字
8.符号,指数,单独的点,不能做结尾
 
 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的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  4. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  5. 【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 ...

  6. 【leetcode】Valid Triangle Number

    题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...

  7. 【Leetcode】【Hard】Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  8. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  9. 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

随机推荐

  1. Loader Generator---loading图片生成器

    if(公司配有专业的设计师) return; Recommend("http://loadergenerator.com/");

  2. TextBox 英文文档

    TextBox Extend from $.fn.validatebox.defaults. Override defaults with $.fn.textbox.defaults. The Tex ...

  3. android自定义控件(2)-拖拽实现开关切换

    在这里,我们的主要工作就是在原有代码的基础上,增加一个重写的onTouchEvent方法,刚添加上来的时候是这个样子的: @Override public boolean onTouchEvent(M ...

  4. Java Programming Test Question 4

    What will be the boolean flag value to reach the finally block? public class JPTQuestion4 { public s ...

  5. Sublime Text 3初阶

    本文主要介绍一些Sublime Text3的初级阶段,主要从最初的安装,到插件,还有主题这三个方面介绍,还会提到一些关于使用ST3的一些小小经验... 一:安装 首先进入sublime的官方地址去下载 ...

  6. IOS表情存入MYSQL数据库失败

    从 MySQL 5.5.3 开始,MySQL 支持一种 utf8mb4 的字符集,这个字符集能够支持 4 字节的 UTF8 编码的字符. utf8mb4 字符集能够完美地向下兼容 utf8 字符串.在 ...

  7. 连接到kali linux服务器上的MySQL服务器错误

    前言:想把数据库什么的都放在虚拟机kali Linux里,但无奈出了好多错误. 首先:可以参照上一篇文章开启kali服务器端的远程连接功能,上一篇文章 然后:使用window端的sqlyog(MySQ ...

  8. 知识梳理HTML篇

    HTML 浏览器内核: IE:trident         Firefox : gecko        Safari/chrome : webkit        Opera : presto(新 ...

  9. [Storm] 内部消息缓存

    这篇文件翻译自 http://www.michael-noll.com/blog/2013/06/21/understanding-storm-internal-message-buffers/ 当进 ...

  10. POJ 1019 Number Sequence

    找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...