成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 65. Valid Number

SoapEye / 2822人閱讀

Problem

Validate if a given string can be interpreted as a decimal number.

Some examples:

"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3   " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

Numbers 0-9
Exponent - "e"
Positive/negative sign - "+"/"-"
Decimal point - "."

Of course, the context of these characters also matters in the input.

Solution
class Solution {
    public boolean isNumber(String s) {
        if (s == null || s.length() == 0) return false;
        
        s = s.trim();
        int len = s.length();
        if (len == 0) return false;
        
        int signCount = 0;
        boolean hasE = false;
        boolean hasNum = false;
        boolean hasPoint = false;
        
        for (int i = 0; i < len; i++) {
            char ch = s.charAt(i);
            
            if (ch >= "0" && ch <= "9") {
                hasNum = true;
            } else if (ch == "e" || ch == "E") {
                if (hasE || !hasNum || i == len-1) return false;
                hasE = true;
            } else if (ch == ".") {
                if (hasPoint || hasE) return false;
                //0. is true
                if (i == len-1 && !hasNum) return false;
                hasPoint = true;
            } else if (ch == "+" || ch == "-") {
                //two signs at most; should not appear in the end
                if (signCount == 2 || i == len-1) return false;
                //sign appears in the middle but no E/e
                if (i > 0 && !hasE) return false;
                signCount++;
            } else return false;
        }
        
        return true;
    }
}

文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://systransis.cn/yun/72364.html

相關文章

  • leetcode65 valid number 正則表達式的運用

    摘要:完整的正則表達式為。代碼如下翻譯如果我們看到數(shù)字,就將設為如果看到小數(shù)點,則判斷是否已有小數(shù)點或是,因為后只能有整數(shù)只能遇到一次,如果第一次遇到但是沒有遇到數(shù)字,則返回錯誤。 題目要求 Validate if a given string is numeric. Some examples: 0 => true 0.1 => true abc => false 1 a => fa...

    DobbyKim 評論0 收藏0
  • LeetCode65. Valid Number -- 判斷合法數(shù)字

    摘要:描述分析該題的說明比較模糊,所以需要慢慢進行嘗試來弄清楚哪些是合法的數(shù)字。代碼去除前后的空格小數(shù)點前面不能出現(xiàn)和小數(shù)點前面不能出現(xiàn),并且需要有數(shù)字保證后面也有數(shù)字符號只能再位和后面一位 描述 Validate if a given string is numeric. Some examples:0 => true 0.1 => trueabc => false1 a => fals...

    jindong 評論0 收藏0
  • leetcode部分題目答案之JavaScript版

    摘要:自己沒事刷的一些的題目,若有更好的解法,希望能夠一起探討項目地址 自己沒事刷的一些LeetCode的題目,若有更好的解法,希望能夠一起探討 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...

    alphahans 評論0 收藏0
  • [LeetCode] 611. Valid Triangle Number

    Problem Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.Examp...

    anonymoussf 評論0 收藏0
  • LeetCode 306. Additive Number

    摘要:描述累加數(shù)是一個字符串,組成它的數(shù)字可以形成累加序列。一個有效的累加序列必須至少包含個數(shù)。說明累加序列里的數(shù)不會以開頭,所以不會出現(xiàn)或者的情況。示例輸入輸出解釋累加序列為。 LeetCode 306. Additive Number Description Additive number is a string whose digits can form additive sequen...

    GeekQiaQia 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<