65. Valid Number
Posted wentiliangkaihua
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了65. Valid Number相关的知识,希望对你有一定的参考价值。
Validate if a given string can be interpreted as a decimal number.
Some examples:"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
" -90e3 "
=> true
" 1e"
=> false
"e3"
=> false
" 6e-1"
=> true
" 99e2.5 "
=> false
"53.5e93"
=> true
" --6 "
=> false
"-+3"
=> false
"95a54e53"
=> false
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:
- Numbers 0-9
- Exponent - "e"
- Positive/negative sign - "+"/"-"
- Decimal point - "."
Of course, the context of these characters also matters in the input.
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.
class Solution public boolean isNumber(String s) int state = 0; s = s.trim();//去除头尾的空格 //遍历所有字符,当做输入 for (int i = 0; i < s.length(); i++) switch (s.charAt(i)) //输入正负号 case ‘+‘: case ‘-‘: if (state == 0) state = 1; else if (state == 4) state = 6; else return false; break; //输入数字 case ‘0‘: case ‘1‘: case ‘2‘: case ‘3‘: case ‘4‘: case ‘5‘: case ‘6‘: case ‘7‘: case ‘8‘: case ‘9‘: //根据当前状态去跳转 switch (state) //这块是state,是integer,不是char case 0: case 1: case 2: state = 2; break; case 3: state = 3; break; case 4: case 5: case 6: state = 5; break; case 7: state = 8; break; case 8: state = 8; break; default: return false; break; //小数点 case ‘.‘: switch (state) case 0: case 1: state = 7; break; case 2: state = 3; break; default: return false; break; //e case ‘e‘: switch (state) case 2: case 3: case 8: state = 4; break; default: return false; break; default: return false; //橙色部分的状态代表合法数字 return state == 2 || state == 3 || state == 5 || state == 8;
以上是关于65. Valid Number的主要内容,如果未能解决你的问题,请参考以下文章