[Leetcode] 65. Valid number Java

Source: Internet
Author: User

Topic:

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 on the reload button to reset your code Definition.

test instructions and analysis: determines whether a string can be converted to a legitimate The number , the main note that at most only one decimal point and e,e cannot have the decimal point first or the last digit can be a decimal point, +/-can only appear in the first bit or after the first bit of E.

Code:

class Solution {public Boolean isnumber (string s) {//Determine if a string is a valid number        s = S.trim ();        Boolean pointseen = false;        Boolean eseen = false;        Boolean isnumber = false;            for (int i=0;i<s.length (); i++) {Char temp = S.charat (i);            if (temp>= ' 0 ' && temp<= ' 9 ') {Isnumber = true; }else if (temp = = '. ')                {if (Eseen | | pointseen) return FALSE;            Pointseen = true;                }else if (temp = = ' E ') {if (Eseen | |!isnumber) return FALSE;                Eseen = true;            Isnumber = false;                    }else if (temp = = ' + ' | | temp== '-') {//can only be in the first or isnumber followed by +/-if (i!=0 && s.charat (i-1)! = ' E ')                return false;            Isnumber = false;        }else return false;    } return isnumber; }}

  

[Leetcode] 65. Valid number Java

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.