zoukankan      html  css  js  c++  java
  • 【leetcode】Valid Number

    最近使用开发的过程中出现了一个小问题,顺便记录一下原因和方法--

        Question : 

        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.

        Anwser 1 :     

        每日一道理
    试试看——不是像企鹅那样静静的站在海边,翘首企盼机会的来临,而是如苍鹰一般不停的翻飞盘旋,执著的寻求。 试试看——不是面对峰回路转、杂草丛生的前途枉自嗟叹,而是披荆斩棘,举步探索。 试试看——不是拘泥于命运的禁锢,听凭命运的摆布,而是奋力敲击其神秘的门扉,使之洞开一个新的天地。微笑着,去唱生活的歌谣。
    class Solution {
    public:
        bool isNumber(const char *s) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            
            if (s == NULL)
                 return false;
                 
             while(isspace(*s))
                 s++;
                 
             if (*s == '+' || *s == '-')
                 s++;
                 
             bool eAppear = false;
             bool dotAppear = false;
             bool firstPart = false;
             bool secondPart = false;
             bool spaceAppear = false;
             while(*s != '\0')
             {
                 if (*s == '.')
                 {
                     if (dotAppear || eAppear || spaceAppear)
                         return false;
                     else
                         dotAppear = true;
                 }
                 else if (*s == 'e' || *s == 'E')
                 {
                     if (eAppear || !firstPart || spaceAppear)
                         return false;
                     else
                         eAppear = true;
                 }
                 else if (isdigit(*s))
                 {
                     if (spaceAppear)
                         return false;
                         
                     if (!eAppear)
                         firstPart = true;
                     else
                         secondPart = true;
                 }
                 else if (*s == '+' || *s == '-')   // behind of e/E
                 {
                     if (spaceAppear)
                         return false;
                         
                     if (!eAppear || !(*(s-1) == 'e' || *(s-1) == 'E'))
                         return false;
                 }
                 else if (isspace(*s))
                     spaceAppear = true;
                 else
                     return false;
                     
                 s++;            
             }
             
             if (!firstPart) {
                 return false;
             }  else if (eAppear && !secondPart) {
                 return false;
             } 
             return true;  
        }
    };

        Anwser 2 :     

    class Solution {
    public:
        bool isNumber(const char *s) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            
            int mat[11][7] = {
                          0 ,0 ,0 ,0 ,0 ,0 ,0,      // false
                          0 ,2 ,3 ,0 ,1 ,4 ,0,      // 1
                          0 ,2 ,5 ,6 ,9 ,0 ,10,     // 2
                          0 ,5 ,0 ,0 ,0 ,0 ,0,      // 3
                          0 ,2 ,3 ,0 ,0 ,0 ,0,      // 4
                          0 ,5 ,0 ,6 ,9 ,0 ,10,     // 5
                          0 ,7 ,0 ,0 ,0 ,8 ,0,      // 6
                          0 ,7 ,0 ,0 ,9 ,0 ,10,     // 7
                          0 ,7 ,0 ,0 ,0 ,0 ,0,      // 8
                          0 ,0 ,0 ,0 ,9 ,0 ,10,     // 9
                          10,10,10,10,10,10,10      // 10
                        };
                        
            int i = 0;
            int stat = 1;
            while(s[i] != '\0') 
            {
                int type = 0;
                if(s[i] >= '0' && s[i] <= '9')
                    type = 1;
                else if(s[i] == '.')
                    type = 2;
                else if(s[i] == 'e')
                    type = 3;
                else if(s[i] == ' ')
                    type = 4;
                else if(s[i] == '+' || s[i] == '-')
                    type = 5;
                if(stat == 0)
                    return false;
                    
                stat = mat[stat][type];
                i++;
            }
            stat = mat[stat][6];
            if(stat == 10) {
                return true;
            }
            
            return false;
        }
    };

        

        

        参考推荐:

        Valid Number

        

    文章结束给大家分享下程序员的一些笑话语录: 刹车失灵
    有一个物理学家,工程师和一个程序员驾驶着一辆汽车行驶在阿尔卑斯山脉 上,在下山的时候,忽然,汽车的刹车失灵了,汽车无法控制地向下冲去, 眼看前面就是一个悬崖峭壁,但是很幸运的是在这个悬崖的前面有一些小树 让他们的汽车停了下来, 而没有掉下山去。 三个惊魂未定地从车里爬了出来。
    物理学家说, “我觉得我们应该建立一个模型来模拟在下山过程中刹车片在高 温情况下失灵的情形”。
    工程师说, “我在车的后备厢来有个扳手, 要不我们把车拆开看看到底是什么 原因”。
    程序员说,“为什么我们不找个相同的车再来一次以重现这个问题呢?”

  • 相关阅读:
    查看版本号以及如何升级
    http协商缓存VS强缓存
    「JOISC 2012」星座(凸包)
    「科技」求欧拉数单项
    「科技」在线 O(1) 逆元
    「JOISC 2017 Day 3」自然公园(交互)
    「IOI 2021」分糖果(线段树)
    「EOJ 317A」击鼓传花(类欧)
    「CF 1483E」Vabank(交互,构造)
    「NOIP 2020」微信步数(计数)
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3036036.html
Copyright © 2011-2022 走看看