Practice/Meta/Leetcode 65. Valid Number
CodingMust
Write a function that validates whether a given string represents a properly formatted numeric value. The string should match the structure of a valid number according to these rules:
A valid number consists of an optional sign character (+ or -), followed by a numeric body, and optionally an exponent part.
The numeric body can be:
An integer (one or more digits)
A decimal number (digits with a decimal point)
digits.digits (e.g., "123.456")digits. (e.g., "123.").digits (e.g., ".456")The exponent part (if present) consists of:
e or E+ or -)Your function should return true if the string is a valid number, and false otherwise.
e or Es consists only of English letters, digits, plus +, minus -, and dot .Example 1:
Input: s = "42" Output: true Explanation: A straightforward positive integer.
Example 2:
Input: s = "-0.75" Output: true Explanation: A negative decimal number with both integer and fractional parts.
Example 3:
Input: s = "6e-1" Output: true Explanation: Integer with negative exponent (represents 0.6).
Example 4:
Input: s = "." Output: false Explanation: Just a decimal point with no digits is not a valid number.
Example 5:
Input: s = "99e2.5" Output: false Explanation: The exponent part cannot contain a decimal point.
Example 6:
Input: s = ".8" Output: true Explanation: Decimal without integer part is valid if fractional part exists.