一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:判断一个string数是否为有效数

大致从以下三点考虑这个问题:

1、全为数字

2、存在有且仅有一个’e‘,这个时候需要判断前后的数字有效,如4e+,.e1

3、存在有些仅有一个’.’,注意指数不能为小数

做题时没有考虑到的特殊情况:

“e”,“4e+”,“.e1”

class Solution {
public:
    bool isNumber(string s) {
        int len = s.length();
        if(len == 0) return false;
        //剔除前面的空格
        int st = 0 ;
        while(st<len&&s[st] == ' ') st++;
        int ed = len-1;
        //剔除后面的空格
        while(ed>=0&&s[ed] == ' ') ed--;
        if(st>ed) return false;//如果全是空格就返回false
        //符号
        if(s[st]=='+'||s[st]=='-') st++;
        //标志.和e的位置
        int hase = -1;
        int hasdot =-1;
        //开始循环
        for(int i =st ; i <= ed ;)
        {
            if(s[i]>='0'&&s[i]<='9') i++;
            else if(s[i]=='e')
            {
                if(hase==-1) hase = i;//只能出现一个e
                else return false;
                if(i==st||i==ed) return false;//e在最前面或最后面返回false
                i++;
                if(s[i]=='+'||s[i]=='-') i++;//考虑指数带有符号
                if(i>ed) return false;//指数非法,如10e+
            }
            else if(s[i]=='.')
            {
                if(hasdot==-1) hasdot = i;//只能有一个.
                else return false;
                i++;
                if(ed-st==0) return false;//string为“.”的特殊情况
            }
            else return false;
        }
        if(hase!=-1&&hasdot!=-1){//判断e前面的数字有效
            if(hase<hasdot) return  false;//指数不能为小数
            if(hasdot==st&&hase-hasdot==1) return false;//“.e1”的特殊情况,e前面数要有效
        }
        return true;
    }
};

【一天一道LeetCode】#65. Valid Number的更多相关文章

  1. [leetcode]65. Valid Number 有效数值

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  2. leetCode 65.Valid Number (有效数字)

    Valid Number  Validate if a given string is numeric. Some examples: "0" => true " ...

  3. [LeetCode] 65. Valid Number 验证数字

    Validate if a given string can be interpreted as a decimal number. Some examples:"0" => ...

  4. Leetcode 65 Valid Number 字符串处理

    由于老是更新简单题,我已经醉了,所以今天直接上一道通过率最低的题. 题意:判断字符串是否是一个合法的数字 定义有符号的数字是(n),无符号的数字是(un),有符号的兼容无符号的 合法的数字只有下列几种 ...

  5. LeetCode 65 Valid Number

    (在队友怂恿下写了LeetCode上的一个水题) 传送门 Validate if a given string is numeric. Some examples: "0" =&g ...

  6. [LeetCode] 65. Valid Number(多个标志位)

    [思路]该题题干不是很明确,只能根据用例来理解什么样的字符串才是符合题意的,本题关键在于几个标志位的设立,将字符串分为几个部分,代码如下: class Solution { public: strin ...

  7. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  8. 【一天一道LeetCode】#191. Number of 1 Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  9. 【leetcode】Valid Number

    Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...

随机推荐

  1. python学习之路基础篇(三)

    博客参考:http://www.cnblogs.com/wupeiqi/articles/4943406.html http://www.cnblogs.com/luotianshuai/p/4949 ...

  2. Linux下常用的配置

    本文主要给出的都是一些常用的Linux配置,系统版本是基于CentOs6.3,供自己复习和新人学习,不当之处还请指正. vmware tools安装 虚拟机--->安装vmware tools ...

  3. R语言do.call 函数用法详解

    虽然R语言有类型很丰富的数据结构,但是很多时候数据结构比较复杂,那么基本就会用到list这种结构的数据类型.但是list对象很难以文本的形式导出,因此需要一个函数能快速将复杂的list结构扁平化成da ...

  4. jQuery 遍历 – 祖先

    祖先是父.祖父或曾祖父等等. 通过 jQuery,您能够向上遍历 DOM 树,以查找元素的祖先. 向上遍历 DOM 树 这些 jQuery 方法很有用,它们用于向上遍历 DOM 树: parent() ...

  5. JMETER_从入门到放弃系列

    基础篇 Jmeter(一)_环境部署 Jmeter(二)_基础元件 Jmeter(三)_配置元件 Jmeter(四)_16个逻辑控制器 Jmeter(五)_24个函数 Jmeter(六)_前置处理器 ...

  6. 2016移动端Android新技术综合预览--好文不多,这一篇就足够

    Csdn /Tamic 原文地址: http://blog.csdn.net/sk719887916/article/details/53525067 本文章6月份已完成(http://www.jia ...

  7. 安卓高级6 玩转AppBarLayout,更酷炫的顶部栏 Toolbar

    原文大神地址:http://www.jianshu.com/p/d159f0176576 上一篇文章[<CoordinateLayout的使用如此简单 >]上一篇文章<Coordin ...

  8. Mac入门

    Mac入门 桌面 windows桌面有图标罗列 Mac桌面有Dock 菜单栏 感觉上和Windows系统的底部菜单栏有点像,但是却略有不同,Mac的菜单栏默认在顶部 左侧的一些功能是固定不变的,跟随当 ...

  9. Dynamics CRM 打开数据加密报错及修改用户邮件保存报错的解决方法

    在项目里会碰到在修改用户的电子邮件时报错的问题 然后跑到数据管理里打开数据加密又是报错 解决上述问题只需要做下数据库的更改即可,把标志位置1即可,记得要重启下IIS才能生效 SELECT [Colum ...

  10. 安卓框架——SlidingMenu使用技巧

    SlidingMenu的一些常用属性 原文转载http://blog.csdn.net/zwl5670/article/details/48274109 [java] view plain copy ...