반응형
난이도 - Hard
Intuition
규칙대로 코딩하면 된다.
1. 숫자가 나오면 had_digit을 True로 한다.
2. 부호가 나오면 맨 앞인지, 아니면 exponential의 앞인지 확인하고 아니면 False반환한다.
3. e나 E가 나오면 이전에 숫자가 나왔는지 또는 이미 exponential이 나왔는지 확인한다.
4. .이 나왔으면 이전에 .이 나왔거나 exponential이후인지 확인한다.
5. 위의 조건이 아니면 False를 반환한다.
Code
class Solution:
def isNumber(self, s: str) -> bool:
had_digit = had_exp = had_dot = False
for i, c in enumerate(s):
if c.isdigit():
had_digit = True
elif c in "+-":
if i > 0 and s[i-1] != "e" and s[i-1] != "E":
return False
elif c in "eE":
if not had_digit or had_exp:
return False
had_exp = True
had_digit = False
elif c == ".":
if had_dot or had_exp:
return False
had_dot = True
else:
return False
return had_digit
Complexity
Time Complexity: O(N)
Space Complexity: O(1)
반응형
'코딩 알고리즘 문제 > Leetcode' 카테고리의 다른 글
| 986. Interval List Intersections (Array, Two Pointers, Line Sweep) (0) | 2025.10.24 |
|---|---|
| 1868. Product of Two Run-Length Encoded Arrays (Array, Two Pointers) (0) | 2025.10.24 |
| 127. Word Ladder (Hash Table, String, Breadth-First Search) (0) | 2025.10.24 |
| 217. Contains Duplicate (Array, Hash Table, Sorting) (0) | 2025.10.24 |
| 48. Rotate Image (0) | 2025.10.24 |