LeetCode—*Valid Number

Validate if a given string is numeric.

Some examples:

"0" => true

" 0.1 " => true

"abc" => false

"1 a" => false

"2e10" => true

主要就是判断一个字符串是不是一个数字,这个题目不是很难,主要是要把所有的情况理清楚

首先有一些情况是允许的

1. 前后空格

2. "+","-"号

3. "1.", ".34","+.1"也被认为是正确的

然后就是常规的情况

比如出现两个 e 或者E 或者两个小数点是不对的,比如E后面没有数字是不对的

class Solution {
public:
    bool isNumber(const char *s) {
        if(s==NULL)
            return false;
        int i=0;
        int j=strlen(s)-1;
        while(s[i]==' ') i++;
        while(j>=0 && s[j]==' ') j--;
        if(i>j)
            return false;

        string str(s+i,j-i+1); //<初始化处理之后的字符串
        int e;
        bool hasE = false;
        for(int i=0;i<str.length();i++)
            if(str[i]=='e' || str[i]=='E'){
                if(hasE)
                    return false; //<出现多个e
                else{
                    hasE=true;
                    e=i; //<记录e出现的位置
                }
            }
        if(hasE){
            string str1(str.begin(),str.begin()+e);
            string str2(str.begin()+e+1,str.end());
            return isNumberWithoutE(str1) && isSignNumber(str2); //<出现e,将其前后划分为两数据
        }
        return isNumberWithoutE(str); //<直接对该字符串检查
    }

    bool isNumberWithoutE(string s){
        if(s.length()==0)
            return false;
        if(s[0]=='+' || s[0]=='-')
            s = string(s.begin()+1,s.end());
        if(s.length()==0)
            return false;
        int dot;
        bool hasDot = false;
        for(int i=0;i<s.length();i++){
            if(s[i]=='.'){
                if(hasDot)   //<出现多个小数点
                    return false;
                else{
                    hasDot=true;
                    dot=i;
                }
            }
        }

        if(hasDot){
            string str1(s.begin(),s.begin()+dot);
            string str2(s.begin()+dot+1,s.end());
            if(str1.length()==0 && str2.length()==0)
                return false;
            if(str1.length()==0)
                return isPureNumber(str2);
            if(str2.length()==0)
                return isPureNumber(str1);
            return isPureNumber(str1) && isPureNumber(str2);  //<根据小数点将数据分开,进行检查
        }

        return isPureNumber(s);
    }

    bool isSignNumber(string s){
        if(s.length()==0)
            return false;
        if(s[0]=='+' || s[0]=='-')
            s = string(s.begin()+1,s.end());
        return isPureNumber(s);
    }

    bool isPureNumber(string s){
        if(s.length()==0)
            return false;
        for(int i=0;i<s.length();i++)
            if(s[i]<'0' || s[i]>'9')
                return false;
        return true;
    }
};

时间: 2024-11-05 12:34:37

LeetCode—*Valid Number的相关文章

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

LeetCode: Valid Number [066]

[题目] 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 ambig

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

LeetCode——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 to be ambiguous.

[leetcode]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 to be ambi

[LeetCode] 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 to be ambiguous. You

LeetCode --- 65. Valid Number

题目链接: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 statemen

Valid Number leetcode java

题目: 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 ambigu

【leetcode刷题笔记】Valid Number

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 题解:题目不难,就是有点麻烦,要注意的地方很多,总结一下: 前面和后面的空格要用s.trim()去掉: 前导的'+'和'-'号需要忽略: