Skip to content

Commit b1ffd60

Browse files
Add Reverse Integer
1 parent 77d007c commit b1ffd60

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This repository contains my solutions to various LeetCode problems. Each solutio
66

77
## LeetCode Stats
88
- **Profile**: [Vinnn_96](https://leetcode.com/u/Vinnn_96/)
9-
- **Problems Solved**: 8
9+
- **Problems Solved**: 9
1010
- **Languages**: Java
1111

1212
## Repository Structure
@@ -25,14 +25,15 @@ Each problem has its own directory containing:
2525
| 4 | [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays) | Hard | [Java](Median%20of%20Two%20Sorted%20Arrays/Solution.java) | Array, Binary Search, Divide and Conquer |
2626
| 5 | [Longest Palindromic Substring](Longest%20Palindromic%20Substring) | Medium | [Java](Longest%20Palindromic%20Substring/Solution.java) | Two Pointers, String, Dynamic Programming |
2727
| 6 | [Zigzag Conversion](Zigzag%20Conversion) | Medium | [Java](Zigzag%20Conversion/Solution.java) | String |
28+
| 7 | [Reverse Integer](Reverse%20Integer) | Medium | [Java](Reverse%20Integer/Solution.java) | Math |
2829
| 214 | [Shortest Palindrome](Shortest%20Palindrome) | Hard | [Java](Shortest%20Palindrome/Solution.java) | String, Rolling Hash, String Matching, Hash Function |
2930
| 2924 | [Find Champion II](Find%20Champion%20II) | Medium | [Java](Find%20Champion%20II/Solution.java) | Graph |
3031

3132
## Categories
3233

3334
### By Difficulty
3435
- Easy: 1
35-
- Medium: 5
36+
- Medium: 6
3637
- Hard: 2
3738

3839
### By Topics
@@ -59,6 +60,8 @@ Each problem has its own directory containing:
5960
- [Find Champion II](Find%20Champion%20II)
6061
- String Matching Problems
6162
- [Shortest Palindrome](Shortest%20Palindrome)
63+
- Math Problems
64+
- [Reverse Integer](Reverse%20Integer)
6265

6366
## About LeetCode
6467

Reverse Integer/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Reverse Integer
2+
3+
## LeetCode Problem
4+
- Problem Number: 7
5+
- Problem Link: https://leetcode.com/problems/reverse-integer/
6+
- Difficulty: Medium
7+
- Topics: Math
8+
9+
## Problem Description
10+
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2³¹, 2³¹ - 1], then return 0.
11+
12+
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
13+
14+
## Examples
15+
16+
### Example 1:
17+
```
18+
Input: x = 123
19+
Output: 321
20+
```
21+
22+
### Example 2:
23+
```
24+
Input: x = -123
25+
Output: -321
26+
```
27+
28+
### Example 3:
29+
```
30+
Input: x = 120
31+
Output: 21
32+
```
33+
34+
## Constraints
35+
- -2³¹ <= x <= 2³¹ - 1

Reverse Integer/Solution.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int reverse(int x) {
3+
int INT_MIN = -2147483648;
4+
int INT_MAX = 2147483647;
5+
6+
int result = 0;
7+
8+
while (x != 0) {
9+
int digit = x % 10;
10+
x /= 10;
11+
12+
if (result > (INT_MAX / 10) || (result == INT_MAX / 10 && digit > 7)) {
13+
return 0;
14+
}
15+
if (result < (INT_MIN / 10) || (result == INT_MIN / 10 && digit < -8)) {
16+
return 0;
17+
}
18+
19+
result = result * 10 + digit;
20+
}
21+
return result;
22+
}
23+
}

0 commit comments

Comments
 (0)