-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ7_ReverseInteger.java
More file actions
53 lines (41 loc) · 980 Bytes
/
Q7_ReverseInteger.java
File metadata and controls
53 lines (41 loc) · 980 Bytes
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
package com.gyanblog.leetcode;
/**
* Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
*
* For explanation: https://www.gyanblog.com/gyan/coding-interview/leetcode-reverse-integer/
*/
public class Q7_ReverseInteger {
public int reverse(int x) {
boolean neg = false;
if (x < 0) {
//negative number
neg = true;
x = -x;
}
int s = 0;
while (x > 0) {
int rem = x%10;
if (s > Integer.MAX_VALUE/10 || (s == Integer.MAX_VALUE/10 && rem > 7)) {
return 0;
}
s = s*10 + rem;
x = x/10;
}
if (neg) {
return -s;
}
return s;
}
public static void main(String[] args) {
Q7_ReverseInteger q7 = new Q7_ReverseInteger();
int x = 1234;
System.out.println("Reverse of " + x + "=" + q7.reverse(x));
x = 1534236469;
System.out.println("Reverse of " + x + "=" + q7.reverse(x));
x = -1256;
System.out.println("Reverse of " + x + "=" + q7.reverse(x));
}
}