Skip to content

Commit f2efbd8

Browse files
solves reverse integer
1 parent c5606a7 commit f2efbd8

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
| #Number | Name | Difficulty | Solution | Youtube |
1212
|:--------:|------|:----------:|:--------:|:----------------------:|
1313
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/TwoSum.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/two_sum.py) |
14-
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-isnteger/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ReverseInteger.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/reverse_integer.py) |
14+
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ReverseInteger.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/reverse_integer.py) |
1515
| 9 | [PalindromeNumber](https://leetcode.com/problems/palindrome-number/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PalindromeNumber.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/palindrome_number.py) |
1616
| 13 | [Roman To Integer](https://leetcode.com/problems/roman-to-integer/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RomanToInteger.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/roman_to_integer.py) |
1717
| 14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LongestCommonPrefix.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/longest_common_prefix.py) |

src/ReverseInteger.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
import java.util.Scanner;
44

55
public class ReverseInteger {
6-
public static void main(String[] args) {
7-
Scanner scanner = new Scanner(System.in);
8-
int number = scanner.nextInt();
9-
System.out.println(reverse(number));
10-
}
11-
12-
private static int reverse(int number) {
13-
boolean isPositive = number >= 0;
14-
StringBuilder accumulator = new StringBuilder(number + "");
6+
public int reverse(int x) {
7+
int result = 0, next;
8+
boolean isPositive = x >= 0;
159
if (!isPositive) {
16-
accumulator.replace(0, 1, "");
10+
x *= -1;
1711
}
18-
try {
19-
return Integer.parseInt(accumulator.reverse().insert(0, isPositive ? "" : "-").toString());
20-
} catch (NumberFormatException exception) {
21-
return 0;
12+
while (x > 0) {
13+
next = 10 * result + x % 10;
14+
if (((next - x % 10) / 10) != result) {
15+
return 0;
16+
}
17+
result = next;
18+
x /= 10;
2219
}
20+
return isPositive ? result : -result;
2321
}
2422
}

0 commit comments

Comments
 (0)