-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy path7-reverse-integer.cpp
More file actions
91 lines (70 loc) · 2.24 KB
/
7-reverse-integer.cpp
File metadata and controls
91 lines (70 loc) · 2.24 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
/*
Problem 7: Reverse Integer
https://leetcode.com/problems/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
*/
#include <iostream>
#include <climits>
using namespace std;
class Solution {
public:
// Approach 1: Mathematical approach with overflow check
int reverse(int x) {
int result = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
// Check for overflow before multiplying by 10
if (result > INT_MAX / 10 || (result == INT_MAX / 10 && pop > 7)) {
return 0;
}
if (result < INT_MIN / 10 || (result == INT_MIN / 10 && pop < -8)) {
return 0;
}
result = result * 10 + pop;
}
return result;
}
// Approach 2: Using long long for intermediate calculation
int reverseLongLong(int x) {
long long result = 0;
while (x != 0) {
result = result * 10 + x % 10;
x /= 10;
}
// Check if result is within 32-bit integer range
if (result > INT_MAX || result < INT_MIN) {
return 0;
}
return (int)result;
}
};
// Test function
int main() {
Solution solution;
// Test cases
vector<int> testCases = {123, -123, 120, 0, 1534236469, -2147483648, 2147483647, 1463847412};
cout << "Testing Reverse Integer:\n";
cout << "INT_MAX: " << INT_MAX << ", INT_MIN: " << INT_MIN << "\n\n";
for (int test : testCases) {
int result1 = solution.reverse(test);
int result2 = solution.reverseLongLong(test);
cout << "Input: " << test << " -> Output: " << result1;
cout << " (Long approach: " << result2 << ")";
cout << " Match: " << (result1 == result2 ? "Yes" : "No") << "\n";
}
return 0;
}