# Check whether an integer number given is palindrome or not - Leet Code Solution

September 13, 2019

## 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
utput: true

Example 2:
Input: -121
Output: false

Example 3:
Input: 10
Output: false``````

## Algorithm-1

You can simply reverse the number, and compare with original number. If both are same, it is a palindrome.

### Code

``````private int reverseInt(int x) {
int s = 0;
while (x > 0) {
s = s*10 + x%10;
x = x/10;
}
return s;
}

public boolean isPalindrome(int x) {
if (x < 0) return false;
return x == this.reverseInt(x);
}``````

## Algorithm-2

The fastest algorithm will be one if you can compare first and last digits and move both left and right pointers inwards. So, you need a method to fetch the digit present at certain place.

Lets look at the code:

``````/**
* Example: 1234
* place=0, result=4
* place=1, result=3
*/
private int getDigit(int x, int place) {
x = x / (int)Math.pow(10, place);
return x % 10;
}

public boolean isPalindrome_2(int x) {
if (x < 0) return false;
int l = 0;
int temp = x;
while (temp > 0) {
l++;
temp /= 10;
}

for (int i=0; i<l; i++) {
if (this.getDigit(x, i) != this.getDigit(x, l-1-i)) {
return false;
}
}
return true;
}``````

## Similar Posts

### Add two numbers(reverse order) link list Problem - Leet Code Solution

Problem Statement You are given two non-empty linked lists representing two non…

### Rotate Image - Leet Code Solution

Problem Statement You are given an n x n 2D matrix representing an image, rotate…

### Graph Topological Sorting - Build System Order Example

Graph Topological Sorting This is a well known problem in graph world…

### Leetcode - Rearrange Spaces Between Words

Problem Statement You are given a string text of words that are placed among…

### List of Sorting Algorithms

This topic is one of the most common studied. When somebody started preparation…

### Leetcode - Maximum Non Negative Product in a Matrix

Problem Statement You are given a rows x cols matrix grid. Initially, you are…

## Latest Posts

### Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

### Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Introduction In a normal email sending code from python, I’m getting following…

### Nodejs with MongoDB - Number of Opened Connections Keep on Increasing with Mongoose Library

Introduction In one of my app, I was using to talk to . I have used some event…

### Django Python - How to Build Docker Image and Run Web-service on Apache with Python 3.9

Introduction So you have a Django project, and want to run it using docker image…

### Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

### Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…