-
[Leetcode] 7. Reverse Integer (In Python)[Python] 파이썬/Leetcode_Easy 2021. 1. 3. 19:38
간략한 코드들은 github.com/Eastar-DS/Python 의 Leetcode 폴더에도 있습니다
Eastar-DS/Python
Contribute to Eastar-DS/Python development by creating an account on GitHub.
github.com
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^31, (2^31) − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.32비트 signed integer 이므로 1비트는 + - 를 표시하고 31비트로 수의 크기를 표시하므로, [−2^31, (2^31) − 1]의 범위를 갖는 정수 하나를 인풋으로 넣어주면 아웃풋으로 순서를 바꾼 정수를 반환해주는 함수를 만들면된다.
범위를 벗어나는 정수를 인풋으로 넣어주면 0을 반환한다.
작성한 코드(좌) / 문제의 인풋과 아웃풋예시(우) 몇년만에 다시시작해서 작성한 코드를 블로그에 올리려고 다시보니 정말 가관이다.
코드를 한줄한줄 뜯어보기보다 어떻게 풀지 생각했던 과정만 적고 넘어가겠다. 쉬운난이도의 문제니까!
1. 인풋의 범위가 벗어나면 0을 반환한다.
2. x < 0 이면 스트링앞에 -가 붙으므로 answer를 '-'로 설정하고 for문을 이용해 x의 뒤쪽부터 더해주면서 스트링을 완성시킨다.
3. answer를 integer로 변환해서 반환한다.
쉬운난이도도 못풀어서 쩔쩔매던 내가 어려운 문제도 풀어보려고 아둥바둥하고있다. 절망하지말고 끝까지 해보자!
문제의 출처는 leetcode.com/problems/reverse-integer/입니다.
모르시는게 있으시다면 언제든지 댓글로 질문을 남겨주세요. 저에게 큰 공부가 됩니다. 감사합니다.
'[Python] 파이썬 > Leetcode_Easy' 카테고리의 다른 글
[Leetcode] 231. Power of Two (In Python) (0) 2021.12.21 [Leetcode] 1200. Minimum Absolute Difference (In Python) (0) 2021.12.20 [Leetcode] 9. Palindrome Number (In Python) (0) 2021.01.04 [Leetcode] 1. Two Sum (In Python) (0) 2021.01.01