当前位置:网站首页>力扣_回文数
力扣_回文数
2022-07-04 21:39:00 【上课不要睡觉了】
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
例如,121 是回文,而 123 不是。
示例 1:
输入:x = 121
输出:true
示例 2:
输入:x = -121
输出:false
解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入:x = 10
输出:false
解释:从右向左读, 为 01 。因此它不是一个回文数。
来源:力扣(LeetCode)
Java解法
class Solution {
public boolean isPalindrome(int x) {
if(x<0||(x%10==0&&x!=0)){
//排除负数和最后一位是0的数
return false;
}
int revertedNumber=0;
while(x>revertedNumber){
revertedNumber=revertedNumber*10+x%10;
x/=10;
//将这个数后半部分逆序
}
return x==revertedNumber||x==revertedNumber/10;
}
}
Python解法
class Solution:
def isPalindrome(self, x: int) -> bool:
s=str(x)
l=len(s)
h=l//2
return s[:h]==s[-1:-h-1:-1]
#最后一个-1表示倒叙输出,前两个值表示范围
边栏推荐
- Redis has three methods for checking big keys, which are necessary for optimization
- Master the use of auto analyze in data warehouse
- Drop down selection of Ehlib database records
- gtest从一无所知到熟练运用(1)gtest安装
- Redis 排查大 key 的3种方法,优化必备
- Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
- The drawing method of side-by-side diagram, multi row and multi column
- 并发网络模块化 读书笔记转
- [leetcode] 17. Letter combination of telephone number
- From repvgg to mobileone, including mobileone code
猜你喜欢

QT—双缓冲绘图

NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置

做BI开发,为什么一定要熟悉行业和企业业务?

GTEST from ignorance to proficiency (3) what are test suite and test case
![[weekly translation go] how to code in go series articles are online!!](/img/bf/77253c87bfa1512f4b8d3d8f7ebe80.png)
[weekly translation go] how to code in go series articles are online!!

【Acwing】第58场周赛 题解

使用 BlocConsumer 同时构建响应式组件和监听状态

TCP shakes hands three times and waves four times. Do you really understand?

Bookmark

Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
随机推荐
智洋创新与华为签署合作协议,共同推进昇腾AI产业持续发展
Acwing 2022 daily question
Super detailed tutorial, an introduction to istio Architecture Principle and practical application
Redis has three methods for checking big keys, which are necessary for optimization
HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙
Interpreting the development of various intelligent organizations in maker Education
283. Moving zero-c and language assisted array method
Shutter WebView example
Monitor the shuttle return button
Spatiotemporal prediction 3-graph transformer
淘宝商品评价api接口(item_review-获得淘宝商品评论API接口),天猫商品评论API接口
Shutter textfield example
Golang interview finishing three resumes how to write
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条
Master the use of auto analyze in data warehouse
Interviewer: what is XSS attack?
MySQL存储数据加密
Analysis of maker education technology in the Internet Era
KDD2022 | 什么特征进行交互才是有效的?
网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗