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

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

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

(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. Codeforces 294B Shaass and Bookshelf(记忆化搜索)

    题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #inc ...

  2. 在wpf窗体上添加用户控件

    1.引用用户控件的命名控件 xmlns:my="clr-namespace:WpfApplicationDemo.Control" 2.把用户控件添加到窗体中 <my:Use ...

  3. ***codeigniter 2.2 affected_rows()返回值不准确

    http://blog.icodeu.com/?p=596 问题描述今天在完成一个项目调用想要检验一下计划插入的数据是否都正常插入了.调用insert_batch()方法插入一百多条数据的时候发现af ...

  4. ExtJs之文本框及数字输入

    结合HTML来理解, 比较容易. <!DOCTYPE html> <html> <head> <title>ExtJs</title> &l ...

  5. Codeforces Round #336 (Div. 2)C. Chain Reaction DP

    C. Chain Reaction   There are n beacons located at distinct positions on a number line. The i-th bea ...

  6. linux kill命令详解

    1.命令格式: kill[参数][进程号] 2.命令功能: 发送指定的信号到相应进程.不指定型号将发送SIGTERM(15)终止指定进程.如果任无法终止该程序可用“-KILL” 参数,其发送的信号为S ...

  7. Sold out

    When will the writer see the play? 'The play may begin at any moment,'I said. 'It may have begun alr ...

  8. lintcode:打劫房屋 III

    题目 打劫房屋 III 在上次打劫完一条街道之后和一圈房屋之后,窃贼又发现了一个新的可以打劫的地方,但这次所有的房子组成的区域比较奇怪,聪明的窃贼考察地形之后,发现这次的地形是一颗二叉树.与前两次偷窃 ...

  9. lintcode:将二叉查找树转换成双链表

    题目 将一个二叉查找树按照中序遍历转换成双向链表 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5. ...

  10. java连接access数据库

    完整代码: package odbcj; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prep ...