Java for Leetcode 065 Valid number

Source: Internet
Author: User

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.

Problem Solving Ideas:

The boundary conditions are quite good "1", ".", "1.1e+.1" is also correct. The solution is to trim it first, then divide it according to E, and then make a judgment about E before and after the Java implementation is as follows:

    public boolean isnumber (String s) {s = S.trim (); string[] Splitarr = S.split ("e"); if (s.length () = = 0 | | S.charat (0) = = ' E ' | | s.charat (S.LENGTH ()-1) = = ' E ' | | splitarr.l Ength > 2) return false;for (int k = 0; k < splitarr.length; k++) {String str = Splitarr[k];boolean Isdecimal = False if (Str.charat (0) = = '-' | | Str.charat (0) = = ' + ') str = str.substring (1); if (str.length () = = 0) return false;for (int i = 0 ; I < str.length (); i++) {if (' 0 ' <= str.charat (i) && Str.charat (i) <= ' 9 ') Continue;else if (Str.charat (i) = = '. ' &&!is Decimal) {if (k = = 0 && str.length () > 1) isdecimal = True;elsereturn false;} Elsereturn false;}} return true;}

Java for Leetcode 065 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.