整数反转
This commit is contained in:
parent
37b4b2dcf1
commit
940cd3b85a
34
src/main/java/com/leetcode/huawei/ReverseInteger.java
Normal file
34
src/main/java/com/leetcode/huawei/ReverseInteger.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user