[LeetCode] [C++] Reverse Integer
2021. 5. 11. 17:19ㆍ알고리즘/LeetCode
1. problem
leetcode.com/problems/reverse-integer/
Given integer and return this integer with its digits reverse
(if reversing integer out of range "int", then return 0)
2. sol
if x is minus => abs(x)
if x == max, min => return 0
push => x%10
pop => x/10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class Solution {
public:
int reverse(int x) {
int ret = 0, xx =x;
if(x == INT32_MIN || x == INT32_MAX)
return 0;
if(x < 0)
xx *= -1;
while(xx != 0) {
int xxx = xx%10;
xx /= 10;
if(INT32_MAX/10 < ret)
return 0;
ret = ret*10 + xxx;
}
if(x < 0)
ret*=-1;
return ret;
}
};
|
cs |
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode] [Medium] ZigZag Conversion (0) | 2021.03.21 |
---|---|
[LeetCode] [Medium] Longest Palindromic Substring (dp) (0) | 2021.03.20 |
[LeetCode] [Medium] Longest Substring Without Repeating Characters (0) | 2021.03.18 |
[LeetCode] [Medium] Add Two Numbers (linked list) (0) | 2021.03.17 |
[LeetCode] [easy] Two Sum (brute-force, map) (0) | 2021.03.16 |