coding-interview|September 13, 2019|1 min read

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

TL;DR

Reverse only half the number — compare the first half with the reversed second half. This avoids integer overflow and doesn't need string conversion.

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

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;
}

Related Posts

Leetcode Solution - Best Time to Buy and Sell Stock

Leetcode Solution - Best Time to Buy and Sell Stock

Problem Statement You are given an array prices where prices[i] is the price of…

Binary Tree - Level Order Traversal

Binary Tree - Level Order Traversal

Problem Statement Given a Binary tree, print out nodes in level order traversal…

Four Sum - Leet Code Solution

Four Sum - Leet Code Solution

Problem Statement Given an array nums of n integers and an integer target, are…

Leetcode - Rearrange Spaces Between Words

Leetcode - Rearrange Spaces Between Words

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

Leetcode - Maximum Non Negative Product in a Matrix

Leetcode - Maximum Non Negative Product in a Matrix

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

Leetcode - Split a String Into the Max Number of Unique Substrings

Leetcode - Split a String Into the Max Number of Unique Substrings

Problem Statement Given a string s, return the maximum number of unique…

Latest Posts

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…

Deep Dive on API Gateway: A System Design Interview Perspective

Deep Dive on API Gateway: A System Design Interview Perspective

“An API Gateway is the front door to your microservices. Every request walks…

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…