-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringManipulation_twoSum.java
More file actions
47 lines (32 loc) · 1.15 KB
/
Copy pathStringManipulation_twoSum.java
File metadata and controls
47 lines (32 loc) · 1.15 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
/*
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. */
public int reverse(int x) {
StringBuilder builder = new StringBuilder();
String numstr = String.valueOf(x);
builder.append(numstr);
String reversedStr = builder.reverse().toString();
if('-' == reversedStr.charAt(reversedStr.length() - 1)) {
builder.deleteCharAt(reversedStr.length() - 1);
reversedStr = '-' + builder.toString();
}
try {
if(Integer.parseInt(reversedStr) > Integer.MAX_VALUE || Integer.parseInt(reversedStr) < Integer.MIN_VALUE) {
return 0;
}
}
catch (NumberFormatException e) {
return 0;
}
return (int) Integer.parseInt(reversedStr);
}