https://github.com/lepharamramchiary/palindrome-number-cpp-opps-leetcode
https://github.com/lepharamramchiary/palindrome-number-cpp-opps-leetcode
cpp
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lepharamramchiary/palindrome-number-cpp-opps-leetcode
- Owner: LepharamRamchiary
- Created: 2023-02-17T08:23:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-17T08:27:50.000Z (over 2 years ago)
- Last Synced: 2024-12-29T20:33:28.998Z (5 months ago)
- Topics: cpp
- Homepage:
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
**# Palindrome Number in C++(oops)**
**Question**
Given an integer x, return true if x is a palindrome, and false otherwise.
Example 1:
```
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
```
Example 2:
```
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
```
Example 3:
```
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
```**Solution**:-
```c++
#includeusing namespace std;
class Solution {
public:
bool isPalindrome(int x) {
stack st;
if (x < 0) {
return false;
}
int y = x;
while (x) {
st.push(x % 10);
x /= 10;
}
while (y) {
if (st.top() != (y % 10)) {
return false;
}
st.pop();
y /= 10;
}
return true;
}
};int main() {
int x;
cout << "Enter a number: ";
cin >> x;Solution sol;
if (sol.isPalindrome(x)) {
cout << x << " is a palindrome." << endl;
} else {
cout << x << " is not a palindrome." << endl;
}return 0;
}