题目

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.

Update (2015-02-10):

The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

分析

参考博客

这道题的判定种类很多,没有想出好的解决方法,AC是参考上面博客的,分析很透彻,谢谢博主!

将其分析复制如下,方便查阅:

用有限状态机,非常简洁,不需要复杂的各种判断!

先枚举一下各种合法的输入情况:

1.空格+ 数字 +空格

2.空格+ 点 + 数字 +空格

3.空格+ 符号 + 数字 + 空格

4.空格 + 符号 + 点 + 数字 +空格

5.空格 + (1, 2, 3, 4) + e + (1, 2, 3, 4) +空格

组后合法的字符可以是:

1.数字

2.空格

有限状态机的状态转移过程:

起始为0:

  当输入空格时,状态仍为0,

  输入为符号时,状态转为3,3的转换和0是一样的,除了不能再接受符号,故在0的状态的基础上,把接受符号置为-1;

  当输入为数字时,状态转为1, 状态1的转换在于无法再接受符号,可以接受空格,数字,点,指数;状态1为合法的结束状态;

  当输入为点时,状态转为2,状态2必须再接受数字,接受其他均为非法;

  当输入为指数时,非法;

状态1:

  接受数字时仍转为状态1,

  接受点时,转为状态4,可以接受空格,数字,指数,状态4为合法的结束状态,

  接受指数时,转为状态5,可以接受符号,数字,不能再接受点,因为指数必须为整数,而且必须再接受数字;

状态2:

  接受数字转为状态4;

状态3:

  和0一样,只是不能接受符号;

状态4:

  接受空格,合法接受;

  接受数字,仍为状态4;

  接受指数,转为状态5,

状态5:

  接受符号,转为状态6,状态6和状态5一样,只是不能再接受符号,

  接受数字,转为状态7,状态7只能接受空格或数字;状态7为合法的结束状态;

状态6:

  只能接受数字,转为状态7;

状态7:

  接受空格,转为状态8,状态7为合法的结束状态;

  接受数字,仍为状态7;

状态8:

  接受空格,转为状态8,状态8为合法的结束状态;

AC代码

class Solution {
public:
bool isNumber(string s) {
//输入参数枚举
enum InputType{
INVALID, //代表不正确
SPACE, // 代表空格
SIGN, // 代表符号
DIGIT,
DOT, //代表点符号
EXPONENT, //代表科学计算
NUM_INPUTS //数字输入
}; int transitionTable[][NUM_INPUTS] =
{
-1, 0, 3, 1, 2, -1, // next states for state 0
-1, 8, -1, 1, 4, 5, // next states for state 1
-1, -1, -1, 4, -1, -1, // next states for state 2
-1, -1, -1, 1, 2, -1, // next states for state 3
-1, 8, -1, 4, -1, 5, // next states for state 4
-1, -1, 6, 7, -1, -1, // next states for state 5
-1, -1, -1, 7, -1, -1, // next states for state 6
-1, 8, -1, 7, -1, -1, // next states for state 7
-1, 8, -1, -1, -1, -1, // next states for state 8
}; int state = 0, i = 0;
while (s[i] != '\0')
{
InputType inputType = INVALID;
if (isspace(s[i]))
inputType = SPACE;
else if (s[i] == '+' || s[i] == '-')
inputType = SIGN;
else if (isdigit(s[i]))
inputType = DIGIT;
else if (s[i] == '.')
inputType = DOT;
else if (s[i] == 'e' || s[i] == 'E')
inputType = EXPONENT; state = transitionTable[state][inputType]; if (state == -1)
return false;
else
i++;
} return state == 1 || state == 4 || state == 7 || state == 8; }
};

GitHub测试程序源码

LeetCode(65) Valid Number的更多相关文章

  1. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  2. LeetCode(202) Happy Number

    题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...

  3. LeetCode(36)Valid Sudoku

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. LeetCode(65):有效数字

    Hard! 题目描述: 验证给定的字符串是否为数字. 例如:"0" => true" 0.1 " => true"abc" =& ...

  5. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

  6. LeetCode(242)Valid Anagram

    题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...

  7. LeetCode(49)-Valid Parentheses

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  8. LeetCode(125) Valid Palindrome

    题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...

  9. LeetCode(20)Valid Parentheses

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

随机推荐

  1. hud3371 Connect the Cities 简单最小生成树

    //我看过Discuss说不能用克鲁斯卡尔因为有很多边 //但是只能用G++过,C++的确超时 #include <stdio.h> #include <string.h> # ...

  2. 449B

    B. Chtholly's request time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  3. zabbix网络发现主机

    1 功能介绍 默认情况下,当我在主机上安装agent,然后要在server上手动添加主机并连接到模板,加入一个主机组. 如果有很多主机,并且经常变动,手动操作就很麻烦. 网络发现就是主机上安装了age ...

  4. IDEA远程调试hadoop程序

    远程调试Hadoop各组件 Hadoop学习之配置Eclipse远程调试Hadoop IDEA远程调试hadoop Hadoop 研发之远程调试详细剖析--WordCount V2.0 eclipse ...

  5. Mysql查询语句的 where子句、group by子句、having子句、order by子句、limit子句

    Mysql的各个查询语句 一.where子句   语法:select *|字段列表 from 表名 where 表达式.where子句后面往往配合MySQL运算符一起使用(做条件判断) 作用:通过限定 ...

  6. 题解报告:NYOJ #78 圈水池(打印凸包顶点)

    描述: 有一个牧场,牧场上有很多个供水装置,现在牧场的主人想要用篱笆把这些供水装置圈起来,以防止不是自己的牲畜来喝水,各个水池都标有各自的坐标,现在要你写一个程序利用最短的篱笆将这些供水装置圈起来!( ...

  7. ASP.NET MVC Identity 兩個多個連接字符串問題解決一例

    按照ASP.NET MVC Identity建立了一個用戶權限管理模塊,由于還要加自己已有的數據庫,所以建立了一個實體模型,建立了之后,發現登錄不了: 一直顯示“Login in failed for ...

  8. centos设置系统时间

    系统日期设定成1996年6月10日上午9点date -s 06/22/96date -s 09:00:00

  9. VBA 从sql存储过程-记录集-导入

    cnn.Open cnnstr cmd.ActiveConnection = cnn cmd.CommandTimeout = 120 cmd.CommandText = "dbo.t_bi ...

  10. Vue之过滤器的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...