Palindrome Number Leetcode Python – Python Solutions

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.

Palindrome Number Leetcode Python

Question

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward.

  • For example, 121 is a palindrome while 123 is not.

Examples

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.


Constraints:

  • -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.

class Solution(object):
def isPalindrome(self, x):
“””
:type x: int
:rtype: bool
“””
x = str(x)
for i in range(0, len(x)/2):
if(x[i] == x[len(x)-1-i]):
pass
else:
return False

return True

Follow up: Could you solve it without converting the integer to a string? If you do have a solution Please comment below.

READ MORE

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

Share the Knowledge