当前位置:网站首页>leetcode-2.回文判断
leetcode-2.回文判断
2022-07-06 01:36:00 【东方不败就是我】
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
例如,121 是回文,而 123 不是。
这个题我的本来思路是按照java的,把字符串转成数组,然后截成一半前后对比,用python写起来实在麻烦。看题解给出的思路
class Solution:
def isPalindrome(self, x: int):
return (str(x)[::-1]==str(x))
将int转为str,通过切片方法将字符串翻转,然后与原字符串对比。很简单的方法。
#方法二 reversed函数
#print("".join(reversed(a))==a)
#print(''.join(reversed(a))) reversed(a)得到的是一个列表 join列表得到字符串
#. join(): 连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串
使用reversed函数,将字符串翻转,然后通过join方法,将列表转为字符串,再对比。
边栏推荐
- Unity VR resource flash surface in scene
- MATLB | real time opportunity constrained decision making and its application in power system
- [flask] official tutorial -part2: Blueprint - view, template, static file
- About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
- Loop structure of program (for loop)
- Threedposetracker project resolution
- leetcode刷题_反转字符串中的元音字母
- Paging of a scratch (page turning processing)
- You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
- 【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
猜你喜欢
Mathematical modeling learning from scratch (2): Tools
电气数据|IEEE118(含风能太阳能)
普通人下场全球贸易,新一轮结构性机会浮出水面
Threedposetracker project resolution
C web page open WinForm exe
Basic operations of database and table ----- set the fields of the table to be automatically added
Redis-列表
现货白银的一般操作方法
MATLB | real time opportunity constrained decision making and its application in power system
How to see the K-line chart of gold price trend?
随机推荐
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
Folio.ink 免费、快速、易用的图片分享工具
Leetcode 208. 实现 Trie (前缀树)
module ‘tensorflow. contrib. data‘ has no attribute ‘dataset
How to see the K-line chart of gold price trend?
A Cooperative Approach to Particle Swarm Optimization
3D模型格式汇总
晶振是如何起振的?
VMware Tools installation error: unable to automatically install vsock driver
Folio. Ink is a free, fast and easy-to-use image sharing tool
2022年PMP项目管理考试敏捷知识点(8)
Reasonable and sensible
【网络攻防实训习题】
[flask] response, session and message flashing
Basic operations of database and table ----- set the fields of the table to be automatically added
How does the crystal oscillator vibrate?
leetcode刷题_验证回文字符串 Ⅱ
c#网页打开winform exe
National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
Yii console method call, Yii console scheduled task