一天一道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. 设计模式之中介者模式(Mediator )

    中介者模式是关于数据交互的设计模式,该模式的核心是一个中介者对象,负责协调一系列对象之间的不同的数据请求,这一系列对象成为同事类.如房产中介(简直不想提它),买房的卖房的,租房的放租的都到房产中介那里 ...

  2. asp.net使用session完成: 从哪个页面进入登录页面,登录成功还回到那个页面

    1.在Login.aspx页面Load中加入 if (!IsPostBack && Request.UrlReferrer != null) {      Session[ " ...

  3. Spring错误之org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookService' is expected to be of type 'pw.fengya.tx.BookService' but was actually of type 'com.sun.proxy.$Proxy1

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cas ...

  4. mysql substr() 函数

    mysql substr() 函数 用法:substr(string string,num start,num length); string为字符串:start为起始位置:length为长度. 注意 ...

  5. Android超精准计步器开发-Dylan计步

    转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/52868803 本文出自[DylanAndroid的博客] Android超精准 ...

  6. Unity3D各平台Application.xxxPath的路径

    前几天我们游戏在一个同事的Android手机上启动时无法正常进入,经查发现Application.temporaryCachePath和Application.persistentDataPath返回 ...

  7. Bootstrap3 栅格系统-媒体查询

    在栅格系统中,我们在 Less 文件中使用以下媒体查询(media query)来创建关键的分界点阈值. /* 超小屏幕(手机,小于 768px) */ /* 没有任何媒体查询相关的代码,因为这在 B ...

  8. Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》

    在之前的小案例中写过一篇使用HttpUrlConnection获取网络数据的例子.在OKhttp盛行的时代,当然要学会怎么使用它,本篇就对其基本使用做一个介绍,然后再使用它的接口回调的方式获取相同的数 ...

  9. Xcode7.3.1中通过最新的CocoaPod安装pop动画引擎

    CocoaPod是一个用ruby实现,用于方便的管理Xcode中第三方插件的管理器.用它我们可以很方便的安装和升级插件而不用担心破坏原有的项目. 而pop是一个用于实现App中动画的引擎,它是由Fac ...

  10. Web服务,XFire的一个例子

    Web服务优点 互操作性:实现不同系统间的相互调用(语言无关.平台无关) Web服务是什么 Web 服务是一类应用程序,是能够用编程的方法通过Web调用来实现某个功能的应用程序 Web服务的体系结构 ...