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]
'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글
LeetCode 242. Valid Anagram (0) | 2020.05.25 |
---|---|
LeetCode 387. First Unique Character in a String (0) | 2020.05.25 |
LeetCode 350. Intersection of Two Arrays II (0) | 2020.05.24 |
LeetCode 136. Single Number (0) | 2020.05.23 |
LeetCode 217. Contains Duplicate (0) | 2020.05.23 |
댓글