## Creek Shi

Hew out of the mountain of despair a stone of hope !

# LeetCode Reverse Integer

int reverse(int x,bool &flag) {//flag means if the result is overflow
int signal=1;
if (x<0)
{
signal=-1;
}
int temp,ret=0,newv=0;
while(x!=0)
{
temp = x%10;
x = x/10;
if (x!=0)
{
newv = newv*10 + temp;
}
else//last bit may overflow
{
if (signal<0)
{
if ( 10*newv + temp >= 0)
{
flag = false;
ret = -1;
return ret;
}
ret = 10 * newv + temp;
}
else
{
if ( 10*newv + temp <= 0)
{
flag = false;
ret = -1;
return ret;
}
ret = 10 * newv + temp;
}

}
}
return ret;
}

-20的二进制补码 = 11101100       -25的二进制补码 = 11100111
-20             11101100
+（-25）    =    + 11100111
-45            111010011

-45 的补码刚好是 11010011，所以以上式子正确。

#### LeetCode7——Reverse Integer

2015-01-26 11:21:12

#### [LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

2015-02-28 00:17:09

#### 【LeetCode-Algorithm】【7-Reverse Integer】【Python】

2016-09-20 19:04:47

#### [C++]LeetCode 7:Reverse Integer（翻转整数）

2015-05-02 14:23:48

#### 【leetcode-7】Reverse Integer(C语言)

2015-07-11 08:43:10

#### leetcode第7题，Reverse Integer

2017-02-18 20:30:16

#### 【LeetCode-面试算法经典-Java实现】【007-Reverse Integer（翻转整数）】

2015-07-18 06:25:38

#### [leetcode 7] Reverse Integer(整数反转) c函数实现

2017-03-22 18:58:22

#### [LeetCode][7]Reverse Integer解析与位运算实现 -Java实现

2016-06-06 15:57:38

#### Leetcode 7 Reverse Integer 反转数字

2015-04-20 10:28:58