Leetcode | | 65, Valid number

Source: Internet
Author: User

Problem:

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.

Hide TagsMath StringTest instructions: Determines whether a string can effectively represent a number, the representation of a number is: positive negative, Integer, fractional, scientific notation when submitted, Leetcode judgment '. 1 ' and ' 1. ' is also a valid number!! It's a little funny, it's an assessment of the system, and no one in reality thinks it works. It also caused my submission to fail.

Thinking:

(1) Details of the problem, note the ' E ' and ' e ' case

(2) Space Judgment function isspace (char*) can be called, reduce the workload

Code

By version: Refer to http://www.cnblogs.com/remlostime/archive/2012/11/18/2775938.html, note parameter char *s, not string, previous version


My own version: failed, Reason (1)leetcode judgment '. 1 ' and ' 1. ' Also a valid number (2) case ' E ' e ' does not pay attention to the distinction (3) ...

Class Solution {Public:bool Isnumber (string s) {int n=s.size ();        if (n==0) return false;            if (n==1) {if (s.at (0) >= ' 0 ' && s.at (0) <= ' 9 ') return true;        else return false;            } if (s.at (0) = = ") {string::iterator start=s.begin ();            String::iterator End=start;            while (*end== ') end++;            S.erase (Start,end);        return Isnumber (s);            } if (s.at (n-1) = = ") {string::iterator end=s.end ();            String::iterator start=end-1;            while (*start== ') start--;            S.erase (++start,end);        return Isnumber (s);            } if (s.at (0) = = ' + ' | | s.at (0) = = '-') {S.erase (S.begin (), S.begin () +1);        return Isnumber (s);        } int count1=0;        int index1=0;        int count2=0;        int index2=0; int count3=0;        for (int i=0;i<n;i++) {if ((s.at (i) >= ' 0 ' &&s.at (i) <= ' 9 ') | | s.at (i) = = '. ' | | s.at (                i) = = ' E ') {if (s.at (i) = = '. ')                    {count1++;                Index1=i;                    } else if (s.at (i) = = ' E ') {count2++;                Index2=i;            } else count3++;        } else return false;        }//for if (count3==n) return true;        if (count1>1 | | count2>1) return FALSE;            if (count2==1) {if (index2==0 | | index2==n-1) return FALSE;        else return Check2 (S,INDEX2); } if (count1==1) {if (index1==0 | |            INDEX1==N-1) return false;        else return Check1 (S,INDEX1); }}protected:bool Check1 (string s, int loc)//decimal does not contain e {for (int i=0;i<loc;i++) if (s.at (i) > ' 9 ' | | s.at (i) < ' 0 ') return FAL        Se        for (int j=loc+1;j<s.size (); j + +) if (s.at (j) > ' 9 ' | | s.at (j) < ' 0 ') return false;    return true;        } bool Check2 (string s, int loc)//Scientific count {bool flag1=true, flag2=true;        int count=0;        int index=0;            for (int i=0;i<loc;i++) {if (s.at (i) = = '. ')                {count++;                Index=i;            Continue        } if (s.at (i) < ' 0 ' | | s.at (i) > ' 9 ') return false;        } if (count>1) return false;            if (count==1) {string tmp (S,0,LOC);        if (!check1 (Tmp,index)) return false; }/* The first half of the check is finished */for (int j=loc+1;j<s.size (); j + +) {if (s.at (loc+1) = = ' + ' | | s.at (loc+1) = = '-            ') continue; if (s.at (j) < ' 0' ||        S.at (j) > ' 9 ') return false;    } return true; }};



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.