State machine for handling floating-point numbers

Source: Internet
Author: User

The problem comes from Leetcode 065:

Valid number

If you use regular expressions to write a floating-point resolution, it's probably:

[+-]? ([0-9]+\.?) | (\. [0-9])) [0-9]* ([ee][+-]?[ 0-9]+)?

I myself use dot to draw a state machine out:

If you write code to simulate the state machine, you can split the "EE" into two parts of the processing; the front part of "EE" can also be split into "." Before and after two parts (if a bit) to write.

This problem on the Leetcode difficulty for hard, you can see this small details, more able to test the basic skills.

------------------

Due to the word limit, attach my words this state machine uses the source code:

1 digraph {2 Graph [fontname = "Microsoft Yahei", fontsize=16 ranksep=1.0 splines=1 overlap=false size= "+," compound=true]
    3 Edge [fontname = "Microsoft Yahei", fontsize=16]4 node [fontname = "Microsoft Yahei", fontsize=16 weight=2 rank=same width=1 RANKDIR=LR]5 6 p0[label= "Start"]7 p2[label= "Initial-check"]8 p4[label= "Dot-first"]9 p5[label= "AFTER-E"]Ten p6[label= "After-e-mark"] One p7[label= "Almost-done"] A p8[label= "End"] - p9[label= "Sign-mark"] - pa[label= "After-dot"] the  -  - P0-P9[label= "[+-]"] - P0, p9[label= "blank-string"] +  - P9, P4[label= "". +      A p9-P2[label= "[0-9]"] at  - P2-p2[label= "[0-9]"] - P2, pa[label= "". - P2, pa[label= "blank-string"] - P4-Pa[label= "[0-9]"] - PA-and Pa[label= "[0-9]"] in PA-p8[label= "$"] - PA-P5[label= "[Ee]"] to P5-p6[label= "[+-]"] + P5, p6[label= "blank-string"] - P6-P7[label= "[0-9]"] the P7-P7[label= "[0-9]"] * P7, p8[label= "$"] $}

State machine for handling floating-point numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.