[Leetcode] Valid number to confirm whether it is a numeric value

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.

Click to show spoilers.

Update (2014-12-06):
New test cases had been added. Thanks Unfounder ' s contribution.

Confirm that the input string is a numeric value, a series of judgments, mainly some positional judgments:
    1. Enter Front space
    2. PLUS sign
    3. Consecutive values, including '. '
    4. Symbol E
    5. PLUS sign
    6. Consecutive values, not including '. '
    7. Subsequent spaces

Follow the rules above.

#include <iostream>using namespacestd;classSolution { Public:    BOOLIsnumber (Const Char*s) {intIDX =0;  for(; s[idx]==' '; idx++); if(s[idx]=='-'|| s[idx]=='+') idx++; intPoint=0, num=0;  for(;(s[idx]>='0'&&s[idx]<='9')|| s[idx]=='.'; idx++) S[idx]=='.'? point++:num++; if(point>1|| num<1)return false; if(s[idx]=='e') {idx++; if(s[idx]=='-'|| s[idx]=='+') idx++; Num=0;  for(; s[idx]>='0'&&s[idx]<='9'; idx++) num++; if(num<1)return false; }         for(; s[idx]==' '; idx++); returns[idx]==' /'; }};intMain () {Chara[]="-e-";    Solution Sol; cout<<sol.isnumber (a) <<Endl; return 0;}

[Leetcode] Valid number to confirm whether it is a numeric value

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.