当前位置:网站首页>leetcode003--判断一个整数是否为回文数
leetcode003--判断一个整数是否为回文数
2022-04-23 04:38:00 【singularityDZF】
import java.util.Scanner;
/**
* 给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
* 回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。
*
* 示例 1:
* 输入:x = 121
* 输出:true
*
* 示例2:
* 输入:x = -121
* 输出:false
* 解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
*
* 示例 3:
* 输入:x = 10
* 输出:false
* 解释:从右向左读, 为 01 。因此它不是一个回文数。
*
* 示例 4:
* 输入:x = -101
* 输出:false
*
*
* 提示:
* -231<= x <= 231- 1
*
*/
public class test03 {
public static void main(String[] args) {
System.out.print("请输入一个数字:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(isPalindrome(num)){
System.out.println(num+"是回文数!");
}else{
System.out.println(num+"不是回文数!");
}
}
public static boolean isPalindrome(int x){
if(x < 0 || x%10==0 && x!=0){
return false;
}
else {
int reverNumber = 0;
while (x > reverNumber) {
reverNumber = reverNumber * 10 + x % 10;
x /= 10;
}
return x == reverNumber || x == reverNumber/10;
}
}
}
版权声明
本文为[singularityDZF]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dangzefei/article/details/124356795
边栏推荐
- 520.检测大写字母
- STM32 upper μ C / shell transplantation and Application
- Set classic topics
- Phishing for NFT
- [paper reading] [3D object detection] voxel transformer for 3D object detection
- C语言:恶搞小游戏
- mysql ,binlog 日志查询
- 指纹Key全国产化电子元件推荐方案
- QML advanced (V) - realize all kinds of cool special effects through particle simulation system
- Create VPC in AWS console (no plate)
猜你喜欢
Alibaba cloud IOT transfer to PostgreSQL database scheme
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
MYSQL去重方法汇总
Effects of antibiotics on microbiome and human health
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
MYSQL50道基础练习题
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
Installation du compilateur croisé de la plateforme zynq
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
随机推荐
Introduction to Cortex-M3 register set, assembly language and C language interface
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
顺序表的基本操作
shell wc (统计字符数量)的基本使用
C language character constant
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
Nature medicine reveals individual risk factors of coronary artery disease
TreeSet after class exercises
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
mysql table 中增加列的SQL语句
Basic use of shell WC (counting the number of characters)
STM32单片机ADC规则组多通道转换-DMA模式
io.Platform.packageRoot; // ignore: deprecated_member_use
Installation and use of Apache bench (AB pressure test tool)
国外LEAD,联盟经理常见问答
383. Ransom letter
Detailed explanation of life cycle component of jetpack
Redis 命令大全
thymeleaf th:value 为null时报错问题
QML进阶(四)-绘制自定义控件