-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path7.reverse-integer.cpp
More file actions
70 lines (69 loc) · 1.18 KB
/
7.reverse-integer.cpp
File metadata and controls
70 lines (69 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* @lc app=leetcode id=7 lang=cpp
*
* [7] Reverse Integer
*
* https://leetcode.com/problems/reverse-integer/description/
*
* algorithms
* Medium (26.93%)
* Likes: 7801
* Dislikes: 10274
* Total Accepted: 2.2M
* Total Submissions: 8M
* Testcase Example: '123'
*
* Given a signed 32-bit integer x, return x with its digits reversed. If
* reversing x causes the value to go outside the signed 32-bit integer range
* [-2^31, 2^31 - 1], then return 0.
*
* Assume the environment does not allow you to store 64-bit integers (signed
* or unsigned).
*
*
* Example 1:
*
*
* Input: x = 123
* Output: 321
*
*
* Example 2:
*
*
* Input: x = -123
* Output: -321
*
*
* Example 3:
*
*
* Input: x = 120
* Output: 21
*
*
*
* Constraints:
*
*
* -2^31 <= x <= 2^31 - 1
*
*
*/
// @lc code=start
class Solution
{
public:
int reverse(int32_t x)
{
string strNum = to_string(abs(x));
std::reverse(strNum.begin(), strNum.end());
int64_t reverseNum = stol(strNum);
if (x < 0)
reverseNum *= -1;
if (reverseNum > INT_MAX || reverseNum < INT_MIN)
return 0;
return reverseNum;
}
};
// @lc code=end