LeetCode has an Easy coding Problem in Its’ Algorithm Section in Python “Palindrome Number Leetcode Python”. Today We are going to solve this problem. LeetCode Link of the Problem is HERE.
Given an integer
x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
- For example,
121is a palindrome while
Input: x = 121
Explanation: 121 reads as 121 from left to right and from right to left.
Input: x = -121
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: x = 10
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
-231 <= x <= 231 - 1
Solution to Palindrome Number Leetcode Python
Firstly, Convert Given Input integer to String.
Secondly, Loop through the String to the middle of the string. Create a loop which will Start from 0 to len(given str) divided by 2.
Thirdly, Compare the Characters one by one from the 0th element to the middle and from the last to the middle.
Finally, Check for both sides of the String, if both the sides are the same then the given integer is a palindrome. Else it is not a palindrome.
def isPalindrome(self, x):
:type x: int
x = str(x)
for i in range(0, len(x)/2):
if(x[i] == x[len(x)-1-i]):
Follow up: Could you solve it without converting the integer to a string? If you do have a solution Please comment below.
Python-related posts Visit HERE
C++ related posts Visit HERE
Databases related posts Visit HERE
Data Structures related posts visit HERE
Algorithms related posts visit HERE
Data Science related posts visit HERE