当前位置:网站首页>力扣_回文数
力扣_回文数
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表示倒叙输出,前两个值表示范围
边栏推荐
- Drop down selection of Ehlib database records
- 1807. Replace the parentheses in the string
- Cadre WebGIS - kalrry
- Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
- 可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
- WebGIS框架---kalrry
- 输入的查询SQL语句,是如何执行的?
- Nat. Commun.| 机器学习对可突变的治疗性抗体的亲和力和特异性进行共同优化
- Flink1.13 SQL basic syntax (I) DDL, DML
- GTEST from ignorance to proficiency (3) what are test suite and test case
猜你喜欢
Shutter textfield example
Why do you have to be familiar with industry and enterprise business when doing Bi development?
PMO:比较25种分子优化方法的样本效率
QT—双缓冲绘图
案例分享|金融业数据运营运维一体化建设
开源之夏专访|Apache IoTDB社区 新晋Committer谢其骏
Machine learning notes mutual information
bizchart+slider实现分组柱状图
机器学习笔记 - 互信息Mutual Information
i. Mx6ull driver development | 24 - platform based driver model lights LED
随机推荐
El tree combined with El table, tree adding and modifying operations
bizchart+slider实现分组柱状图
QT—绘制其他问题
HBuilder X 常用的快捷键
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
How much is the minimum stock account opening commission? Is it safe to open an account online
并发网络模块化 读书笔记转
哈希表(Hash Tabel)
HDU - 2859 Phalanx(DP)
You don't have to run away to delete the library! Detailed MySQL data recovery
Is it safe to open an account in the stock of Caicai college? Can you only open an account by digging money?
服装企业为什么要谈信息化?
Interviewer: what is XSS attack?
案例分享|金融业数据运营运维一体化建设
Rotary transformer string judgment
力扣3_383. 赎金信
Spatiotemporal prediction 3-graph transformer
sqlserver对数据进行加密、解密
机器学习笔记 - 互信息Mutual Information
使用 BlocConsumer 同时构建响应式组件和监听状态