[LeetCode] 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.

click to show spoilers.

Update (2014-12-06):
New test cases had been added. Thanks unfounder‘s contribution.


  确认输入的字符串是否为一个数值,一系列的判断,主要是一些位置的判断:

  1. 输入前置空格
  2. 正负号
  3. 连续的数值,包括‘.’
  4. 符号e
  5. 正负号
  6. 连续数值,不包括‘.‘
  7. 后续空格

按上面的规则便行。

#include <iostream>
using namespace std;

class Solution {
public:
    bool isNumber(const char *s)
    {
        int idx =0;
        for(;s[idx]==‘ ‘;idx++);
        if(s[idx]==‘-‘||s[idx]==‘+‘)    idx++;
        int Point=0,Num=0;
        for(;(s[idx]>=‘0‘&&s[idx]<=‘9‘)||s[idx]==‘.‘;idx++)
            s[idx]==‘.‘?Point++:Num++;
        if(Point>1||Num<1)  return false;
        if(s[idx]==‘e‘){
            idx++;
            if(s[idx]==‘-‘||s[idx]==‘+‘)    idx++;
            Num=0;
            for(;s[idx]>=‘0‘&&s[idx]<=‘9‘;idx++)    Num++;
            if(Num<1)   return false;
        }
        for(;s[idx]==‘ ‘;idx++);
        return s[idx]==‘\0‘;
    }
};

int main()
{
    char a[]="-e-";
    Solution sol;
    cout<<sol.isNumber(a)<<endl;
    return 0;
}
时间: 2024-10-05 01:08:46

[LeetCode] Valid Number 确认是否为数值的相关文章

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

LeetCode: Valid Number [066]

[题目] 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 ambig

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

LeetCode——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.

[leetcode]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 ambi

LeetCode—*Valid Number

Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true 主要就是判断一个字符串是不是一个数字,这个题目不是很难,主要是要把所有的情况理清楚 首先有一些情况是允许的 1. 前后空格 2

LeetCode --- 65. Valid Number

题目链接: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 statemen

Valid Number leetcode java

题目: 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 ambigu

【leetcode刷题笔记】Valid Number

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 题解:题目不难,就是有点麻烦,要注意的地方很多,总结一下: 前面和后面的空格要用s.trim()去掉: 前导的'+'和'-'号需要忽略: