Leetcode 65.Valid Number (valid numbers)

Source: Internet
Author: User

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.

Update (2015-02-10):

The signature of the C + + function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your co De definition.


Idea: This problem is really difficult, and most importantly, I do not know the rules of a valid number ... Not even the rules do not know how to play a good game, so decisive hanging 10 times 8 times, and finally did not fully correct. Temporarily put someone else's code out below, there is time to do a good study:

public class Solution {public Boolean isnumber (String s) {//Start typing your Java solution below//D O not write main () function/* "0" = "0.1" + true "abc" = FAL        Se "1 a" + = false "+-2e10" = True//*///check input.        if (S==null | | s.length () ==0) return false;        int sz = s.length ();                int i=0;                while (I<sz && s.charat (i) = = ") ++i;        Boolean space = false;        Boolean exp = false;        Boolean dot = false;        Boolean number = false;                Boolean neg = false;            for (; i<sz; i++) {char c = s.charat (i);            if (c== ") {space = true;            } else if (Space==true) {return false;                } else if ((c== ' e ' | | c== ' e ') && exp==false && number==true) {exp = true;                Number = false; Dot =True            Neg = false;                } else if (c== '. ' && dot==false) {dot = true;               Neg = true;            Number = false;            } else if (c>= ' 0 ' && c<= ' 9 ') {number = true;            } else if ((c== ' + ' | | c== '-') && neg==false && number==false) {neg = true;            } else {return false;    }} return number; }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 65.Valid Number (valid 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.