File tree Expand file tree Collapse file tree 2 files changed +13
-15
lines changed Expand file tree Collapse file tree 2 files changed +13
-15
lines changed Original file line number Diff line number Diff line change 11
11
| #Number | Name | Difficulty | Solution | Youtube |
12
12
| :--------:| ------| :----------:| :--------:| :----------------------:|
13
13
| 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 ) |
15
15
| 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 ) |
16
16
| 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 ) |
17
17
| 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 ) |
Original file line number Diff line number Diff line change 3
3
import java .util .Scanner ;
4
4
5
5
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 ;
15
9
if (!isPositive ) {
16
- accumulator . replace ( 0 , 1 , "" ) ;
10
+ x *= - 1 ;
17
11
}
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 ;
22
19
}
20
+ return isPositive ? result : -result ;
23
21
}
24
22
}
You can’t perform that action at this time.
0 commit comments