[LeetCode]65.Valid Number

简介:

题目

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 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
227 1
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
191 0
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
122 0
LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid
LeetCode Contest 178-1368. 使网格图至少有一条有效路径的最小代价 Minimum Cost to Make at Least One Valid Path in a Grid
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
274 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
376 2