leetcode easy
7. Reverse Integer
msna
2019. 2. 28. 23:10
https://leetcode.com/problems/reverse-integer/
Given a 32-bit signed integer, reverse digits of an integer.
32비트의 부호있는 정수가 있다, 정수의 숫자를 반전시켜라.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1].
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
노트 :
32비트의 부호있는 정수 범위[−231, 231 − 1]의 환경이라고 가정하자.
이 문제의 목적을 위해 반전된 정수가 오버플로우 되면 0을 반환하는 것으로 가정한다.
푸는 방법은 2가지가 있는데 데이터를 문자로 취급해서 처리하는 방법과 수치로 취급해서 처리하는 방법이 있다.
문자로 처리하는 방법 :
수치를 계산하여 처리하는 방법 :
leetcode에서 위 방법들을 처리하면 문자는 28ms, 숫자는 15ms에 실행이 된다.
간단한 프로토타입이라면 문자열로 처리해도 되지만 자주 사용되는 코드라면 숫자를 계산하는 코드를 써야겠다.