【leetcode】9.回文数
source link: https://blog.feelyou.top/posts/leetcode9.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
【leetcode】9.回文数
10
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
输入: 121
输出: true
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
python
class Solution:
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0:
return False
else:
x = list(str(x))
for i in range(int(len(x)/2)):
if x[i] != x[-(i+1)]:
return False
return True
TODO
进阶:
你能不将整数转为字符串来解决这个问题吗?
PS:
python3取商为float类型
最后更新时间:2021-04-22 19:41:40
转载请注明来源:http://blog.feelyou.top/posts/leetcode9.html
Related Issues not found
Please contact @qiaohaoforever to initialize the comment
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK