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

資訊專欄INFORMATION COLUMN

[LintCode] Valid Number

since1986 / 2583人閱讀

摘要:兩種情況有無(wú),如上所示。如何判斷是否只有一個(gè)之后的長(zhǎng)度小于等于。注意,為空只有在字符串最末位或者只有都是錯(cuò)誤的。規(guī)則若字符串首位為,取其,然后將應(yīng)用轉(zhuǎn)換為數(shù)組,逐位判斷字符,出現(xiàn)將置如已經(jīng)置,再次出現(xiàn)則直接返回。

Problem

Validate if a given string is numeric.

Example
"0" => true

" 0.1 " => true

"abc" => false

"1 a" => false

"2e10" => true
Note
if !e
A can be "+/-" + int + "." + int

if e: AeB
A can be "+/-" + int + "." + int
B can be "+/-" + int

兩種情況:有無(wú)e,如上所示。
然后按有無(wú)e來(lái)拆分字符串,若拆分之后只有一部分,即用helper()函數(shù)判斷是否滿足構(gòu)成數(shù)的規(guī)則;若有兩部分,則對(duì)前一部分按數(shù)A(可以有小數(shù)部分)分析,后一部分按構(gòu)成數(shù)B分析(只能為整數(shù))。
如何判斷是否只有一個(gè)e?split()之后的String[]長(zhǎng)度小于等于2。
由于數(shù)A和數(shù)B的區(qū)別只存在于有沒(méi)有小數(shù)點(diǎn)".",建立一個(gè)flag hasDot(),其他應(yīng)用一樣的規(guī)則即可。注意,s為空、只有"e"、"e"在字符串最末位、或者只有"."都是錯(cuò)誤的。

規(guī)則:若字符串首位為+/-,取其.substring(1),然后將string應(yīng)用toCharArray()轉(zhuǎn)換為char數(shù)組,逐位判斷字符,出現(xiàn)"."將hasDot()true;如hasDot()已經(jīng)置true,再次出現(xiàn)"."則直接返回false。然后判斷其余位是否為數(shù)字即可。

Solution

public class Solution {

public boolean isNumber(String s) {
    s = s.trim();
    if (s == null || (s.length() > 0 && s.charAt(s.length()-1) == "e") || s.equals(".")) return false;
    String[] strs = s.split("e");
    if (strs.length > 2) return false;
    boolean res = helper(strs[0], false);
    if (strs.length == 2) {
        res = res && helper(strs[1], true);
    }
    return res;
}
public boolean helper(String s, boolean hasDot) {
    if (s.length() > 0 && (s.charAt(0) == "+" || s.charAt(0) == "-"))
        s = s.substring(1);
        char[] ch = s.toCharArray();
        if (ch.length == 0) return false;
        for (int i =  0; i < ch.length; i++) {
            if (ch[i] == ".") {
                if (hasDot) return false;
                hasDot = true;
            }
            else if (!(ch[i] >= "0" && ch[i] <= "9")) return false;
        }
        return true;
}

}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65520.html

相關(guān)文章

  • [LintCode/LeetCode] Count Binary Substrings

    Problem Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0s and 1s, and all the 0s and all the 1s in these substrings are grouped consecutively. Subs...

    BaronZhang 評(píng)論0 收藏0
  • [LintCode] Baseball Game

    Problem Youre now a baseball game point recorder. Given a list of strings, each string can be one of the 4 following types: Integer (one rounds score): Directly represents the number of points you get...

    newtrek 評(píng)論0 收藏0
  • [LintCode] UTF-8 Validation

    Problem A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n...

    tolerious 評(píng)論0 收藏0
  • [LeetCode/LintCode] Valid Palindrome

    Valid Palindrome Problem Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Example A man, a plan, a canal: Panama is a palindrome. race a ca...

    Ververica 評(píng)論0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<