当前位置:网站首页>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
边栏推荐
- Error occurs when thymeleaf th: value is null
- AWS eks add cluster user or Iam role
- Understand the gut organ axis, good gut and good health
- io.Platform.packageRoot; // ignore: deprecated_member_use
- Go reflection rule
- [AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
- Matlab minimalist configuration of vscode configuration
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
- VHDL语言实现32位二进制数转BCD码
猜你喜欢
补充番外14:cmake实践项目笔记(未完待续4/22)
C语言:恶搞小游戏
那些年我面试过的Android开发岗总结(附面试题+答案解析)
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
Bacterial infection and antibiotic use
STM32 upper μ C / shell transplantation and Application
Installation du compilateur croisé de la plateforme zynq
520.检测大写字母
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
Chapter 4 - understanding standard equipment documents, filters and pipelines
随机推荐
IDE idea automatic compilation and configuration of on update action and on frame deactivation
国外LEAD,联盟经理常见问答
[mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)
test
Supplement: Annotation
/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
MySQL 2013 lost connection to MySQL server during query
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
zynq平臺交叉編譯器的安裝
Basic use of shell WC (counting the number of characters)
Use recyclerview to realize left-right side-by-side classification selection
mysql ,binlog 日志查询
[paper reading] [3D object detection] voxel transformer for 3D object detection
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
在AWS控制台创建VPC(无图版)
Go reflection rule
A heavy sword without a blade is a great skill
RC低通滤波器的逆系统
Bridge between ischemic stroke and intestinal flora: short chain fatty acids