题目:
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
题意:
把给定数字翻转它的数位。
思路:
翻转后溢出int类型输出0!!!从最右边非0位开始翻转!!!~~~不想做题随便水水,好歹养成每日一题的习惯,好想有个妹纸一起打卡学习啊。白日做梦系列emmm
Code:
class Solution {
public:
int reverse(int x) {
if(x==0) return 0;
int flag1=0,flag2=0;
long long y=0,max=((long long)1<<31)-1,min=-(1<<31);
if(x<0){
flag1=1;
x=-x;
}
while(x){
int t=x%10;
if(!flag2&&!t){
x/=10;
continue;
}
flag2=1;
y=y*10+t;
x/=10;
}
if(flag1) y=-y;
return y>max||y<min?0:y;
}
};