[LeetCode]65.Valid Number

160 篇文章 28 订阅

题目

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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

思路

代码

/*---------------------------------------
*   日期:2015-06-16
*   作者:SJF0115
*   题目: 65.Valid Number
*   网址:https://leetcode.com/problems/valid-number/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <cstdio>
using namespace std;

class Solution {
public:
    bool isNumber(string s) {
        int size = s.size();
        if(size == 0){
            return false;
        }//if
        // 前导0
        int start = 0;
        while(s[start] == ' '){
            ++start;
        }//while
        // 后导0
        int end = size - 1;
        while(s[end] == ' '){
            --end;
        }//while
        bool hasNum = false,hasPoint = false,hasE = false;
        for(int i = start;i <= end;++i){
            if(s[i] == '.'){
                // 如果前面已经有了'.' 或者 'e'
                if(hasPoint || hasE){
                    return false;
                }//if
                hasPoint = true;
            }//if
            else if(s[i] == 'e'){
                // 如果前面已经有了'e' 或者 没数字
                if(hasE || !hasNum){
                    return false;
                }//if
                hasE = true;
            }//else
            else if(s[i] < '0' || s[i] > '9'){
                // +2
                if(i == start && (s[i] == '+' || s[i] == '-')){
                    continue;
                }//if
                // 1e-2
                else if((i != 0 && s[i-1] == 'e') && (s[i] == '+' || s[i] == '-')){
                    continue;
                }
                else{
                    return false;
                }//else
            }//else
            else{
                hasNum = true;
            }//else
        }//for
        // 最后有效位不能是'e+-'
        if(s[end] == 'e' || s[end] == '+' || s[end] == '-'){
            return false;
        }//if
        // '.'
        if(!hasNum && hasPoint){
            return false;
        }//if
        // 全是空格
        if(end == -1){
            return false;
        }//if
        return true;
    }
};

int main(){
    Solution s;
    string str("    4.4e3   ");
    //char* str = ".3e4";
    cout<<s.isNumber(str)<<endl;
    return 0;
}

运行时间

这里写图片描述

测试用例

正确:
.3
3.
3.3
4e4
46.e3
4.3e3
.4e4

错误:
e
.
4e
e4
.e4
45e.2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值