给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
你有注意到翻转后的整数可能溢出吗?因为给出的是32位整数,则其数值范围为[−2^{31}, 2^{31} − 1]。翻转可能会导致溢出,如果反转后的结果会溢出就返回 0。
import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return int整型
*/
public int reverse (int x) {
// write code here
int res=0;
while(x!=0){
int t=x%10;
int newRes= res*10+t;
if((newRes-t)/10 !=res){
return 0;
}
res=newRes;
x=x/10;
}
return res;
}
}