Leetcode #65 Solution

Problem Description

  • 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.

Some Details

  • ‘numeric’ is the key of this problem, also the reason of it being the most difficult problem to solve in leetcode.
  • You need to make a list before coding, about how to define the number.

Solution

I finish this problem by making this list:

  1. Different bases. Your code need to distinguish data in binary, octal, decimal and hex formats.
  2. Scientific notation. ‘2e10’ as an example to show it. pic below.
    这里写图片描述
  3. Space. Ignore if the string has space before the number of after the number.
  4. Symbol. ‘+”-’ is only allowed once before the number(twice in scientific notation because 2 numbers in scientific notation in fact).
  5. Decimal point. ‘.’ is only allowed once in the middle of the number.
  6. At least one digit. For example, ‘+’ , ‘.’ is not a number but ‘+1’ , ‘.0’ is OK.

In fact, this list is more stringent than the requirements of the problem.

  • Bases are unnecessary to discuss in this problem. Specifically, format in binary system and octal system is similar to it is in decimal system. What’s more, ‘abc’ as an example to show hex format is not allowed in the problem.
  • The problem doesn’t follow strict rules in Scientific notation. Look at the pic below, if you judge whether A is in range of [1,10), you can’t pass this problem.
    这里写图片描述

It’s not really easy to accept this problem, so you could find more details in my code. Here are many hints in Chinese in my code.

Code

class Solution {
public:
    bool binary(string s,int l,int r)
    {
        return false;
        //和十进制有差别吗? 
    }
    bool oct(string s,int l,int r)
    {   
        return false;
        //和十进制有差别吗? 
    }
    bool dec(string s,int l,int r)
    {
        //十进制
        bool p=false; int pn=-1; //小数点有吗
        for (int i=l;i<=r;i++)
        {
            if (s[i]=='.')
            {
                if (!p)
                {
                    p=true; pn=i;
                } else return false;
            }
        }
        bool eq=false;//这一部分合法吗?
        //什么叫合法? 至少有一位数字 至多有一个符号 
        for (int i=l;i<=r;i++)
        {
            if (s[i]>='0' && s[i]<='9')
            {
                eq=true;
            } else //不是数字
            {
                if (i==l && (s[i]=='+' || s[i]=='-')); else
                if (p && i==pn && s[i]=='.'); else
                return false;
                //不是第一位出现的正负号 中间的小数点 就不合法 
            }
        }
        //允许有忽略前后置0的. 
        return eq;//可能存在只有符号 
    }
    bool hex(string s,int l,int r)
    {
        return false;
        //十六进制,前面可能有0x,数字为0-9 A-F 
        //样例 abc为false 先不考虑十六进制 
    }
    bool sci(string s,int l,int r)
    {
        //科学计数法 aeb=a*10^b  其中 1<=|a|<10 b整数
        //0e1居然是true?? leetcode有点智障 
        bool e=false; int en=-1;
        for (int i=l;i<=r;i++)
        {
            if (s[i]=='e')
            {
                if (!e)
                {
                    e=true; en=i;
                } else return false;
            }
        }
        if (!e) return false;//没e 
        if (l==en || en==r) return false;//第一位最后一位e 
        if (!dec(s,l,en-1)) return false;//前半部分不是数
        int pn=en;
        for (int i=l;i<en;i++)
            if (s[i]=='.')
            {
                pn=i; break;
            }
        int num=0,now=1; bool eq=false;
        for (int i=pn-1;i>=l;i--)
        {//e前面部分的整数部分是不是[1,10) 
            if (s[i]>='0' && s[i]<='9')
            {
                eq=true;
                num=num+now*(s[i]-'0'); now*=10;
            } else;
        }
        //暂且放过0 
        //暂且放过大于10 
        //if (num==0 || num>=10) return false;
        eq=false;
        for (int i=en+1;i<=r;i++)
        {//后半部分是不是整数 
            if (s[i]>='0' && s[i]<='9')
            {
                eq=true;
            } else
            {
                if (i==en+1 &&(s[i]=='+'||s[i]=='-')); else
                return false;
            }
        }
        return eq;
    } 
    bool isNumber(string s) 
    {
        int len=s.length(),l=0,r=len-1;
        while (s[l]==' ' && l<len) l++;
        while (s[r]==' ' && r>=0) r--;
        if (l>r) return false;
        if (binary(s,l,r)) return true; else
        if (oct(s,l,r)) return true; else
        if (dec(s,l,r)) return true; else
        if (hex(s,l,r)) return true; else
        if (sci(s,l,r)) return true; else
        return false;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值