Javarevisited
Published in

Javarevisited

Palindrome Number: Leetcode Problem #9

Originally published at https://asyncq.com

Understanding Problem

As per the problem we have been given an integer and we need to check if it is palindromic or not.

input_x    = 121
reversed_x = 121
input_x and reversed_x are palindromic
input_x = -121
reversed_x = 121-
input_x and reversed_x are not palindrominc

My Thought About Solution

  • The solution looks pretty easy for me, we just have to read input integer digits backward and construct reversed integers.
  • Then we can compare the input integer and reversed integer and return the result.
  • One additional optimization we can do is that we can return negative integers without even checking if its palindromic or not since negative signs will make them not palindromic.
  • Data structure: We are not using any collection, just integer is enough to hold the data.
    Time-complexity: O(length(N)) where N is the input integer
    Space-complexity: O(1) constant space

Pseudocode

1: First check if input integer is negative , if yes the return false , since input integers cannon be palindromic due do - sign
2: Construct reversed input integer by reading from last digit and append to reversed integer
3: Now compare the input integers and return the result

Solutions

Result

Our Code Runtime beats 99.96% of the submission. Nice job!

Conclusion

This Leetcode is a good problem to understand

  • How to iterate over each digit in integer and play around with it.
  • Optimizing solution by finding out cases where we don't need to perform our core business logic.

Let me know if you have some opinion on the solution in the comment!

Happy Leetcoding!

Liked this blog ? Find more @ : https://asyncq.com/

A humble place to learn Java and Programming better.

Recommended from Medium

How did we fine-tune the Go application performance

Learn Kubernetes step by step using LKE

Deploying Rancher in a RKE cluster

How I made a Web App that converts E-book into AudioBook With Under 20 Lines of Code

Building stuff with the Kubernetes API (part 1) — Exploring API objects

Contributing to a KnowledgeGraph via Your Calendar Manager— courtesy of Virtuoso’s CalDAV support

Setup Django with Postgres.app on macOS for Django tutorials

My microservice journey — How to use 12-factor app to guide your architecture and development…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam

Sam

Google Cloud Certified Professional Data Engineer. Find more blogs: https://asyncq.com/

More from Medium

Leetcode 54: Spiral Matrix. Solution in Java

Tree data structure: Binary Search Tree

Binary Tree Paths 🦠

LeetCode Problem: Contains Duplicate