[Leetcode] Valid number

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 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 code definition.

Problem Solving Ideas:

This problem is not difficult, mainly to understand the test instructions, and to clarify the double type of the situation. For double types with E, the left side of E must be a floating-point type, the right must be an integral type, and cannot be empty. Another case is that there may be spaces on both sides, and the problem is correct, so I added a trim function to my code. In addition, do not consider the capitalization of E, in fact, should be considered.

My train of thought is very simple, there are two kinds of discussion about E and no E. Note that the string type of C + + is find to look for, not indexof.

Here's the code:

Class Solution {Public:bool Isnumber (string s) {//s = S.tolower ();        s = Trim (s);        int eindex = S.find (' e ');        if (Eindex = = S.length ()-1) {return false;        } else if (Eindex < 0) {return isdouble (s);        }else{return isdouble (s.substr (0, Eindex)) &&isint (S.substr (Eindex + 1));        }}private:bool isdouble (string s) {int len = s.length ();        if (len = = 0) {return false;            }//Sign bit if (s[0] = = ' + ' | | s[0] = = '-') {s = s.substr (1);            len--;            if (len==0) {return false; }}//Only one decimal point if (s[0]== '. ')        &&len==1) {return false;  } int dotindex =-1; Subscript for (int i = 0; i<len; i++) {if (s[i]== ') of the decimal point.                {if (dotindex>=0) {//already has decimal points return false;  }else{Dotindex = i;              }}else if (s[i]< ' 0 ' | |            S[i]> ' 9 ') {return false;        }}} bool Isint (string s) {//sign bit int len = s.length ();        if (len = = 0) {return false;            } if (s[0] = = ' + ' | | s[0] = = '-') {s = s.substr (1);            len--;            if (len==0) {return false; }} for (int i=0; i<len; i++) {if (s[i]> ' 9 ' | |            s[i]< ' 0 ') {return false;    }} return true;        } string Trim (string s) {int len = s.length ();   int L = 0,r = len-1;                s the position of the first non-null character while (L<len) {if (s[l]== ' | | s[l] = = ' \ t ' | | s[l]== ' \ r ' | | s[l]== ' \ n ') {                l++;            Continue        } break;                } while (r>=0) {if (s[r]== ' | | | s[r] = = ' \ t ' | | s[r]== ' \ r ' | | s[r]== ' \ n ') {r--;            Continue } break;        } s = S.substr (l, r-l + 1);    return s; }};


[Leetcode] 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.