当前位置:网站首页>力扣_回文数
力扣_回文数
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表示倒叙输出,前两个值表示范围
边栏推荐
- QT - plot other problems
- HDU - 1078 fatmouse and cheese (memory search DP)
- 1807. 替换字符串中的括号内容
- 【Acwing】第58场周赛 题解
- [leetcode] 17. Letter combination of telephone number
- KDD2022 | 什么特征进行交互才是有效的?
- 1807. Replace the parentheses in the string
- NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
- Operation of adding material schedule in SolidWorks drawing
- 玩转gRPC—深入概念与原理
猜你喜欢

一文掌握数仓中auto analyze的使用

How is the entered query SQL statement executed?

PMO:比较25种分子优化方法的样本效率

复数在数论、几何中的用途 - 曹则贤

KDD2022 | 什么特征进行交互才是有效的?

应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设

开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏

HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙

能源势动:电力行业的碳中和该如何实现?

DevEco Device Tool 3.0 Release带来5大能力升级,让智能设备开发更高效
随机推荐
一文掌握数仓中auto analyze的使用
复数在数论、几何中的用途 - 曹则贤
KDD2022 | 什么特征进行交互才是有效的?
What is the stock account opening process? Is it safe to use flush mobile stock trading software?
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
案例分享|金融业数据运营运维一体化建设
Monitor the shuttle return button
Why do you have to be familiar with industry and enterprise business when doing Bi development?
How is the entered query SQL statement executed?
【米哈游2023届秋招】开启【校招唯一专属内推码EYTUC】
哈希表(Hash Tabel)
AcWing 2022 每日一题
赋能数字经济 福昕软件出席金砖国家可持续发展高层论坛
置信区间的画法
Interviewer: what is XSS attack?
《命令行上的数据科学第二版》校对活动重新启动
Shutter WebView example
HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙
El tree combined with El table, tree adding and modifying operations
QT—绘制其他问题