当前位置:网站首页>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
边栏推荐
- Supplement 14: cmake practice project notes (to be continued 4 / 22)
- Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
- 229. 求众数 II
- Installation of zynq platform cross compiler
- [AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
- /etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
- 华为机试--高精度整数加法
- Basic operation of sequence table
- io. Platform. packageRoot; // ignore: deprecated_ Member_ use
- 补充番外14:cmake实践项目笔记(未完待续4/22)
猜你喜欢
Improving 3D object detection with channel wise transformer
Coinbase:关于跨链桥的基础知识、事实和统计数据
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
补:注解(Annotation)
Installation and use of Apache bench (AB pressure test tool)
Interaction of diet gut microbiota on cardiovascular disease
win10, mysql-8.0.26-winx64.zip 安装
AWS EKS 部署要点以及控制台与eksctl创建的差异
补充番外14:cmake实践项目笔记(未完待续4/22)
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
随机推荐
Differences among electric drill, electric hammer and electric pick
Common string processing functions in C language
三十六计是什么
补:注解(Annotation)
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
Improving 3D object detection with channel wise transformer
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
Go反射—Go语言圣经学习笔记
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
Introduction to Cortex-M3 register set, assembly language and C language interface
Redis command Encyclopedia
Detailed explanation of life cycle component of jetpack
无线键盘全国产化电子元件推荐方案
递归调用--排列的穷举
Chlamydia infection -- causes, symptoms, treatment and Prevention
在AWS控制台创建VPC(无图版)
win10, mysql-8.0.26-winx64.zip 安装
从MySQL数据库迁移到AWS DynamoDB
229. 求众数 II