LeetCode-Valid Number - 有限状态机 & 正则表达式

牛逼啊,见这种方式的编程,记录下,思路很新颖,非常好。。。。


今天看到了大神的解法(https://github.com/fuwutu/LeetCode/blob/master/Valid%20Number.cpp),

用有限状态机,非常简洁,不需要复杂的各种判断!

先枚举一下各种合法的输入情况:

1.空格+ 数字 +空格

2.空格+ 点 + 数字 +空格

3.空格+ 符号 + 数字 + 空格

4.空格 + 符号 + 点 + 数字 +空格

5.空格 + (1, 2, 3, 4) + e + (1, 2, 3, 4) +空格

组后合法的字符可以是:

1.数字

2.空格

有限状态机的状态转移过程:

起始为0:

  当输入空格时,状态仍为0,

  输入为符号时,状态转为3,3的转换和0是一样的,除了不能再接受符号,故在0的状态的基础上,把接受符号置为-1;

  当输入为数字时,状态转为1, 状态1的转换在于无法再接受符号,可以接受空格,数字,点,指数;状态1为合法的结束状态;

  当输入为点时,状态转为2,状态2必须再接受数字,接受其他均为非法;

  当输入为指数时,非法;

状态1:

  接受数字时仍转为状态1,

  接受点时,转为状态4,可以接受空格,数字,指数,状态4为合法的结束状态,

  接受指数时,转为状态5,可以接受符号,数字,不能再接受点,因为指数必须为整数,而且必须再接受数字;

状态2:

  接受数字转为状态4;

状态3:

  和0一样,只是不能接受符号;

状态4:

  接受空格,合法接受;

  接受数字,仍为状态4;

  接受指数,转为状态5,

状态5:

  接受符号,转为状态6,状态6和状态5一样,只是不能再接受符号,

  接受数字,转为状态7,状态7只能接受空格或数字;状态7为合法的结束状态;

状态6:

  只能接受数字,转为状态7;

状态7:

  接受空格,转为状态8,状态7为合法的结束状态;

  接受数字,仍为状态7;

状态8:

  接受空格,转为状态8,状态8为合法的结束状态;

复制代码
 1 class Solution
 2 {
 3 public:
 4     bool isNumber(const char *s)
 5     {
 6         enum InputType
 7         {
 8             INVALID,    // 0
 9             SPACE,      // 1
10             SIGN,       // 2
11             DIGIT,      // 3
12             DOT,        // 4
13             EXPONENT,   // 5
14             NUM_INPUTS  // 6
15         };
16         
17         int transitionTable[][NUM_INPUTS] =
18         {
19             -1,  0,  3,  1,  2, -1,     // next states for state 0
20             -1,  8, -1,  1,  4,  5,     // next states for state 1
21             -1, -1, -1,  4, -1, -1,     // next states for state 2
22             -1, -1, -1,  1,  2, -1,     // next states for state 3
23             -1,  8, -1,  4, -1,  5,     // next states for state 4
24             -1, -1,  6,  7, -1, -1,     // next states for state 5
25             -1, -1, -1,  7, -1, -1,     // next states for state 6
26             -1,  8, -1,  7, -1, -1,     // next states for state 7
27             -1,  8, -1, -1, -1, -1,     // next states for state 8
28         };
29         
30         int state = 0;
31         while (*s != '\0')
32         {
33             InputType inputType = INVALID;
34             if (isspace(*s))
35                 inputType = SPACE;
36             else if (*s == '+' || *s == '-')
37                 inputType = SIGN;
38             else if (isdigit(*s))
39                 inputType = DIGIT;
40             else if (*s == '.')
41                 inputType = DOT;
42             else if (*s == 'e' || *s == 'E')
43                 inputType = EXPONENT;
44             
45             state = transitionTable[state][inputType];
46             
47             if (state == -1)
48                 return false;
49             else
50                 ++s;
51         }
52         
53         return state == 1 || state == 4 || state == 7 || state == 8;
54     }
55 };


package Level2;  
   
/** 
 * Valid Number 
 *  
 *  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. 
 
 
 * 
 */ 
public class S65 {  
   
    public static void main(String[] args) {  
   
    }  
   
    public boolean isNumber(String s) {  
        if(s.trim().isEmpty()){  
            return false;  
        }  
        String regex = "[-+]?(\\d+\\.?|\\.\\d+)\\d*(e[-+]?\\d+)?";  
        if(s.trim().matches(regex)){  
            return true;  
        }else{  
            return false;  
        }  
    }  
}  


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值