728x90
https://leetcode.com/problems/string-to-integer-atoi/description/
단순히 문제에서 제시하는 상황을 구현하는문제였습니다.
class Solution:
def myAtoi(self, s: str) -> int:
PLUS = "+"
MINUS = "-"
MAX_INT = (2 ** 31) - 1
MIN_INT = -2 ** 31
answer = ""
s = s.lstrip()
if s and s[0] == MINUS:
answer += MINUS
s = s[1:]
elif s and s[0] == PLUS:
s = s[1:]
for target in s:
if target.isdigit():
answer += target
else:
break
print(answer)
if not answer or answer == MINUS:
return 0
int_answer = int(answer)
if int_answer > MAX_INT:
int_answer = MAX_INT
elif int_answer < MIN_INT:
int_answer = MIN_INT
return int_answer
728x90
'PS > 릿코드' 카테고리의 다른 글
[릿코드 380] Insert Delete GetRandom O(1) (파이썬/python) (0) | 2023.03.24 |
---|---|
[릿코드 1390] Four Divisors (파이썬/python) (0) | 2023.03.24 |
[릿코드 1466] Reorder Routes to Make All Paths Lead to the City Zero(파이썬/python) (0) | 2023.03.24 |
[릿코드 1297] Maximum Number of Occurrences of a Substring (파이썬/python) (0) | 2023.03.24 |
[릿코드 1319] Number of Operations to Make Network Connected(파이썬/python) (0) | 2023.03.23 |