整数反转

This commit is contained in:
LingZhaoHui 2021-02-25 22:49:46 +08:00
parent 37b4b2dcf1
commit 940cd3b85a
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.leetcode.huawei;
/**
*
* 给你一个 32 位的有符号整数 x 返回 x 中每位上的数字反转后的结果
如果反转后整数超过 32 位的有符号整数的范围 [231,  231  1] 就返回 0
假设环境不允许存储 64 位整数有符号或无符号
来源力扣LeetCode
链接https://leetcode-cn.com/problems/reverse-integer
著作权归领扣网络所有商业转载请联系官方授权非商业转载请注明出处
*
*/
public class ReverseInteger {
public int reverse(int x) {
long n = 0;
while(x != 0) {
n = n*10 + x%10;
x = x/10;
}
return (int)n==n? (int)n:0;
}
public static void main(String[] args) {
ReverseInteger reInteger = new ReverseInteger();
int x = -123;
int res = reInteger.reverse(x);
System.out.println(res);
}
}