整数翻转
Int32 //等于int, 占4個字節. 取值范圍:-2147483648 2147483647
判斷整數溢出的方法:
temp=res*10+x%10;
res>214748364,temp溢出;res=214748364,x%10>7溢出。
同理res為負數時
class Solution {
public:
? ? int reverse(int x) {
? ? ? ?int res=0;
? ? ? ? int temp;
? ? ? ? while(x){
? ? ? ? ? ??
? ? ? ? ??
? ? ? ? ? ? if(res>INT_MAX/10||(res==INT_MAX/10&&(x%10>7)))
? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? if(res<INT_MIN/10||(res==INT_MIN/10&&(x%10<-8)))
? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? res=res*10+x%10;
? ? ? ? ? ? x=x/10;
? ? ? ? ? ?
? ? ? ? }
? ? ? ? return res;
? ? }
};
總結
- 上一篇: servlet 中 out.printl
- 下一篇: 最短无序连续子数组