1. 程式人生 > 實用技巧 >劍指 Offer 20. 表示數值的字串

劍指 Offer 20. 表示數值的字串

劍指 Offer 20. 表示數值的字串

可以用有限自動機
也可以暴力去做,就是提交的次數有點多,我提交了20次。不停的測試所有的邊界條件
大體思路是
先判斷有沒有e
1.有e則用substr()得到前後兩個數字str1,str2
2.沒有e得到s

寫兩個判斷函式judge1,judge2
judge1:判斷小數,整數
judge2:判斷整數

結果
1.judge1(str1)&&judge2(str2)
2.judge1(s)

程式碼

class Solution {
public:
    bool judge1(string s)
    {
        int count=0;
        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='+'||s[i]=='-')
                count++;
        }
        if(count>1)
            return false;
        if(count==1)
        {
            if(s[0]=='+'||s[0]=='-')
            {
                s=s.substr(1,s.length()-1);
            }else
            {
                return false;
            }
        }
        count=0;
        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='.')
                count++;
        }
        if(count>1)
            return false;
        else if(count==1)
        {
            int index;
            for(int i=0;i<s.length();i++)
            {
                if(s[i]=='.')
                    index=i;
            }

            if(index==0)
            {
                if(s.length()==1)
                    return false;
                else
                {
                    if(s[1]>'9'||s[1]<'0')
                        return false;
                }
            }else if(index==s.length()-1)
            {
                if(s.length()==1)
                    return false;
                else
                {
                    if(s[index-1]>'9'||s[index-1]<'0')
                        return false;
                }
            }
            else{
                if(s[index+1]<='9'&&s[index+1]>='0'&&s[index-1]<='9'&&s[index-1]>='0')
                {

                }else
                {
                    return false;
                }
            }
        }else
        {
            if(s.length()==0)
                return false;
        }
        return true;
    }

    bool judge2(string s)
    {
        int count=0;
        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='+'||s[i]=='-')
                count++;
        }
        if(count>1)
            return false;
        if(count==1)
        {
            if(s[0]=='+'||s[0]=='-')
            {
                if(s.length()==1)
                    return false;
            }else
            {
                return false;
            }
        }
        count=0;
        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='.')
                count++;
        }
        if(count>0)
            return false;
        return true;
    }



    bool isNumber(string s) {
        while(s.length()>0)
        {
            if(s[s.length()-1]==' ')
                s=s.substr(0,s.length()-1);
            else
                break;
        }

        while(s.length()>0)
        {
            if(s[0]==' ')
                s=s.substr(1,s.length()-1);
            else
                break;
        }

        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='+'||s[i]=='-'||s[i]=='.'||s[i]=='e'||s[i]=='E'||(s[i]>='0'&&s[i]<='9'))
            {

            }else
            {
                return false;
            }
        }

        int count=0;
        for(int i=0;i<s.length();i++)
        {
            if(s[i]>='0'&&s[i]<='9')
                count++;
        }
        if(count==0)
            return false;

        count=0;
        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='e'||s[i]=='E')
                count++;
        }
        bool flag=true;
        if(count>1)
            return false;
        else if(count==1)
        {
            if(s.length()<3)
                return false;
            else
            {
                int index=0;
                for(int i=0;i<s.length();i++)
                {
                    if(s[i]=='e'||s[i]=='E')
                        index=i;
                }
                string str1=s.substr(0,index);
                string str2=s.substr(index+1,s.length()-index-1);
                if(str1.length()==0||str2.length()==0)
                    return false;
                flag=judge1(str1)&&judge2(str2);
            }

        }else
        {
            flag=judge1(s);
        }
        return flag;
    }
};