-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0007_reverse_integer.cpp
More file actions
144 lines (108 loc) · 3.04 KB
/
Copy paths0007_reverse_integer.cpp
File metadata and controls
144 lines (108 loc) · 3.04 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* File : s0007_reverse_integer.cpp
* Project : leetcode-cpp
* Author : Wei Tan <[email protected]>
* Date : 2026-09-06 12:35:52
* Last Modified Date: 2026-09-06 19:14:43
* Last Modified By : Wei Tan <[email protected]>
*/
/**
* [0007] Reverse Integer
*
* 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
*
*/
// problem: https://leetcode.com/problems/reverse-integer/
// discuss: https://leetcode.com/problems/reverse-integer/discuss/
#include <limits>
#include <utility>
using namespace std;
// submission codes start here
class Solution {
public:
int reverse(int x) {
long long result = 0;
while (x) {
result = result * 10 + x % 10;
x = x / 10;
}
return std::in_range<int>(result) ? static_cast<int>(result) : 0;
}
int reverse2(int x) {
int result = 0;
while (x != 0) {
int digit = x % 10;
if ((result > std::numeric_limits<int>::max() / 10) ||
(result < std::numeric_limits<int>::min() / 10)) {
return 0;
}
result = result * 10 + digit;
x = x / 10;
}
return result;
}
};
// submission codes end
#if defined(ENABLE_GTEST)
#include <gtest/gtest.h>
TEST(Problem0007, Example1) {
Solution solution;
auto x = 123;
auto result = 321;
EXPECT_EQ(solution.reverse(x), result);
EXPECT_EQ(solution.reverse2(x), result);
}
TEST(Problem0007, Example2) {
Solution solution;
auto x = -123;
auto result = -321;
EXPECT_EQ(solution.reverse(x), result);
EXPECT_EQ(solution.reverse2(x), result);
}
TEST(Problem0007, Example3) {
Solution solution;
auto x = 120;
auto result = 21;
EXPECT_EQ(solution.reverse(x), result);
EXPECT_EQ(solution.reverse2(x), result);
}
TEST(Problem0007, EdgeCase1) {
Solution solution;
auto x = 0;
auto result = 0;
EXPECT_EQ(solution.reverse(x), result);
EXPECT_EQ(solution.reverse2(x), result);
}
TEST(Problem0007, EdgeCase2) {
Solution solution;
auto x = 1'563'847'412; // 2'147'483'647 is the max int, 2'147'483'651
// greater than this
auto result = 0;
EXPECT_EQ(solution.reverse(x), result);
EXPECT_EQ(solution.reverse2(x), result);
}
TEST(Problem0007, EdgeCase3) {
Solution solution;
auto x = -1'563'847'412; // -2'147'483'647 is the min int, -2'147'483'651
// less than this
auto result = 0;
EXPECT_EQ(solution.reverse(x), result);
EXPECT_EQ(solution.reverse2(x), result);
}
#endif