Skip to content

Commit 35168be

Browse files
Add Palindrome Number
1 parent 4eecfd5 commit 35168be

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

Palindrome Number/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Palindrome Number
2+
3+
## LeetCode Problem
4+
- Problem Number: 9
5+
- Problem Link: https://leetcode.com/problems/palindrome-number/
6+
- Difficulty: Easy
7+
- Topics: Math
8+
9+
## Problem Description
10+
Given an integer x, return true if x is a palindrome, and false otherwise.
11+
12+
A palindrome is a number that reads the same forwards and backwards.
13+
14+
## Examples
15+
16+
### Example 1:
17+
```
18+
Input: x = 121
19+
Output: true
20+
Explanation: 121 reads as 121 from left to right and from right to left.
21+
```
22+
23+
### Example 2:
24+
```
25+
Input: x = -121
26+
Output: false
27+
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
28+
```
29+
30+
### Example 3:
31+
```
32+
Input: x = 10
33+
Output: false
34+
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
35+
```
36+
37+
## Constraints
38+
- -2³¹ <= x <= 2³¹ - 1
39+
40+
## Follow up
41+
Could you solve it without converting the integer to a string?
42+
43+
## Hints
44+
Beware of overflow when you reverse the integer.

Palindrome Number/Solution.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public boolean isPalindrome(int x) {
3+
if (x < 0 || (x % 10 == 0 && x != 0)) {
4+
return false;
5+
}
6+
7+
int reversedHalf = 0;
8+
while (x > reversedHalf) {
9+
reversedHalf = reversedHalf * 10 + x % 10;
10+
x /= 10;
11+
}
12+
13+
return x == reversedHalf || x == reversedHalf / 10;
14+
}
15+
}

README.md

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

88
## LeetCode Stats
99
- **Profile**: [Vinnn_96](https://leetcode.com/u/Vinnn_96/)
10-
- **Problems Solved**: 10
10+
- **Problems Solved**: 11
1111
- **Languages**: Java
1212

1313
## Repository Structure
@@ -28,13 +28,14 @@ Each problem has its own directory containing:
2828
| 6 | [Zigzag Conversion](Zigzag%20Conversion) | Medium | [Java](Zigzag%20Conversion/Solution.java) | String |
2929
| 7 | [Reverse Integer](Reverse%20Integer) | Medium | [Java](Reverse%20Integer/Solution.java) | Math |
3030
| 8 | [String to Integer (atoi)](./String%20to%20Integer%20(atoi)) | Medium | [Java](./String%20to%20Integer%20(atoi)/Solution.java) | String |
31+
| 9 | [Palindrome Number](Palindrome%20Number) | Easy | [Java](Palindrome%20Number/Solution.java) | Math |
3132
| 214 | [Shortest Palindrome](Shortest%20Palindrome) | Hard | [Java](Shortest%20Palindrome/Solution.java) | String, Rolling Hash, String Matching, Hash Function |
3233
| 2924 | [Find Champion II](Find%20Champion%20II) | Medium | [Java](Find%20Champion%20II/Solution.java) | Graph |
3334

3435
## Categories
3536

3637
### By Difficulty
37-
- Easy: 1
38+
- Easy: 2
3839
- Medium: 7
3940
- Hard: 2
4041

@@ -57,14 +58,14 @@ Each problem has its own directory containing:
5758
- [Add Two Numbers](Add%20Two%20Numbers)
5859
- Math Problems
5960
- [Add Two Numbers](Add%20Two%20Numbers)
61+
- [Reverse Integer](Reverse%20Integer)
62+
- [Palindrome Number](Palindrome%20Number)
6063
- Binary Search Problems
6164
- [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays)
6265
- Graph Problems
6366
- [Find Champion II](Find%20Champion%20II)
6467
- String Matching Problems
6568
- [Shortest Palindrome](Shortest%20Palindrome)
66-
- Math Problems
67-
- [Reverse Integer](Reverse%20Integer)
6869

6970
## About LeetCode
7071

0 commit comments

Comments
 (0)