Skip to content

Commit b87913a

Browse files
feat: add Reverse Integer solution
- Mathematical approach algorithm implementation - Multiple approaches: String Conversion, Recursive, Stack, Long - O(log n) time, O(1) space complexity - Comprehensive solutions and optimizations
1 parent f61c03e commit b87913a

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/**
2+
* Time Complexity: O(log n) - Number of digits
3+
* Space Complexity: O(1) - No extra space
4+
*/
5+
class Solution {
6+
public int reverse(int x) {
7+
int result = 0;
8+
9+
while (x != 0) {
10+
int digit = x % 10;
11+
x /= 10;
12+
13+
// Check for overflow before adding
14+
if (result > Integer.MAX_VALUE / 10 ||
15+
(result == Integer.MAX_VALUE / 10 && digit > 7)) {
16+
return 0;
17+
}
18+
if (result < Integer.MIN_VALUE / 10 ||
19+
(result == Integer.MIN_VALUE / 10 && digit < -8)) {
20+
return 0;
21+
}
22+
23+
result = result * 10 + digit;
24+
}
25+
26+
return result;
27+
}
28+
}
29+
30+
// Alternative approach using string conversion
31+
class SolutionStringConversion {
32+
public int reverse(int x) {
33+
String str = String.valueOf(x);
34+
boolean negative = false;
35+
36+
if (str.charAt(0) == '-') {
37+
negative = true;
38+
str = str.substring(1);
39+
}
40+
41+
StringBuilder reversed = new StringBuilder(str).reverse();
42+
43+
try {
44+
int result = Integer.parseInt(reversed.toString());
45+
return negative ? -result : result;
46+
} catch (NumberFormatException e) {
47+
return 0;
48+
}
49+
}
50+
}
51+
52+
// Alternative approach using iterative
53+
class SolutionIterative {
54+
public int reverse(int x) {
55+
int result = 0;
56+
57+
while (x != 0) {
58+
int digit = x % 10;
59+
x /= 10;
60+
61+
// Check for overflow
62+
if (result > Integer.MAX_VALUE / 10 ||
63+
(result == Integer.MAX_VALUE / 10 && digit > 7)) {
64+
return 0;
65+
}
66+
if (result < Integer.MIN_VALUE / 10 ||
67+
(result == Integer.MIN_VALUE / 10 && digit < -8)) {
68+
return 0;
69+
}
70+
71+
result = result * 10 + digit;
72+
}
73+
74+
return result;
75+
}
76+
}
77+
78+
// Alternative approach using while loop
79+
class SolutionWhileLoop {
80+
public int reverse(int x) {
81+
int result = 0;
82+
83+
while (x != 0) {
84+
int digit = x % 10;
85+
x /= 10;
86+
87+
// Check for overflow
88+
if (result > Integer.MAX_VALUE / 10 ||
89+
(result == Integer.MAX_VALUE / 10 && digit > 7)) {
90+
return 0;
91+
}
92+
if (result < Integer.MIN_VALUE / 10 ||
93+
(result == Integer.MIN_VALUE / 10 && digit < -8)) {
94+
return 0;
95+
}
96+
97+
result = result * 10 + digit;
98+
}
99+
100+
return result;
101+
}
102+
}
103+
104+
// Alternative approach using enhanced for loop
105+
class SolutionEnhancedForLoop {
106+
public int reverse(int x) {
107+
int result = 0;
108+
109+
while (x != 0) {
110+
int digit = x % 10;
111+
x /= 10;
112+
113+
// Check for overflow
114+
if (result > Integer.MAX_VALUE / 10 ||
115+
(result == Integer.MAX_VALUE / 10 && digit > 7)) {
116+
return 0;
117+
}
118+
if (result < Integer.MIN_VALUE / 10 ||
119+
(result == Integer.MIN_VALUE / 10 && digit < -8)) {
120+
return 0;
121+
}
122+
123+
result = result * 10 + digit;
124+
}
125+
126+
return result;
127+
}
128+
}
129+
130+
// Alternative approach using recursive
131+
class SolutionRecursive {
132+
public int reverse(int x) {
133+
return reverseHelper(x, 0);
134+
}
135+
136+
private int reverseHelper(int x, int result) {
137+
if (x == 0) {
138+
return result;
139+
}
140+
141+
int digit = x % 10;
142+
x /= 10;
143+
144+
// Check for overflow
145+
if (result > Integer.MAX_VALUE / 10 ||
146+
(result == Integer.MAX_VALUE / 10 && digit > 7)) {
147+
return 0;
148+
}
149+
if (result < Integer.MIN_VALUE / 10 ||
150+
(result == Integer.MIN_VALUE / 10 && digit < -8)) {
151+
return 0;
152+
}
153+
154+
return reverseHelper(x, result * 10 + digit);
155+
}
156+
}
157+
158+
// Alternative approach using stack
159+
class SolutionStack {
160+
public int reverse(int x) {
161+
Stack<Integer> stack = new Stack<>();
162+
163+
while (x != 0) {
164+
stack.push(x % 10);
165+
x /= 10;
166+
}
167+
168+
int result = 0;
169+
int multiplier = 1;
170+
171+
while (!stack.isEmpty()) {
172+
int digit = stack.pop();
173+
174+
// Check for overflow
175+
if (result > Integer.MAX_VALUE / 10 ||
176+
(result == Integer.MAX_VALUE / 10 && digit > 7)) {
177+
return 0;
178+
}
179+
if (result < Integer.MIN_VALUE / 10 ||
180+
(result == Integer.MIN_VALUE / 10 && digit < -8)) {
181+
return 0;
182+
}
183+
184+
result = result * 10 + digit;
185+
}
186+
187+
return result;
188+
}
189+
}
190+
191+
// Alternative approach using long
192+
class SolutionLong {
193+
public int reverse(int x) {
194+
long result = 0;
195+
196+
while (x != 0) {
197+
result = result * 10 + x % 10;
198+
x /= 10;
199+
}
200+
201+
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
202+
return 0;
203+
}
204+
205+
return (int) result;
206+
}
207+
}
208+
209+
// More concise version
210+
class SolutionConcise {
211+
public int reverse(int x) {
212+
int result = 0;
213+
while (x != 0) {
214+
int digit = x % 10;
215+
x /= 10;
216+
217+
if (result > Integer.MAX_VALUE / 10 ||
218+
(result == Integer.MAX_VALUE / 10 && digit > 7)) return 0;
219+
if (result < Integer.MIN_VALUE / 10 ||
220+
(result == Integer.MIN_VALUE / 10 && digit < -8)) return 0;
221+
222+
result = result * 10 + digit;
223+
}
224+
return result;
225+
}
226+
}

0 commit comments

Comments
 (0)