leetCode/src/main/java/com/leetcode/huawei/ReverseInteger.java

35 lines
934 B
Java
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}