`

LeetCode 65 - Valid Number

 
阅读更多

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.

Solution 1:

public boolean isNumber(String s) {
    s = s.trim();
    if(s.isEmpty()) return false;
    boolean num = false, exp = false, dot = false;
    for(int i=0; i<s.length(); i++) {
        char c = s.charAt(i);
        if(c == 'e' || c == 'E') {
            if(!num || exp) {
                return false;
            }
            num = false; // Should be: 2e2 , so there should be number follow "e"
            exp = true;
        } else if(c == '.') {
            if(dot || exp) { // can't be: e0.2 can't be: ..
                return false;
            }
            dot = true;
        } else if(c >= '0' && c <= '9') {
            num = true;
        } else if(c == '+' || c == '-') {
            if(i != 0 && s.charAt(i-1) != 'e') { // filter : " 005047e+6", this is true.
                return false;
            }
        } else { // invalid character.
            return false;
        }
    }
    return num;
}

 

Solution 2:

正则表达式!

public boolean isNumber(String s) {
    s = s.trim();
    if(s.isEmpty()) return false;
    String regex = "[+-]?(\\d+\\.?|\\.\\d+)\\d*([eE][+-]?\\d+)?";
    return s.matches(regex);
}

 

分享到:
评论

相关推荐

    js-leetcode题解之65-valid-number.js

    javascript js_leetcode题解之65-valid-number.js

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

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

    leetcode浇花-LCSolutions:我的力扣解决方案

    leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...

    LeetCode-Hot-100

    比如"Valid Palindrome"要求判断字符串是否为回文,可以使用双指针法进行检查。"Reverse String"则需要反转字符串,可以利用C++的字符串操作函数实现。 4. **数学逻辑**:一些题目涉及基础数学概念,如质因数分解、...

    java-leetcode题解之Number of Valid Words for Each Puzzle.java

    java java_leetcode题解之Number of Valid Words for Each Puzzle.java

    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,字符串...

    dna匹配leetcode-leetcode:leetcode刷题

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

    leetcode分类-leetcode:leetcode刷题(中等难度分类)

    字符串处理题目也是LeetCode的重要部分,例如"Reverse Words in a String"(翻转字符串中的单词)和"Valid Palindrome"(验证回文串)。这些题目通常涉及到字符串的遍历、分割、比较以及特殊字符的处理。 四、位...

    leetcode-cpp刷题

    - **2.1.14 Valid Sudoku** - 判断给定的数独是否有效。 - 实现思路:分别检查行、列和小九宫格是否包含重复数字。 - **2.1.15 Trapping Rain Water** - 计算雨水能够困住的水量。 - 实现思路:使用动态规划...

    leetcode2-Algorithms-Practice:创建此repo是为了跟踪我在解决问题方面的进展

    Number 运行时间:92 毫秒内存使用:14 MB 13. Roman to Integer 运行时间:52 毫秒内存使用:14.1 MB 14. Longest Common Prefix 运行时间:40 毫秒内存使用:13.9 MB 20. Valid Parentheses 运行时间:40 毫秒内存...

    Leetcode扑克-leetcode:Leetcode算法实践

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

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

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

    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

    leetcode分类-leetcode:leetcode

    比如,实现strStr()函数(Implement strStr())和有效的括号(Valid Parentheses)等,需要深入理解字符串操作和正则表达式。 6. **哈希表**:哈希表提供高效的查找和插入操作,常用于解决查找重复项、最近点对等...

    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

    leetcode530-algorithm:算法

    leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** ...Valid Parentheses 022 Generate Parentheses 028 Implement strStr() 031 Next Permutat

    javalruleetcode-leetcode-java:力码笔记

    20.Valid Parentheses 26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and ...

    算法-leetcode-剑指offer上的题很多

    - **验证回文字符串(Valid Palindrome)**: 判断一个字符串是否是回文。 - **最长回文子串(Longest Palindromic Substring)**: 找出字符串中最长的回文子串。 - **通配符匹配(Wildcard Matching)**: 实现通配符‘*’...

    颜色分类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:力扣刷题70道!

    Number of Recent Calls 力扣 225 用队列实现栈 | Implement Stack Using Queue 力扣 622 设计循环队列 | Design Circular Queue 力扣 641 设计循环双端队列 | Design Circular Deque 栈 Stack 力扣 20 有效的括号 |...

Global site tag (gtag.js) - Google Analytics