# Share my cpp solution (4ms)(not using long)

• class Solution {
public:
int reverse(int x) {
int a = r(x);
if(r(a) != x && x % 10 != 0) return 0;
else return a;
}

int r(int x)
{
int r = 0;
while(x != 0)
{
r *= 10;
r += x % 10;
x /= 10;
}
return r;
}
};

But do the reverse twice.

•  int reverse(int x) {
vector<int> stack;
int max = 0x7fffffff;
int min = 0x80000000;
max = max / 10;
min = min / 10;
while (x)
{
stack.push_back(x % 10);
x /= 10;
}
int sum = 0;
for (vector<int>::iterator p = stack.begin(); p != stack.end(); p++)
{
if (sum > max || sum<min) sum = 0;
else sum = sum * 10 + *p;

}
return sum;
}

I reverse olny once,but the time is 8ms.Is it the reason for using vector？
the other,I try after,not use vector and only once,but also is 4ms....

int reverse(int x) {
int max = 0x7fffffff;
max = max / 10;
int temp = 0;
while (x)
{
if(temp>max || temp<(-1*max)) temp=0;
else temp = temp*10+x%10;
x /= 10;
}
return temp;
}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.