`
king_tt
  • 浏览: 2109112 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【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


分享到:
评论

相关推荐

    leetcode最难-LeetCode_ValidNumber:因为我疯了,所以我去了LeetCode,并按Hardest:LeastSolv

    leetcode最难LeetCode_ValidNumber 因为我疯了,所以我去了 LeetCode,并按 Hardest:LeastSolved 排序。 没有汗! 语言:C# 我学到的是 这个很疯狂,因为它在定义实际可以算作数字方面确实做得很差。 如果这是面对面...

    LeetCode最全代码

    260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium || 268| [Missing ...

    程序员面试宝典LeetCode刷题手册

    第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most ...

    Leetcode-Solutions:JavaScript 语言的 Leetcode 解决方案

    力码解决方案 Leetcode是一个网站,人们——主要是软件工程师——练习他们的编码技能。 有 800 多个问题(并且还在不断增加),每个问题都有多个解决方案。 问题按难度等级排列:简单、中等和...├── Valid Number │

    leetcode分类-boost-65-valid-number:65.有效号码

    leetcode 分类C++ Boost 演示 观察如何用 . 还要观察标题模板如何“爆炸”出来,即。 我包含了 2 个头文件,并且有一大堆头文件由bcp提取用于静态编译。 OSX 构建说明 通过安装 Boost 和 CMake brew install boost ...

    leetcode中国-leetcode:leetcode刷题

    number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to integer ,easy , 模拟 count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,stack Length of Last Word,字符串...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    java lru leetcode :ice_cream: LeetCode ...Valid Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73

    最大公共字符串leetcode-leetcode:坚持每周刷一道LeetCode题

    number of nodes along the shortest path from the root node down to the nearest leaf node. 解题思路: 思路一:深度优先遍历,递归遍历左右子树,遇到叶子节点时返回 1,返回过程中,比较左右子树,较小深度...

    颜色分类leetcode-leetcode-[removed]我对Leetcode问题的解决方案

    Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from Sorted ...

    leetcode:关于leetcode网站上的题目代码

    leetcode 持续刷leetcode中... 欢迎大家交流,也欢迎star. 关于leetcode网站上的题目解法,语言选择的Java。后续会进行题目翻译和解法分析。 题目分析: Easy Leetcode 760 : Find Anagram ...Leetcode 794: Valid

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    leetcode题库-LeetCode:力码

    Number.cpp 12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号生成 G

    Leetcode扑克-leetcode:Leetcode算法实践

    Number 电话号码的字母组合 回溯法,递归 / Backtrack,Recursion 回溯+递归 Daily Challenge 2020/08/26 20 Valid Parentheses 有效的括号 Stack / 栈 用栈实现配对 Daily Challenge 2020/08/14 28 Implement ...

    leetcodepython001-LeetCode:力码

    leetcode Python 001 力码 简单的 # 问题 完成日期 语 001 001_Two_Sum 2021 年 1 月 5 日 Python 002 007_Reverse_Integer 2021 年 1 月 7 日 Python 003 009_Palindrome_Number 2021 年 1 月 7 日 Python 004 013_...

    leetcode和oj-leetCode:尝试一些OJ

    Number 52.2% Easy 371 两个整数的和 51.6% Easy 104 二叉树的最大深度 50.1% Easy 325% Add the Easy 389.数字 49.9% 简单 226 反转二叉树 48.9% 简单 283 移动零点 46.9% 简单 404 左叶总和 45.5% 简单 383 赎金...

    leetcode士兵-365DaysLeetCode:每天至少在LeetCode上做1道题的个人挑战

    LeetCode 编程挑战,持续一年! 注意* 这个项目现在不会在这里更新。 可以在以下位置找到新链接 计算团队数量 There are n soldiers standing in a line. Each soldier is assigned a unique rating value. You have...

    LeetCode:LeetCode题解

    LeetCode LeetCode题解 传送门 # 标题 解决方案 困难 笔记 1个 简单的 3 ... Valid_Palindrome Java 简单的 136 单号 Java 简单的 137 Single_NumberII Java 中等的 167 Two_Sum_II_Input_

    dna匹配leetcode-leetcode:leetcode刷题

    Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 Group Anagrams 排序 unordered_map Minimum Window Substring 两个指针遍历 map Maximal Rectangle 栈 局部递增 或者 动态规划 Binary Tree ...

    lrucacheleetcode-leetcode:leetcode

    Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse ...

    leetcodelintcode差异-leetcode-python:leetcode-python

    leetcode lintcode差异 ...Valid Triangle Number LeetCode 18. 4Sum (LeetCode 86. Partition List) LintCode 373. Partition Array by Odd and Even Mock Interview 题目 Solution Tag Dynamic Programming

Global site tag (gtag.js) - Google Analytics