coding-interview|August 28, 2019|1 min read

Check whether number is palindrome or not - Leet Code Solution

TL;DR

Reverse the number digit by digit and compare with the original. Negative numbers are never palindromes. Avoid string conversion for the O(1) space solution.

Check whether number 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
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

Algorithms

There can be many solutions to this:

  1. Convert number to character array, and keep on matching characters.
  2. Reverse whole number and compare
  3. Get the length of number, and iterate over number digit by digit

Solution - Reverse number and compare

public class Q9_Palindrome {
	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);
    }
}

Solution - Iterate over number digit by digit

public class Q9_Palindrome {
    /**
     * 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(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

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…