题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形。

思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已。

  下面取巧来解决,分情况讨论:

(1)整数

(2)浮点数

(3)整数e整数

(4)浮点数e整数

  只有以上4种情况。但是要数之前可能带1个符号,这个可以直接过滤1个,而不影响结果。而且,其包含关系是从上到下扩展的,(1)扩展到(2),(3)扩展到 (4)。

  

  那么先解决符号 e 之前的,必须满足:正负号至多1个,有数字1个以上,点至多1个。

  如果没有e,那么可以结束了。若有e,跳过1个e,再继续。

  解决e后面的,必须满足:正负号至多1个,有数字1个以上,没有点。

 class Solution {
public:
bool isNumber(string s) {
int i=, d=;
while( !s.empty() && s[s.size()-]==' ' ) //直接删后缀空格
s.erase(s.end()-); while(i<s.size() && s[i]==' ') //忽略前缀空格
i++;
if( s[i]=='+' || s[i]=='-' ) i++; //这个符号出现了等于没出现 while(i<s.size() && isdigit(s[i]))
i++,d++;
if(i<s.size()&&s[i]=='.') i++;
while(i<s.size() && isdigit(s[i]))
i++,d++;
if(!d) return false; //保证e之前不为空,且合法 //以上是e前的情况
if(i==s.size()) return true;
else if(s[i]!='e') return false; //有e出现则e后不能空。无e则出错 //下面e后的情况
if(s[++i]=='+'||s[i]=='-') i++; //可以有符号 d=;
while(i<s.size()&& isdigit(s[i])) //只能出现整数了
i++,d++;
if(d && i==s.size()) return true;
else return false;
}
};

AC代码

LeetCode Valid Number 有效数字(有限自动机)的更多相关文章

  1. leetCode 65.Valid Number (有效数字)

    Valid Number  Validate if a given string is numeric. Some examples: "0" => true " ...

  2. LeetCode: Valid Number 解题报告

    Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  3. [LeetCode] Valid Number 验证数字

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  4. [leetcode]Valid Number @ Python

    原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...

  5. LeetCode——Valid Number

    Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...

  6. Leetcode Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  7. leetcode - valid number 正则表达式解法

    import java.util.regex.Pattern; public class Solution { Pattern p = Pattern.compile("^[\\+\\-]? ...

  8. [LeetCode] Valid Number 确认是否为数值

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  9. [Swift]LeetCode65. 有效数字 | Valid Number

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

随机推荐

  1. POJ 3696 The Luckiest number (欧拉函数,好题)

    该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...

  2. php怎么获取checkbox复选框的内容?

    由于checkbox属性,所有必须把checkbox复选择框的名字设置为一个如果checkbox[],php才能读取,以数据形式,否则不能正确的读取checkbox复选框的值哦. <form n ...

  3. POJ 2653 Pick-up sticks(线段相交)

    题目链接 题意 : 把每根棍往地上扔,找出最后在上面的棍,也就是说找出所有的没有别的棍子压在它的上面的棍子. 思路 : 对于每根棍子,压在他上面的棍子一定是在它之后扔的棍子,所以在找的时候只要找它之后 ...

  4. poj 2425 A Chess Game 博弈论

    思路:SG函数应用!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include< ...

  5. SQL四舍五入及两种舍入

    round() 遵循四舍五入把原值转化为指定小数位数,如:round(1.45,0) = 1;round(1.55,0)=2floor()向下取整 如:floor(1.45)= 1,floor(1.5 ...

  6. return x>y?x:y ?:啥意思?

    ? :是一个三目运算符,先判断‘?’前面的,若为真,执行‘?’后面语句,else,执行‘:’后面语句! return (x>y?x:y) 即if(x>y) 执行xelse执行y

  7. java编译错误:varargs 方法的非 varargs 调用

    转自:http://www.blogjava.net/ideame/archive/2007/03/23/105849.html 警告: 最后一个参数使用了不准确的变量类型的 varargs 方法的非 ...

  8. 关于WII光枪定位的设计(转)

    方法1. 简单1 LED方法 这是一个很忽悠的方法,把LED看成是屏幕中心,把光枪摄像头的视野范围看作是屏幕范围. 假设WII枪头摄像头的数据范围为[0,1024]*[0,768],显示器屏幕分辨率为 ...

  9. Why you have so few friends?

    Why you have so few friends?十个原因告诉你:为什么你的朋友那么少1. You Complain A Lot 你总是抱怨 If you’re constantly compl ...

  10. 数值的整数次方(剑指offer面试题11)

    实现函数 double Power(double base, int exponent),即乘方运算. 考虑问题 exponet < 0 , 可以转化为 1.0 / Power(base, -1 ...