Thursday, 30 April 2020

palindrome Number (leet code) implementation in java

Hi  in this blog post we are going to implement the leet code problem palindrome Number 

Problem Statement :

       given a number we have to determine weather it is palindrome or not  , (i.e) it reads the same if reads backwards as well as forwards 

for eg 121 is palindrome 


how do we solve the problem , there are lot of ways to solve the problem the approach i used was Two pointer approach , using two pointer one at the beginning and another at the end if they didn't match then it is not a palindrome if it matches then we increment the starting pointer and  decrement the endpointer until both pointers meet 

Lets visually see it 





this is how the number looks like  now we add two pointers one at the beginning of the array and another  at the end of the array 



so we are comparing both their values  if they are same then  we increment the start pointer and decrement the endpointer   since both pointers meet we exit the loop and returns true that the given number is a palindrome


Time Complexity:

             the time complexity is o(n)  since we are comparing it in one go 

             space complexity is o(n) since we use a character array to store number 



Code





TestCases:


in the above  we can see the test cases with coverage , if you have any doubts or opinions please comment 

No comments:

Post a Comment