leetcode#65 Valid Number

Source: Internet
Author: User

Original title Address

A good question to study the "engineering" ability, but you can explain the rules do not.

Solve with state machine:

When the string has been traversed:

1. The green State must appear at least 1

2. The red state cannot be used as the end state

Code:

1 enumStatus {2 pre_padding,3 Sign ,4 Number_before_point,5 Point ,6 Number_after_point,7 EXPONENT,8 Exponent_sign,9 Exponent_number,Ten post_padding One }; A  - BOOLIsnumber (Const Char*s) { -   BOOLmask[Ten] = {false}; theStatus State =pre_padding; -  -    while(*s) { -     Switch(state) { +      Casepre_padding: -       if(*s = =' ') Mask[state = pre_padding] =true; +       Else if(*s = ='+'|| *s = ='-') Mask[state = sign] =true; A       Else if(*s >='0'&& *s <='9') Mask[state = Number_before_point] =true; at       Else if(*s = ='.') Mask[state = Point] =true; -       Else return false; -        Break; -      CaseSign : -       if(*s >='0'&& *s <='9') Mask[state = Number_before_point] =true; -       Else if(*s = ='.') Mask[state = Point] =true; in       Else return false; -        Break; to      CaseNumber_before_point: +       if(*s >='0'&& *s <='9') Mask[state = Number_before_point] =true; -       Else if(*s = ='.') Mask[state = Point] =true; the       Else if(*s = ='e') Mask[state = EXPONENT] =true; *       Else if(*s = =' ') Mask[state = post_padding] =true; $       Else return false;Panax Notoginseng        Break; -      CasePoint : the       if(*s >='0'&& *s <='9') Mask[state = Number_after_point] =true; +       Else if(*s = ='e') Mask[state = EXPONENT] =true; A       Else if(*s = =' ') Mask[state = post_padding] =true; the       Else return false; +        Break; -      CaseNumber_after_point: $       if(*s >='0'&& *s <='9') Mask[state = Number_after_point] =true; $       Else if(*s = ='e') Mask[state = EXPONENT] =true; -       Else if(*s = =' ') Mask[state = post_padding] =true; -       Else return false; the        Break; -      CaseEXPONENT:Wuyi       if(*s >='0'&& *s <='9') Mask[state = Exponent_number] =true; the       Else if(*s = ='+'|| *s = ='-') Mask[state = Exponent_sign] =true; -       Else return false; Wu        Break; -      Caseexponent_sign: About       if(*s >='0'&& *s <='9') Mask[state = Exponent_number] =true; $       Else return false; -        Break; -      CaseExponent_number: -       if(*s >='0'&& *s <='9') Mask[state = Exponent_number] =true; A       Else if(*s = =' ') Mask[state = post_padding] =true; +       Else return false; the        Break; -      Casepost_padding: $       if(*s = =' ') Mask[state = post_padding] =true; the       Else return false; the        Break; the     default: the       return false; -     } ins++; the   } the  About   return! (state = =pre_padding the|| state = = Sign the|| state = =EXPONENT the|| state = =exponent_sign) +&&(Mask[number_before_point] -||Mask[number_after_point]); the}

leetcode#65 Valid Number

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.