码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]65.Valid Number

时间:2015-06-16 16:48:14      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:leetcode   经典面试题   

题目

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

[LeetCode]65.Valid Number

标签:leetcode   经典面试题   

原文地址:http://blog.csdn.net/sunnyyoona/article/details/46519889

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!