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.符号,指数,单独的点,不能做结尾
 
  1. class Solution {
  2. public:
  3.  
  4. enum TYPE
  5. {
  6. INVALID,
  7. SPACE,
  8. SIGN,
  9. DIGIT,
  10. DOT,
  11. EXP
  12. };
  13.  
  14. bool isNumber(const char *s) {
  15.  
  16. while(*s!='\0'&&*s==' ') s++;
  17. if(*s=='+'||*s=='-') s++;
  18.  
  19. if(strlen(s)==) return false;
  20.  
  21. bool hasSign=false;
  22. bool hasDigit=false;
  23. bool hasDot=false;
  24. bool hasExp=false;
  25.  
  26. TYPE preType;
  27. TYPE type;
  28. while(*s!='\0')
  29. {
  30. type=getType(s);
  31.  
  32. if(type==INVALID) return false;
  33.  
  34. if(preType==SIGN &&(type!=DIGIT&&type!=DOT)) return false;
  35. if(preType==DOT&&(type!=DIGIT&&type!=SPACE&&type!=EXP))return false;
  36. if(preType==EXP&&(type!=DIGIT&&type!=SIGN))return false;
  37. if(preType==SPACE&&type!=SPACE)return false;
  38.  
  39. switch(type)
  40. {
  41. case SPACE:
  42. preType=SPACE;
  43. break;
  44. case SIGN:
  45. if(hasSign) return false;
  46. else
  47. {
  48. if(preType!=EXP) return false;
  49. hasSign=true;
  50. preType=SIGN;
  51. }
  52. break;
  53. case DIGIT:
  54. hasDigit=true;
  55. preType=DIGIT;
  56. break;
  57. case DOT:
  58. if(hasDot||hasExp) return false;
  59. else
  60. {
  61. hasDot=true;
  62. preType=DOT;
  63. }
  64. break;
  65. case EXP:
  66. if(hasExp||!hasDigit) return false;
  67. else
  68. {
  69. hasExp=true;
  70. preType=EXP;
  71. }
  72. break;
  73. }
  74. s++;
  75. }
  76.  
  77. if(preType==SIGN||preType==EXP||(!hasDigit&&hasDot)) return false;
  78.  
  79. return true;
  80. }
  81.  
  82. TYPE getType(const char *s)
  83. {
  84. if(*s==' ') return SPACE;
  85. else if(*s=='+'||*s=='-') return SIGN;
  86. else if(isdigit(*s))return DIGIT;
  87. else if(*s=='.')return DOT;
  88. else if(*s=='e')return EXP;
  89. else return INVALID;
  90. }
  91. };

【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. GoLang之基础

    GoLang之基础 Go是一种并发的.带垃圾回收的.快速编译的语言. 经典的"hello world"入门: package main import "fmt" ...

  2. css1-css3的那些模糊点

    css很重要, 但也不是万能的, 也不能抛弃dom 元素和 元素的属性!! 很多时候, dom "元素" 的 "属性" 也很重要 也很实用! 要结合属性来写 包 ...

  3. Java字节流:ByteArrayInputStream ByteArrayOutputStream

    ----------------------------------------------------------------------------------- ByteArrayInputSt ...

  4. Junit初级编码(二)探索JUnit核心

    序,Junit测试是单元测试的一个框架,提供了很多方法,供我们快速开展单元测试.现在就让我们慢慢学习Junit单元测试框架 一.Junit的三个核心概念测试类.测试集.测试运行器 1 测试类 公共的, ...

  5. mysql 总结二(自定义存储过程)

    mysql执行流程: sql命令--->mysql引擎-----(分析)---->语法正确-----(编译)--->可识别命令----(执行)---->执行结果---(返回)- ...

  6. activti表结构

    1.结构设计 1.1.    逻辑结构设计 Activiti使用到的表都是ACT_开头的. ACT_RE_*: ’RE’表示repository(存储),RepositoryService接口所操作的 ...

  7. POJ 3071 Football

    很久以前就见过的...最基本的概率DP...除法配合位运算可以很容易的判断下一场要和谁比.    from——Dinic算法                         Football Time ...

  8. Spotlight on oracle

    Spotlight on Oracle 能让你迅速发现任何性能瓶颈,无论是实时还是历史查询.Spotlight 能鉴别和诊断几千种性能问题,无论是特定用户问题.集中资源SQL事务. I/O瓶颈.锁定等 ...

  9. 个人建了一个APPCAN移动前端开发交流QQ群258213194

    QQ群号:258213194,欢迎有兴趣的同志加一下. 二维码如下:

  10. centos安装

    转:http://www.cnblogs.com/Johness/archive/2012/12/03/2800126.html 在已经安装了Win7的系统下安装CentOS 注意:1.由于涉及到对硬 ...