当前位置:网站首页>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
边栏推荐
- Mysql50 basic exercises
- Introduction to Cortex-M3 register set, assembly language and C language interface
- 【Pytorch基础】torch.split()用法
- Jetpack 之 LifeCycle 组件使用详解
- MySQL 2013 lost connection to MySQL server during query
- AWS eks add cluster user or Iam role
- 383. 赎金信
- 383. Ransom letter
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
- 单片机串口数据处理(2)——uCOSIII+循环队列接收数据
猜你喜欢

383. 赎金信

Understand the gut organ axis, good gut and good health
![[echart] démarrer avec echart](/img/40/e057f4ac07754fe6f3500f3dc72293.jpg)
[echart] démarrer avec echart

Alibaba cloud IOT transfer to PostgreSQL database scheme

无线键盘全国产化电子元件推荐方案

520. Detect capital letters

Bridge between ischemic stroke and intestinal flora: short chain fatty acids

Installation of zynq platform cross compiler

Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.

补:注解(Annotation)
随机推荐
Go reflection rule
Migrate from MySQL database to AWS dynamodb
QML advanced (IV) - drawing custom controls
Redis command Encyclopedia
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
2019 is coming to an end, the longest day.
2020 is coming to an end, special and unforgettable.
无线键盘全国产化电子元件推荐方案
MYSQL去重方法汇总
Installation and use of Apache bench (AB pressure test tool)
Key points of AWS eks deployment and differences between console and eksctl creation
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
Matlab minimalist configuration of vscode configuration
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
Huawei machine test -- high precision integer addition
递归调用--排列的穷举
520.检测大写字母
Phishing for NFT
Effects of antibiotics on microbiome and human health