Similar Problems

Similar Problems not available

Palindrome Number - Leetcode Solution

LeetCode:  Palindrome Number Leetcode Solution

Difficulty: Easy

Topics: math  

Problem Statement:

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121

Output: true

Example 2:

Input: -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: 10

Output: false

Explanation: Reads 01 from right to left. Therefore, it is not a palindrome.

Solution Approach:

The simplest approach to solve this problem is to convert the integer to a string and then check whether the string is a palindrome or not. If the string is a palindrome, then the integer is also a palindrome.


  1. Convert the integer to a string

  2. Reverse the string

  3. Compare the original string and the reversed string

  4. If they are equal, return true; otherwise, return false


class Solution { public: bool isPalindrome(int x) { if (x < 0) { // Edge case return false; } string s = to_string(x); // Convert the integer to a string int n = s.length(); for (int i = 0; i < n/2; i++) { // Check if the string is a palindrome if (s[i] != s[n-1-i]) { return false; } } return true; } };

Time Complexity: O(logn)

Space Complexity: O(logn)

The space complexity is O(logn) because we used an additional variable to store the string representation of the integer.

Palindrome Number Solution Code