[Leetcode]valid number

Source: Internet
Author: User
Tags valid
Problem-solving ideas: First of all to clarify what is valid number, the topic gives the following example "0" = true "0.1" = True "abc" and false "1 a" and false "2e "+" + True add: "2e" = false "2e+" = False "2e+6" + = true "1 1" = false "+" = False "+5" =&G T  True ". 1" + = true "1." = True
Then, a valid number can be decomposed as follows 1, prefix white space [optional] 2, + or-number [optional] 3, Integer or decimal 4, power [optional] if the memory In ' e ', then ' e ' must exist after an integer (can be positive minus) 5, postfix white space [optional]
Pre-condition: I is used to traverse string s,isnumber to record whether part-3 exists, end condition: If I can traverse the string s in order of the above pattern, and isnumber== true, then you can determine that s is a valid number


public class Solution {public Boolean isnumber (String s) {Boolean isnumber = false;
        int i = 0;

        int n = s.length ();
        while (I < n && s.charat (i) = = ") i++;

        if (I < n && (S.charat (i) = = ' + ' | | S.charat (i) = = '-')) i++;
            while (I < n && character.isdigit (S.charat (i))) {i++;
        Isnumber = true; } if (I < n && s.charat (i) = = '. ')
            {i + +;
                while (I < n && character.isdigit (S.charat (i))) {i++;
            Isnumber = true;
            }} if (Isnumber && i < n && s.charat (i) = = ' E ') {i + +;
            Isnumber = false;
            if (I < n && (S.charat (i) = = ' + ' | | S.charat (i) = = '-')) i++;
                while (I < n && character.isdigit (S.charat (i))) {i + +;
            Isnumber = true;

  } 
        }      while (I < n && s.charat (i) = = ") i++;

    return Isnumber && i = = N; }
}


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.