LeetCode题解(python)-9. 回文数
2019-03-07 22:11
274 查看
版权声明:转载请注明出处,谢谢 https://blog.csdn.net/zysps1/article/details/88322506
LeetCode题解(python)
9. 回文数
题目描述:
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
示例 1:
输入: 121 输出: true
示例 2:
输入: -121 输出: false 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入: 10 输出: false 解释: 从右向左读, 为 01 。因此它不是一个回文数。
解题心得:
本题很简单,反序即可
解决方案很多,几种常用的如下:
- 利用
list方法:list.reverse()- 利用
str反序:str[::-1]- 利用求余和除法把每一位分离出来,然后利用乘加(x10)组合起来(不需要str)
解题代码:
class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False xstr = list(str(x)) re_xstr = xstr.reverse() xf = ''.join(xstr) if int(xf) == x: return True else: return False
相关文章推荐
- [LeetCode]题解(python):036-Valid Sudoku
- Leetcode 063 不同路径|| Python C++ 详细题解
- [LeetCode]题解(python):009-Palindrome Number
- leetcode1题 题解 翻译 C语言版 Python版
- [LeetCode]题解(python):146-LRU Cache
- LeetCode题解(python)-29. 两数相除
- [LeetCode]题解(python):023-Merge k Sorted Lists
- 【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree
- [LeetCode]题解(python):125-Valid Palindrome
- [LeetCode]题解(python):013-Roman to Integer
- [LeetCode]题解(python):053-Maximum Subarray
- [LeetCode]题解(python):055-Jump Game
- [LeetCode]题解(python):082-Remove Duplicates from Sorted List II
- [LeetCode]题解(python):091-Decode Ways
- [LeetCode]题解(python):132-Palindrome Partitioning II
- leetcode66题 题解 翻译 C语言版 Python版
- [LeetCode]题解(python):020-Valid Parentheses
- leetcode 19 题解 翻译 Python版
- LeetCode234_PalindromeLinkedList (判断是否为回文链表) Java题解
- [LeetCode]题解(python):001-Two-Sum