본문 바로가기
LeetCode/Top Interview Q. - Easy

LeetCode 7. Reverse Integer

by 벤진[Benzene] 2020. 5. 25.

Problem:

Given a 32-bit signed integer, reverse digits of an integer.

 

Example 1:

Input: 123

Output: 321

 

Example 2:

Input: -123

Output: -321

 

Example 3:

Input: 120

Output: 21

 

-Summary-

1. Typecast the integer 'x' to the string 'x' to make iterable.

2. Create a 'res' variable to iterate each number in x and keep adding into it.

(Used res = res*10 + x[i])

3. if the character is '-' then we multiply -1 to make the value negative. 

4. if the reversed value is less than the system minimum value or bigger than the system maximum integer value, then we return 0.

 

Solution 을 보니, String으로 굳이 안바꾸고 숫자안에서 바로 숫자들을 뽑아내어 계산할수 있었다.

 

 

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]

https://leetcode.com/

댓글