LeetCode 65. Valid Number

1. 题目描述

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.

2. 解题思路

这道题目其实不难, 不过需要弄清楚各种约束关系, 空格, 小数点, e, 字符等情形的处理。

3. code

class Solution {
public:
    bool isNumber(string s) {
        // handle the space
        int pos = s.find_first_not_of(' ');
        if (pos != -1)
            s = s.substr(pos);
        pos = s.find_last_not_of(' ');
        if (-1 != pos)
            s = s.substr(0, pos + 1);

        bool hasNum = false, hasOper = false, hasE = false, hasPoint = false;
        int id = 0;
        int len_s = s.size();
        while (id < len_s){
            // +, - handle
            if (id < len_s && s[id] == '+' || s[id] == '-'){
                if (hasOper)
                    return false;
                id++;
            }
            hasOper = true;

            // num
            if (id < len_s && s[id] >= '0' && s[id] <= '9'){
                hasNum = true;
                id++;
            }
            // has E, e
            else if (id < len_s && (s[id] == 'e' || s[id] == 'E')){
                if (!hasNum || hasE)
                    return false;
                hasE = true;
                hasOper = false;
                hasNum = false;
                id++;
            }
            // has point
            else if (id < len_s && s[id] == '.'){
                if (hasPoint || hasE)
                    return false;
                id++;
                hasPoint = true;
            }
            else{
                return false;
            }
        }

        return hasNum;
    }
};

4. 大神解法

/*
All we need is to have a couple of flags so we can process the string in linear time:
*/
public boolean isNumber(String s) {
    s = s.trim();

    boolean pointSeen = false;
    boolean eSeen = false;
    boolean numberSeen = false;
    boolean numberAfterE = true;
    for(int i=0; i<s.length(); i++) {
        if('0' <= s.charAt(i) && s.charAt(i) <= '9') {
            numberSeen = true;
            numberAfterE = true;
        } else if(s.charAt(i) == '.') {
            if(eSeen || pointSeen) {
                return false;
            }
            pointSeen = true;
        } else if(s.charAt(i) == 'e') {
            if(eSeen || !numberSeen) {
                return false;
            }
            numberAfterE = false;
            eSeen = true;
        } else if(s.charAt(i) == '-' || s.charAt(i) == '+') {
            if(i != 0 && s.charAt(i-1) != 'e') {
                return false;
            }
        } else {
            return false;
        }
    }

    return numberSeen && numberAfterE;
}
/*
We start with trimming.

If we see [0-9] we reset the number flags.
We can only see . if we didn't see e or ..
We can only see e if we didn't see e but we did see a number. We reset numberAfterE flag.
We can only see + and - in the beginning and after an e
any other character break the validation.
At the and it is only valid if there was at least 1 number and if we did see an e then a number after it as well.

So basically the number should match this regular expression:

[-+]?[0-9]*(.[0-9]+)?(e[-+]?[0-9]+)?
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值