`
bcyy
  • 浏览: 1826139 次
文章分类
社区版块
存档分类
最新评论

Valid Number -- LeetCode

 
阅读更多
原题链接:http://oj.leetcode.com/problems/valid-number/
这是一道检查字符串输入是否为合法的题目。基本规则是按照科学计数法,所以会出现的特殊字符有以下几个:符号位‘+’,‘-’,小数点‘.’,还有‘e’和‘E’,剩下的就只有数字0-9了,其他字符如果出现就是非法字符,返回false。数字字符在哪里出现都是ok的,我们主要考虑几个特殊字符的情况。
对于小数点出现的时候,我们要满足一下这些条件:(1)前面不能有小数点或者‘e’和‘E’;(2)前一位是数字(不能是第一位);(3)后一位要是数字(不能是最后一位)。
对于正负号出现的情况,要满足条件:(1)必须是第一位或者在‘e’和‘E’后一位;(2)后一位要是数字。
对于‘e’和‘E’的情况,要满足:(1)前面不能有‘e’和‘E’出现过;(2)不能是第一位(前面没数字科学计数没有意义)或者最后一位(后面没数字就不用写指数了)。
根据上面列举的情况,我们用两个标签和做前后位的判断来实现,算法复杂度比较明显是O(n)的,只需要O(1)的额外空间。代码如下:
public boolean isNumber(String s) {
    if(s==null)
        return false;
    s = s.trim();
    if(s.length()==0)
        return false;
    boolean dotFlag = false;
    boolean eFlag = false;
    for(int i=0;i<s.length();i++)
    {
        switch(s.charAt(i))
        {
            case '.':
                if(dotFlag || eFlag 
                || ((i==0||!(s.charAt(i-1)>='0'&&s.charAt(i-1)<='9')) 
                    && (i==s.length()-1||!(s.charAt(i+1)>='0'&&s.charAt(i+1)<='9'))))
                    return false;
                dotFlag = true;
                break;
            case '+':
            case '-':
                if((i>0 && (s.charAt(i-1)!='e' && s.charAt(i-1)!='E'))
                  || (i==s.length()-1 || !(s.charAt(i+1)>='0'&&s.charAt(i+1)<='9'||s.charAt(i+1)=='.')))
                    return false;
                break;
            case 'e':
            case 'E':
                if(eFlag || i==s.length()-1 || i==0)
                    return false;
                eFlag = true;
                break;
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                break;
            default:
                return false;
        }
    }
    return true;
}
这种题目一般在面试tester职位中比较常见,需要考虑比较多的边界情况,更多的是在寻找bug的感觉。
分享到:
评论

相关推荐

    leetcodelintcode差异-leetcode-python:leetcode-python

    leetcode-python 九章算法基础班 二分 题目 地址 153. Find Minimum in Rotated Sorted Array 双指针 题目 Solution Tag LintCode 604. Window Sum LeetCode 283. Moves Zeroes Array、Two Pointers Array、Two ...

    颜色分类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 ...

    javalruleetcode-leetcode-java:力码笔记

    leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted Array 53....

    dna匹配leetcode-leetcode:leetcode刷题

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

    gasstationleetcode-leetcode-rust:莱特代码休息

    leetcode 力码锈 问题 # 标题 命令 1 cargo run --bin 1-two-sum 2 cargo run --bin 2-add-two-numbers 3 cargo run --bin 3-longest-substring-without-repeating-characters 7 cargo run --bin 7-reverse-integer ...

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

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    single-number 动态规划 candy 贪心 gas-station 动态规划 palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归 valid-palindrome 模拟 pascals-...

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

    :package: Leetcode-Solutions ├── Readme.md ├── First Missing Positive │ ├── Readme.md │ └── solution.js ├── Trapping Rain Water │ ├── Readme.md │ └── solution.js ├── ...

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

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

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

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

    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:leetcode刷题

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

    Leetcode扑克-leetcode:Leetcode算法实践

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

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

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

    Leetcode-Algorithm-Exercise

    1_TwoSum 9_PalindromeNumber 13_RomanToInteger 14_LongestCommonPrefix 20_ValidParentheses 21_MergeTwoSortedLists 26_RemoveDuplicatesFromSortedArray 27_RemoveElement 28_ImplementStrStr() 35_Search...

    validnumberleetcode自动机-LeetCode:LeetCode学习记录

    valid number leetcode 自动机 老衲的 LeetCode Easy(2021-01-02) 1. 题目地址: 描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 题解: 2. 题目地址: 描述: 判断一个整数是否是...

    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 赎金...

Global site tag (gtag.js) - Google Analytics