当前位置:网站首页>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
边栏推荐
- [AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
- MySQL queries users logged in for at least N consecutive days
- Huawei machine test -- high precision integer addition
- VHDL implementation of 32-bit binary to BCD code
- 阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
- Mysql, binlog log query
- 【Pytorch基础】torch.split()用法
- 第四章 --- 了解标准设备文件、过滤器和管道
- A heavy sword without a blade is a great skill
- C语言: 指针的进阶
猜你喜欢
协程与多进程的完美结合
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
Supplément: annotation
Cortex-M3寄存器组、汇编语言与C语言的接口介绍
Jetpack 之 LifeCycle 组件使用详解
C语言:恶搞小游戏
Summary of MySQL de duplication methods
MYSQL50道基础练习题
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
随机推荐
Understand the gut organ axis, good gut and good health
Chlamydia infection -- causes, symptoms, treatment and Prevention
Supplément: annotation
383. 赎金信
指纹Key全国产化电子元件推荐方案
Introduction to Cortex-M3 register set, assembly language and C language interface
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
A heavy sword without a blade is a great skill
Common string processing functions in C language
MYSQL去重方法汇总
RC低通滤波器的逆系统
在AWS控制台创建VPC(无图版)
QML进阶(四)-绘制自定义控件
Key points of AWS eks deployment and differences between console and eksctl creation
Set classic topics
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
Interaction of diet gut microbiota on cardiovascular disease
Recursive call -- Enumeration of permutations
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data