[leetcode] Valid Number

来源:互联网 发布:中信证券mac版 编辑:程序博客网 时间:2024/06/12 19:13

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.

思路:纯暴力解法,考虑各种情况

代码:

class Solution {public:    bool isNumber(const char *s) {        int dot=0,e=0,minus=0,plus=0;        int len=strlen(s);        int flag=0;        for(int i=0;i<len;i++){            if(s[i]<='9' && s[i]>='0'){                flag=1;                break;            }        }        if(flag==0) return false;        int first=-1;        for(int i=0;i<len;i++){            if(s[i]==' ') first=i;            if(s[i]!=' ') break;        }        if(s[first+1]=='e') return false;        int flagfirst=0,flagend=0;        for(int i=0;i<len;i++){            if(s[i]==' ' && i!=0 && i!=len-1){                for(int j=0;j<i;j++){                    if(s[j]!=' '){                        flagfirst=1;                    }                }                for(int j=i;j<len;j++){                    if(s[j]!=' '){                        flagend=1;                    }                }                if(flagfirst==1 && flagend==1){                    return false;                }            }            if(s[i]=='e' && e!=0){                return false;            }            if(s[i]=='e'){                for(int j=i;j<len;j++){                    if(s[j]=='.') return false;                }            }            if(s[i]=='e' && e==0){                if(i+1>len-1) return false;                if((s[i+1]>'9' || s[i+1]<'0') && s[i+1]!='+' && s[i+1]!='-') return false;                e++;            }            if(s[i]=='-' && minus!=0){                return false;            }            if(s[i]=='-' && minus==0){                if(i-1>=0 && s[i-1]!='e'){                    while(i!=0){                        i--;                        if(s[i]!=' ') return false;                    }                }                if((i+1<=len-1) && ((s[i+1]>'9' || s[i+1]<'0') && s[i+1]!='.')) return false;                minus++;            }                        if(s[i]=='+' && plus!=0){                return false;            }            if(s[i]=='+' && i==len-1) return false;            if(s[i]=='-' && i==len-1) return false;            if(s[i]=='+' && plus==0){                if(i-1>=0 && s[i-1]!='e'){                    while(i!=0){                        i--;                        if(s[i]!=' ') return false;                    }                }                plus++;            }                        if(s[i]=='.' && dot!=0){                return false;            }            if(s[i]=='.' && dot==0){                if((i+1<=len-1) && ((s[i+1]>'9' || s[i+1]<'0') && s[i+1]!=' ' && s[i+1]!='e')) return false;                if(i==0){                    if((i+1<=len-1) && (s[i+1]>'9' || s[i+1]<'0')) return false;                }              //  if((i+1<=len-1) && s[i]!=' ')                dot++;            }            if((s[i]>'9' || s[i]<'0') && s[i]!=' ' && s[i]!='e' && s[i]!='.' && s[i]!='-' && s[i]!='+'){                return false;            }        }        return true;    }};


0 0