leetcode-65-面试题20-表示数值的字符串

Posted oldby

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-65-面试题20-表示数值的字符串相关的知识,希望对你有一定的参考价值。

题目描述:

技术图片

 

 方法一:逻辑判断法:O(N)

技术图片
class Solution:
    def isNumber(self, s: str) -> bool:
        s = s.strip()
        met_dot = met_e = met_digit = False
        for i, char in enumerate(s):
            if char in (+, -):
                if i > 0 and s[i-1] != e and s[i-1] != E:
                    return False
            elif char == .:
                if met_dot or met_e: 
                    return False
                met_dot = True
            elif char == e or char == E:
                if met_e or not met_digit:
                    return False
                met_e, met_digit = True, False # e后必须接,所以这时重置met_digit为False,以免e为最后一个char
            elif char.isdigit():
                met_digit = True
            else:
                return False
        return met_digit
View Code

方法二;正则表达式

技术图片
class Solution:
    def isNumber(self, s: str) -> bool:
        p = re.compile(r^[+-]?(.d+|d+.?d*)([eE][+-]?d+)?$)
        return bool(p.match(s.strip()))
View Code

方法三:try/except法

技术图片
class Solution:
    def isNumber(self, s: str) -> bool:
        try:
            float(s)
            return True
        except:
            return False
View Code

方法四:确定性有限自动机

class Solution(object):
    def isNumber(self, s):
        """
        :type s: str
        :rtype: bool
        """
        #define DFA state transition tables
        states = [{},
                 # State (1) - initial state (scan ahead thru blanks)
                 {blank: 1, sign: 2, digit:3, .:4},
                 # State (2) - found sign (expect digit/dot)
                 {digit:3, .:4},
                 # State (3) - digit consumer (loop until non-digit)
                 {digit:3, .:5, e:6, blank:9},
                 # State (4) - found dot (only a digit is valid)
                 {digit:5},
                 # State (5) - after dot (expect digits, e, or end of valid input)
                 {digit:5, e:6, blank:9},
                 # State (6) - found ‘e‘ (only a sign or digit valid)
                 {sign:7, digit:8},
                 # State (7) - sign after ‘e‘ (only digit)
                 {digit:8},
                 # State (8) - digit after ‘e‘ (expect digits or end of valid input)
                 {digit:8, blank:9},
                 # State (9) - Terminal state (fail if non-blank found)
                 {blank:9}]
        currentState = 1
        for c in s:
            # If char c is of a known class set it to the class name
            if c in 0123456789:
                c = digit
            elif c in  	
:
                c = blank
            elif c in +-:
                c = sign
            # If char/class is not in our state transition table it is invalid input
            if c not in states[currentState]:
                return False
            # State transition
            currentState = states[currentState][c]
        # The only valid terminal states are end on digit, after dot, digit after e, or white space after valid input
        if currentState not in [3,5,8,9]:
            return False
        return True

 

以上是关于leetcode-65-面试题20-表示数值的字符串的主要内容,如果未能解决你的问题,请参考以下文章

面试题20. 表示数值的字符串

面试题20. 表示数值的字符串

剑指Offer-代码的完整性面试题20:表示数值的字符串

剑指OFFER----面试题20. 表示数值的字符串

剑指Offer-面试案例面试题66:把字符串转为整数

Java常见关于api面试题