zoukankan      html  css  js  c++  java
  • Valid Number

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

    断断续续做了2小时,各种情况想不到,最后崩溃了……

    由于科学计数法的引入,要考虑 '.' '-' '+''e'和数字的位置、个数关系。

    如果能够了解以上的字符出现时在之前可以出现的字符都有哪些,这个问题就能够顺利解决。

    最后贴上水码,以后再重新做一遍。

    class Solution {
    public:
        bool isNumber(const char *s) {
            int i  = 0;
            int flag_num = 0;
            //int flag_numa = 0;
            int flag_dot = 0;
            int flag_dota = 0;
            int flag_minus = 0;
            int flag_plus = 0;
            int flag_e = 0;
            int flag_ea = 0;
            int flag_blank = 0;
            int flag_blanka = 0;
            while(s[i]!='')
            {
                if(s[i] == ' ')
                {
                    if(flag_num == 1|| flag_dot ==1 || flag_plus == 1)flag_blank = 1;
                    i++;
                    continue;
                }
                if(s[i] >= '0' && s[i] <= '9')
                {
                    flag_num = 1;
                    if(flag_e == 1)flag_ea = 1;
                    if(flag_dot == 1)flag_dota = 1;
                    if(flag_blank == 1)return 0;
                    i++;
                    continue;
                }
                if(s[i] == '.')
                {
                    if(flag_dot == 1 || flag_blank == 1 ||flag_e == 1 )return 0;
                    else
                    flag_dot = 1;
                    
                    //if(s[i+1] == ' ')return 0;
                    i++;
                    continue;
                }
                if(s[i] == '-')
                {
                    if(flag_minus == 1)return 0;
                    if(flag_num == 1 && flag_e == 1 );
                    else if(flag_num == 1 || flag_dot == 1 )return 0;
                    if(flag_minus == 0)flag_minus = 1;
                    else
                    {
                        if(flag_num == 1)return 1;
                        else flag_num = 1;
                    }
                    i++;
                    continue;
                }
                if(s[i] == '+')
                {
                    if(flag_ea == 1)return 0;
                    if(flag_num == 0 && flag_plus == 0|| flag_num == 1&&flag_e == 1)i++;
                    else return 0;
                    //if(flag_dot == 1 && flag_num == 1)return 0 ;
                    if( flag_dot ==1 && flag_e == 0)return 0;
                    flag_plus = 1;
                    continue;
                }
                if(s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z')
                if(s[i] == 'e' && flag_e == 0 && flag_num == 1 ) {i++;flag_e=1;}
                else return 0;
                
            }
            //if(s[i-1] == '.')return 0;
            if(flag_e == 1 && flag_ea == 1)return 1;
            if(flag_e == 1 && flag_ea == 0)return 0;
            if(flag_dot == 1 && flag_dota == 1)return 1;
            //if(flag_dot == 1 )return 0;
            
            if(flag_num == 1)return 1;
            else return 0;
            
        }
    };
    

      

  • 相关阅读:
    Kafka Shell基本命令(包括topic的增删改查)
    thefuck的安装和使用
    Linux运维利器之ClusterShell
    MySQL数据库的10大经典错误案例
    Mysql 常用操作
    Git 忽略特定文件或文件夹
    为什么需要拷贝构造函数
    C语言编译过程
    设计模式之建造者模式
    设计模式之工厂/抽象工厂模式
  • 原文地址:https://www.cnblogs.com/pengyu2003/p/3575023.html
Copyright © 2011-2022 走看看