bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 65. Valid Number

65. Valid Number

Leetcode Solutions

Approach: Follow The Rules!

  1. Initialize three boolean variables seenDigit, seenExponent, and seenDot to false.
  2. Iterate over each character in the input string.
  3. If the character is a digit, set seenDigit to true.
  4. If the character is a sign, ensure it's the first character or follows an exponent. If not, return false.
  5. If the character is an exponent, check if we've already seen an exponent or haven't seen a digit. If so, return false. Otherwise, set seenExponent to true and seenDigit to false.
  6. If the character is a dot, check if we've already seen a dot or an exponent. If so, return false. Otherwise, set seenDot to true.
  7. If the character is anything else, return false.
  8. After iterating through all characters, return the value of seenDigit to ensure we've seen at least one digit.
UML Thumbnail

Approach: Deterministic Finite Automaton (DFA)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...