当前位置:网站首页>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方法,将列表转为字符串,再对比。
边栏推荐
- yii中console方法调用,yii console定时任务
- Basic operations of databases and tables ----- non empty constraints
- ctf. Show PHP feature (89~110)
- 伦敦银走势中的假突破
- 3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
- dried food! Accelerating sparse neural network through hardware and software co design
- Mongodb problem set
- SPIR-V初窥
- Redis-Key的操作
- Internship: unfamiliar annotations involved in the project code and their functions
猜你喜欢
黄金价格走势k线图如何看?
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
Idea sets the default line break for global newly created files
Basic operations of databases and tables ----- non empty constraints
Leetcode skimming questions_ Sum of squares
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
ThreeDPoseTracker项目解析
普通人下场全球贸易,新一轮结构性机会浮出水面
随机推荐
Luo Gu P1170 Bugs Bunny and Hunter
dried food! Accelerating sparse neural network through hardware and software co design
【Flask】响应、session与Message Flashing
ORA-00030
[技术发展-28]:信息通信网大全、新的技术形态、信息通信行业高质量发展概览
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
XSS learning XSS lab problem solution
CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
Unity VR resource flash surface in scene
Basic process and testing idea of interface automation
ThreeDPoseTracker项目解析
干货!通过软硬件协同设计加速稀疏神经网络
正则表达式:示例(1)
【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
Flutter Doctor:Xcode 安装不完整
现货白银的一般操作方法
【详细】快速实现对象映射的几种方式
Nmap: network detection tool and security / port scanner
Basic operations of database and table ----- delete data table
Yii console method call, Yii console scheduled task