LeetCode-Valid Number-合法数字-状态机
2014-09-30 16:07
337 查看
https://oj.leetcode.com/problems/valid-number/
判断一个字符串是否为合法字符。
这个题目画出状态机,可以做一个二维的状态表,然后回溯。注意这个状态表中其实没有分支,所以其实不需要真的回溯在往下搜索。
判断一个字符串是否为合法字符。
这个题目画出状态机,可以做一个二维的状态表,然后回溯。注意这个状态表中其实没有分支,所以其实不需要真的回溯在往下搜索。
int trans[10][6]={ { 0, 1, 2, 9,-1,-1}, {-1,-1, 2, 9,-1,-1}, { 8,-1, 2, 3, 5,-1}, { 8,-1, 4,-1, 5,-1}, //. { 8,-1, 4,-1, 5,-1}, //\d+ {-1, 6, 7,-1,-1,-1},//e {-1,-1, 7,-1,-1,-1},//- { 8,-1, 7,-1,-1,-1},//\d+ { 8,-1,-1,-1,-1,-1},//$ {-1,-1, 4,-1,-1,-1} //. }; class Solution { public: int m,n; string s; int GetItem(char a){ if (a==' '){return 0;} if (a=='-' || a=='+'){return 1;} if (a>='0' && a<='9'){return 2;} if (a=='.'){return 3;} if (a=='e'){return 4;} return 5; } bool Solve(int p,int stat){ if (p==n){ if (stat==2 || stat==3 || stat==4|| stat==7|| stat==8){return true;} return false; } int curT=GetItem(s[p]); int ns=trans[stat][curT]; if (ns==-1){return false;} return Solve(p+1,ns); } bool isNumber(const char *s) { this->s=string(s); n=this->s.length(); return Solve(0,0); } };
相关文章推荐
- [Leetcode] valid number 验证数字
- LeetCode65——Valid Number(使用DFA)来判断字符串是否为数字
- LeetCode Valid Number(判断字符串是否是合法的数字表示 )
- 【LeetCode】- Valid Number(有效数字)
- [LeetCode] Valid Number 验证数字
- LeetCode 65. Valid Number(合法数字→最差的题!)
- [LeetCode]—Valid Number 判断字符串是否为合法数据(科学计数法)
- Valid Number,判断是否为合法数字
- LeetCode--Find All Numbers Disappeared in an Array(找到数组中未出现的数字)
- [leetcode, python] Total Hamming Distance 多个数字之间的汉明距离
- Leetcode刷题记——9. Palindrome Number(回文数字)
- [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数
- 【前端】输入金额时如何判断数字合法
- LeetCode 65 Valid Number
- [LeetCode118]Valid Number
- LeetCode - Valid Number
- leetCode 65.Valid Number (有效数字)
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
- Leetcode 08 String to Integer (atoi)(实现c++atoi即字符串转数字的功能)
- ip输入框键入.或者合法数字自动选择下一个输入框效果