LeetCode(65) 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.
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;
}
};
LeetCode(65) Valid Number的更多相关文章
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(202) Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- LeetCode(36)Valid Sudoku
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode(65):有效数字
Hard! 题目描述: 验证给定的字符串是否为数字. 例如:"0" => true" 0.1 " => true"abc" =& ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- 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 ...
- LeetCode(49)-Valid Parentheses
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- LeetCode(125) Valid Palindrome
题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...
- LeetCode(20)Valid Parentheses
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
随机推荐
- 洛谷 P1339 [USACO09OCT]热浪Heat Wave
题目链接:https://www.luogu.org/problemnew/show/P1339 解题思路: 一道简单的最短路水题,dijkstra解法模板思路:https://www.cnblogs ...
- 基于CentOS 7.2个人网盘的实现
首先使用YUM安装依赖环境: [root@sishen ~]#yum install python python-setuptools python-imaging python-ldap pytho ...
- PHP 讓 json_encode() 指定回傳格式
PHP 回傳 JSON 很方便, 只要將資料經過 json_encode() 就解決了. 不過因為 PHP 自動轉換型別, 造成很多資料都習慣存成字串, 希望在輸出 JSON 的時候, 數字部份可以輸 ...
- mongoDB内置文档定义
在最近的设计数据库时,犯了一个低级的错误,就是设置内置文档是定义了错误了,导致数据取不出,去找了很多资料都无法解决.最后看了一了一下自己设置的model文件.配置错误,所以导致数据取不出了. 数据库时 ...
- 刷ID卡的就餐系统
需求分析:公司旧的考勤系统,缺 “就餐”功能模块,不能查询和统计每天的就餐人数.故需开发一个简易的“刷ID卡的就餐系统”,三 部 分组成,一部分为人事资料的增删改查,二部分为处理从“刷卡就餐机”采集的 ...
- NodeJS&&前端思考
做大型软件(工程化): 1.测试相关 tdd / bdd 测试覆盖率 2.规范化 standard.各种 lint.hint 3.构建相关 gulp.grunt.webpack,大量插件 4.生成器 ...
- AJPFX总结Collection集合(下)
List集合特有方法 特有方法.凡是可以操作角标的方法都是该体系特有的方法. 增 add(index,element);在指定位置添加元素 addAll(index ...
- AJPFX关于枚举,泛型详解
枚举类型是JDK5.0的新特征.Sun引进了一个全新的关键字enum来定义一个枚举类.下面就是一个典型枚举类型的定义:public enum Color{RED,BLUE,BLACK,YELLOW,G ...
- vs 2015 编译cocos2dx 报错
VS 2015 compiling cocos2d-x 3.3 error “fatal error C1189: #error: Macro definition of snprintf confl ...
- Es6学习笔记(7)----数组的扩展
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ 数组的扩展 1.扩展运算符:可以将数组转化成逗号隔离的单个参数...[1,2,3] //控制台运 ...