LeetCode OJ - Valid Number
来源:互联网 发布:在access数据库中 表是 编辑:程序博客网 时间:2024/05/16 11:22
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.
分析:状态机的使用,虽然一直在看代码,状态机很熟,这里真正是实现了状态机
class Solution {//finite automatapublic: vector<vector<int> > transTable; int inputType(char a) { if(a == ' ') return 0; else if(a == '+' || a == '-') return 1; else if(a >= '0' && a <= '9') return 2; else if(a == '.') return 3; else if(a == 'e') return 4; else return 5;//illegal input } void initTransTable() { transTable.resize(9, vector<int>(5, -1)); transTable[0][0] = 0; transTable[0][1] = 1; transTable[0][2] = 2; transTable[0][3] = 7; transTable[7][2] = 3; transTable[1][2] = 2; transTable[1][3] = 7; transTable[2][2] = 2; transTable[2][0] = 6; transTable[2][3] = 3; transTable[2][4] = 4; transTable[3][2] = 3; transTable[3][4] = 4; transTable[3][0] = 6; transTable[4][1] = 8; transTable[8][2] = 5; transTable[4][2] = 5; transTable[5][2] = 5; transTable[5][0] = 6; transTable[6][0] = 6; } bool isNumber(const char *s) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s == NULL) return false; initTransTable(); int curState = 0; while(*s != '\0') { int curInput = inputType(*s); if(curInput == 5) return false; curState = transTable[curState][curInput]; if(curState == -1) return false; s++; } if(curState == 2 || curState == 3 || curState == 5 || curState == 6) return true; else return false; }};
0 0
- LeetCode OJ:Valid Number
- LeetCode OJ - Valid Number
- LeetCode OJ Valid Number
- [LeetCode OJ]Valid Number
- http://oj.leetcode.com/problems/valid-number/
- LeetCode OJ 65 Valid Number[hard]
- LeetCode OJ算法题(六十六):Valid Number
- LeetCode OJ 之 Valid Number (有效数字的判断)
- LeetCode OJ 系列之65 Valid Number --Python
- 【LeetCode OJ】Valid Parentheses
- LeetCode OJ:Valid Palindrome
- LeetCode OJ:Valid Sudoku
- LeetCode OJ:Valid Parentheses
- LeetCode OJ - Valid Palindrome
- LeetCode OJ - Valid Parentheses
- LeetCode OJ - Valid Sudoku
- LeetCode OJ Valid Sudoku
- LeetCode OJ Valid Palindrome
- hdu 3938
- CX控件记录
- Linux - 判断文件/目录是否存在/具有权限
- jQuery插入,复制、替换和删除节点
- Ajax使用AntiForgeryToken,记录一下
- LeetCode OJ - Valid Number
- CentOS上编译安装OpenCV-2.3.1与ffmpeg-2.1.2
- LeetCode 29 Best Time to Buy and Sell Stock III
- 沁园春
- [编程好习惯] 精确包含头文件(转)
- WebLogic Enterprise JavaBean 编程
- java读取.properties配置文件的几种方法 .
- js正则表达式验证手机号,邮箱,QQ,密码
- VS.添加不了引用Microsoft.Office.Interop