반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- git
- Pro-Con
- 코딩테스트
- 알고리즘
- baekjoon
- 프로그래머스
- 백트래킹
- ASF-110
- 네이버 검색 시스템
- Python
- SRE
- 프로콘
- 제노블레이드 2
- 알고리즘 종류 정리
- Algorithmus
- 백준
- 자소서
- 취준
- Ultimate Search
- 리트코드
- programmers
- DP
- LeetCode
- 프로콘 갈림현상
- 격리수준
- Github
- python3
- GitHub Desktop
- algorithm
- C++
Archives
- Today
- Total
산타는 없다
[리트코드 / LeetCode] 7. Reverse Integer 풀이 - python3 본문
반응형
문제 원문
Given a 32-bit signed integer, reverse digits of an integer.
Note:Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [-2³¹, 2³¹ − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Example 1:
Input: x = 123 Output: 321
Example 2:
Input: x = -123 Output: -321
Example 3:
Input: x = 120 Output: 21
Example 4:
Input: x = 0 Output: 0
Constraints:
-2³¹ <= x <= 2³¹ - 1
코드 원문
class Solution:
def reverse(self, x: int) -> int:
풀이
입력받은 숫자를 뒤집어서 반환하는 문제입니다. 이번엔 파이썬으로 작성해보았습니다.
맨 앞에 -가 붙었을 때만 -를 빼서 뒤집은 다음 다시 붙여줬습니다.
그리고 결과값이 -2³¹ <= x <= 2³¹ - 1 조건을 벗어날 때 0으로 반환하여 줍니다.
성공 코드
class Solution:
def reverse(self, x: int) -> int:
import math
digit = 0
Negative = False
if x < 0:
x = -x
Negative = True
num = str(x)
if Negative:
num = num[0:]
answer = ""
for i in num:
answer = i + answer
if Negative:
answer = '-' + answer
answer = int(answer)
if answer >= 2**31-1 or answer <= -2**31 : return 0
else : return answer
결과
Runtime: 32 ms, faster than 65.93% of Python3 online submissions for Reverse Integer.
Memory Usage: 14.1 MB, less than 64.13% of Python3 online submissions for Reverse Integer.
반응형
'코딩테스트 > 리트코드' 카테고리의 다른 글
[리트코드 / LeetCode] 9. Palindrome Number 풀이 - python3 (0) | 2021.01.01 |
---|---|
[리트코드 / LeetCode] 8. String to Integer (atoi) 풀이 - python3 (0) | 2021.01.01 |
[리트코드 / LeetCode] 6. ZigZag Conversion 풀이 - C++ (0) | 2021.01.01 |
[리트코드 / LeetCode] 4. Median of Two Sorted Arrays 풀이 - C++ (0) | 2020.12.30 |
[리트코드 / LeetCode] 3. Longest Substring Without Repeating Characters 풀이 - C++ (0) | 2020.12.30 |
Comments