当前位置:网站首页>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
边栏推荐
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
- QML advanced (V) - realize all kinds of cool special effects through particle simulation system
- 520.检测大写字母
- Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
- 520. Detect capital letters
- io. Platform. packageRoot; // ignore: deprecated_ Member_ use
- win10, mysql-8.0.26-winx64. Zip installation
- mysql table 中增加列的SQL语句
- Iron and intestinal flora
- 第四章 --- 了解标准设备文件、过滤器和管道
猜你喜欢
![[echart] Introduction to echart](/img/40/e057f4ac07754fe6f3500f3dc72293.jpg)
[echart] Introduction to echart

Mysql50 basic exercises

协程与多进程的完美结合
![[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022](/img/b2/269ae2e9be269c2bff73eb1da5b55d.png)
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022

QML advanced (V) - realize all kinds of cool special effects through particle simulation system

Installation of zynq platform cross compiler

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

The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video

Effects of antibiotics on microbiome and human health
![[BIM introduction practice] wall hierarchy and FAQ in Revit](/img/95/e599c7547029f57ce23ef4b87e8b9a.jpg)
[BIM introduction practice] wall hierarchy and FAQ in Revit
随机推荐
STM32 MCU ADC rule group multi-channel conversion DMA mode
FAQ of foreign lead and alliance Manager
智能电子秤全国产化电子元件推荐方案
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
Detailed explanation of life cycle component of jetpack
指纹Key全国产化电子元件推荐方案
针对NFT的网络钓鱼
Why recommend you to study embedded
Go reflection - go language Bible learning notes
io.Platform.packageRoot; // ignore: deprecated_member_use
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
Common string processing functions in C language
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
Set classic topics
【Echart】echart 入门
兼容NSR20F30NXT5G的小体积肖特基二极管
QML进阶(四)-绘制自定义控件
无线键盘全国产化电子元件推荐方案
Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
win10, mysql-8.0.26-winx64. Zip installation