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. log4j2 使用

    转载自 Blog of 天外的星星: http://www.cnblogs.com/leo-lsw/p/log4j2tutorial.html Log4j 2的好处就不和大家说了,如果你搜了2,说明你 ...

  2. python 文件包含

    Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行.要包含目录里 ...

  3. yii2 funson86\yii2-setting

    Yii2 Setting for other application, especially for Yii2 Adminlte Installation The preferred way to i ...

  4. reactjs 注意点

    render的return return前要留一空行 return的括号要分别各占一行,不能与html同行 return中的html必须要有顶层容器包裹 return中的循环不能用for,改用map方 ...

  5. jqxTreeGrid

    基本TreeGrid样本 <!DOCTYPE html> <html lang="en"> <head> <title id=" ...

  6. iOS钥匙串

    钥匙串 苹果的"生态圈",钥匙串访问,使用 AES 256 加密算法,能够保证用户密码的安全 钥匙串访问SDK,是苹果在 iOS 7.0.3 版本以后公布的 钥匙串访问的接口是纯 ...

  7. css3实现渐变的iPhone滑动解锁效果

    先贴代码 <!DOCTYPE html> <html> <head> <style> p{ width:50%; margin:0 auto; line ...

  8. shell 脚本样例

    1  解压文件,移动文件,删除特定目录 #!/bin/bash pa=$(cd ``; pwd) //获得当前目录的绝对路径 v_dir=${pa} mkdir ${v_dir} dirDist=${ ...

  9. CF459C Pashmak and Buses (构造d位k进制数

    C - Pashmak and Buses Codeforces Round #261 (Div. 2) C. Pashmak and Buses time limit per test 1 seco ...

  10. c# 获取系统时间

    //获取日期+时间DateTime.Now.ToString();             // 2008-9-4 20:02:10DateTime.Now.ToLocalTime().ToStrin ...