LeetCode Valid Number

Source: Internet
Author: User

LeetCode Valid Number
Valid Number for solving LeetCode Problems

Original question

Determines whether a string is of the numerical type. In addition to decimal and positive negative numbers, the numeric type also needs to consider scientific notation e. For example, "-3.2e-23" is of the numerical type.

Note:

No digits before and after decimal point are legal scientific notation followed by negative numbers.

Example:

Input: s = "-3.2e-23"

Output: True

Solutions

A disgusting question is not clearly defined, and some examples are provided, so you need to keep trying it on your own. First, remove the leading and trailing empty characters. Then, consider the symbols, numbers, decimal points, and numbers in sequence. If there are several consecutive values, it indicates that the current value is a common value. Continue to judge "e" (case sensitive), and then judge the symbols and numbers. If there is no number after e, this is an abnormal scientific value. Finally, based on the comprehensive judgment of the three cases, to meet the objective is a numerical type, we must first ensure that the number before e is normal, if there is e, make sure that the number after it is also normal, and finally make sure that the entire string has been traversed. If it does not indicate that there are some abnormal characters in the middle or some extra characters at the end.

AC Source Code
class Solution(object):    def isNumber(self, s):        """        :type s: str        :rtype: bool        """        s = s.strip()        length = len(s)        index = 0        # Deal with symbol        if index < length and (s[index] == '+' or s[index] == '-'):            index += 1        is_normal = False        is_exp = True        # Deal with digits in the front        while index < length and s[index].isdigit():            is_normal = True            index += 1        # Deal with dot ant digits behind it        if index < length and s[index] == '.':            index += 1            while index < length and s[index].isdigit():                is_normal = True                index += 1        # Deal with 'e' and number behind it        if is_normal and index < length and (s[index] == 'e' or s[index] == 'E'):            index += 1            is_exp = False            if index < length and (s[index] == '+' or s[index] == '-'):                index += 1            while index < length and s[index].isdigit():                index += 1                is_exp = True        # Return true only deal with all the characters and the part in front of and behind 'e' are all ok        return is_normal and is_exp and index == lengthif __name__ == "__main__":    assert Solution().isNumber("3.e-23") == True    assert Solution().isNumber(".2e81") == True    assert Solution().isNumber("2e10") == True    assert Solution().isNumber(" 0.1") == True    assert Solution().isNumber("1 b") == False    assert Solution().isNumber("3-2") == False    assert Solution().isNumber("abc") == False

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.