We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f938001 commit 2eac7d5Copy full SHA for 2eac7d5
Java/reverse-integer.java
@@ -0,0 +1,27 @@
1
+/**
2
+* Not a very good approach.
3
+* StringBuilder reverse function used.
4
+*
5
+* Time Complexity: O(n)
6
+* Space Complexity: O(n)
7
+*/
8
+class Solution {
9
+ public int reverse(int x) {
10
+ StringBuilder sb = new StringBuilder(""+x);
11
+ sb.reverse();
12
+ String str = sb.toString();
13
+
14
+ if(str.charAt(str.length()-1) == '-') {
15
+ str = "-" + str.substring(0, str.length()-1);
16
+ }
17
+ int ans = 0;
18
19
+ try{
20
+ ans = Integer.parseInt(str);
21
+ } catch(Exception ex){
22
+ return 0;
23
24
25
+ return ans;
26
27
+}
0 commit comments