当前位置:网站首页>leetcode002--将有符号整数的数字部分反转
leetcode002--将有符号整数的数字部分反转
2022-04-23 04:38:00 【singularityDZF】
import java.util.Scanner;
public class test02 {
/**
* 给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
* 如果反转后整数超过 32 位的有符号整数的范围[?2**31, 2**31? 1] ,就返回 0。
* 假设环境不允许存储 64 位整数(有符号或无符号)。
*
* 示例 1:
* 输入:x = 123
* 输出:321
* 示例 2:
* 输入:x = -123
* 输出:-321
* 示例 3:
* 输入:x = 120
* 输出:21
* 示例 4:
* 输入:x = 0
* 输出:0
*
* 提示:
* -231 <= x <= 231 - 1
*
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个整数:");
int x = sc.nextInt();
System.out.println("整数反转后为:"+reverse(x));
}
public static int reverse(int x){
int rev = 0;
while( x != 0 ){
if( rev > Integer.MAX_VALUE && rev <Integer.MIN_VALUE){
return 0;
}
int digit = x%10;
rev = rev*10 + digit;
x /= 10;
}
return rev;
}
}
版权声明
本文为[singularityDZF]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dangzefei/article/details/124356760
边栏推荐
- 2019 is coming to an end, the longest day.
- AWS EKS添加集群用户或IAM角色
- win10, mysql-8.0.26-winx64.zip 安装
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- [AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
- The perfect combination of collaborative process and multi process
- Coinbase: basic knowledge, facts and statistics about cross chain bridge
- Go reflection rule
- FAQ of foreign lead and alliance Manager
- 【Echart】echart 入門
猜你喜欢

上海航芯技术分享 | ACM32 MCU安全特性概述

智能电子秤全国产化电子元件推荐方案

Bacterial infection and antibiotic use
![[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022](/img/eb/916a3fc1a89356fd8e74b943172ac3.png)
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022

Phishing for NFT

Set classic topics

协程与多进程的完美结合
![[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022](/img/c2/fd9531e48118e384bc5d490ee8e506.png)
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022

【论文阅读】【3d目标检测】point transformer

MySQL queries users logged in for at least N consecutive days
随机推荐
针对NFT的网络钓鱼
Installation of zynq platform cross compiler
test
A heavy sword without a blade is a great skill
智能电子秤全国产化电子元件推荐方案
Improving 3D object detection with channel wise transformer
Basic use of shell WC (counting the number of characters)
RC低通滤波器的逆系统
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
Set classic topics
win10, mysql-8.0.26-winx64.zip 安装
国外LEAD,联盟经理常见问答
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
383. Ransom letter
Mysql50 basic exercises
QML进阶(四)-绘制自定义控件
Go reflection - go language Bible learning notes
C language character constant