leetcode ||65、 Valid Number

problem:

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.

Hide Tags

Math String

题意:判断一个字符串是否可以有效表示一个数字,数字的表示有:正负数,整数,小数,科学表示法

提交时,leetcode 判断‘.1‘和"1."也是有效数字 !!有点搞笑吧,估计是评估系统的问题,现实中没人认为这样写有效吧。这也导致我的提交无法通过

thinking:

(1)细节处理题,注意‘E’ 和 ‘e’的大小写

(2)空格判断函数 isspace(char*)可以调用,减少工作量

code:

通过版本:参考http://www.cnblogs.com/remlostime/archive/2012/11/18/2775938.html,注意参数char *s  ,而不是string,之前的版本

我自己的版本:未通过,原因(1)leetcode 判断‘.1‘和"1."也是有效数字(2)大小写‘e‘ ‘E‘没注意区分(3)...

class Solution {
public:
    bool isNumber(string s) {
        int n=s.size();
        if(n==0)
            return false;
        if(n==1)
        {
            if(s.at(0)>='0' && s.at(0)<='9')
                return true;
            else
                return false;
        }
        if(s.at(0)==' ')
        {
            string::iterator start=s.begin();
            string::iterator end=start;
            while(*end==' ')
                end++;
            s.erase(start,end);
            return isNumber(s);
        }
        if(s.at(n-1)==' ')
        {
            string::iterator end=s.end();
            string::iterator start=end-1;
            while(*start==' ')
                start--;
            s.erase(++start,end);
            return isNumber(s);
        }
        if(s.at(0)=='+' || s.at(0)=='-')
        {
            s.erase(s.begin(),s.begin()+1);
            return isNumber(s);
        }
        int count1=0;
        int index1=0;
        int count2=0;
        int index2=0;
        int count3=0;
        for(int i=0;i<n;i++)
        {
            if((s.at(i)>='0'&&s.at(i)<='9') || s.at(i)=='.' || s.at(i)=='e' )
            {
                if(s.at(i)=='.')
                {
                    count1++;
                    index1=i;
                }
                else if(s.at(i)=='e')
                {
                    count2++;
                    index2=i;
                }
                else
                    count3++;
            }
            else
                return false;
        }//for
        if(count3==n)
            return true;
        if(count1>1 || count2>1)
            return false;
        if(count2==1)
        {
            if(index2==0 || index2==n-1)
                return false;
            else
                return check2(s,index2);
        }
        if(count1==1)
        {
            if(index1==0 ||index1==n-1)
                return false;
            else
                return check1(s,index1);
        }

    }
protected:
    bool check1(string s, int loc) //小数不含e
    {
        for(int i=0;i<loc;i++)
            if(s.at(i)>'9' || s.at(i)<'0')
                return false;
        for(int j=loc+1;j<s.size();j++)
            if(s.at(j)>'9' || s.at(j)<'0')
                return false;
        return true;
    }
    bool check2(string s, int loc)//科学计数
    {
        bool flag1=true, flag2=true;
        int count=0;
        int index=0;
        for(int i=0;i<loc;i++)
        {
            if(s.at(i)=='.')
            {
                count++;
                index=i;
                continue;
            }
            if(s.at(i)<'0' || s.at(i)>'9')
                return false;
        }
        if(count>1)
            return false;
        if(count==1)
        {
            string tmp(s,0,loc);
            if(!check1(tmp,index))
               return false;
        }
        /*前半部分检查完毕*/
        for(int j=loc+1;j<s.size();j++)
        {
            if(s.at(loc+1)=='+' || s.at(loc+1)=='-')
                continue;
            if(s.at(j)<'0' || s.at(j)>'9')
                return false;
        }
        return true;
    }
};
时间: 2024-11-05 18:50:10

leetcode ||65、 Valid Number的相关文章

我也来刷LeetCode——1、Single Number

这道题目的意思大概是这样: 给我一个整型数组,里面的元素都出现两次,但是有一个元素只出现一次,你要把这个只出现一次的元素给找出来.并且要求算法的时间复杂度为线性,即O(N). 一开始我思考了很久,始终没有找到方法.若是不限定元素类型为整型,那么根本不可能在线性时间内找到这个只出现一次的元素.所以我想突破点应该在于元素类型为整型. 为什么整型就可以做到在线性时间内得出结果呢?我点开了该题目所属的标签,[Bit Manipulation]——位操作.好像突然间抓住了什么,位操作是个很神奇的东西,它的

leetcode || 137、Single Number II

problem: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Hide Tags Bit Manipulation 题

leetcode || 136、Single Number

problem: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Hide Tags Hash Table Bit Manipulat

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

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 statement to

LeetCode:Valid Number - 判断字符串中内容是否为数字

1.题目名称 Valid Number(判断字符串中内容是否为数字) 2.题目地址 https://leetcode.com/problems/valid-number/ 3.题目内容 英文:Validate if a given string is numeric. 中文:给出一个字符串,检查这个字符串中内容是否是一个数字 例如:"0"." 0.1"."2e10"是数字,"abc"."1 a"不是数字 4

No.65 Valid Number

No.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

[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