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. MANIFEST.MF的用途(转载)

    MANIFEST.MF的用途(转载) 可以用的上的有: 1. Main-Class 指定程序的入口,这样可以直接用java -jar xxx.jar来运行程序. 2. Class-Path 指定jar ...

  2. C#List的排序和简单去重总结

    List集合在开发过程中很常见,经常我们要对该集合进行一系列操作,本文介绍如何将该集合内的元素进行排序,博主制作简单WinForm应用程序进行演示. 首先,我们来看一下c#泛型List提供的Sort方 ...

  3. 如何快速有效的修改java的环境变量

    之前已经修改过jdk的环境变量,,,,在/etc/profile下,,, export JAVA_HOME=/usr/java/jdk1.7.0_67-cloudera export PATH=${J ...

  4. vim关于 引号和 括号的 高效操作-很好很强大的!

    http://blog.csdn.net/bigshady/article/details/6019963 对括号匹配, 进行跳转, 使用的是%. 匹配的括号, 都会被高亮显示, 但是: 根据光标的 ...

  5. R-数据结构

    目录 数据类型(模式) 字符型 数值型 逻辑型 整形 复数型(虚数) 原生型(字节) 数据结构 向量 矩阵 数组 数据框 列表 数据类型 数据结构  向量 用于存储数值型.字符型或逻辑型数据的一维数组 ...

  6. sqoop

    http://blog.csdn.net/yfkiss/article/details/8700480 http://www.cnblogs.com/admln/p/sqoop1-99-4-javaa ...

  7. ACM2 斐波那契数列

    描述 在数学上,斐波那契数列(Fibonacci Sequence),是以递归的方法来定义: F0 = 0 F1 = 1 Fn = Fn - 1 + Fn - 2 用文字来说,就是斐波那契数列由0和1 ...

  8. JSON 问题

    {"statusCode":"300","message":"栏目插入出现故障==bannerInfoService.add 栏目 ...

  9. css的计数器

    更多关于计数器的问题可以参考:https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Getting_Started/Lists

  10. Hanoi问题

    #include<stdio.h>int main(){ int m; void hanoi(int n,char x,char y,char z); printf("input ...