Skip to main content

DSA Problem Solution

KANISHKA GUPTA
EditReport

Leetcode: Problem-9

Description:

Given an integer x, return true if x is a palindrome, and false otherwise.

Video Explanation

  • 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.

Solutions

class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;
long long reversed = 0, original = x;
while (x != 0) {
reversed = reversed * 10 + x % 10;
x /= 10;
}
return original == reversed;
}
};
Track Your Progress

Done with this topic? Mark it as complete to track your progress.

💬 Discuss this page

Have a question or spot something confusing in "DSA Problem Solution"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.