# 整数反转 Reverse Integer

## 题目

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: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

## 思路&题解 ``````public class Solution {
public int reverse(int x) {
int min = -1<<31;
int max = -min - 1;
int result = 0;
while(x != 0) {
// 取余数，相当于获取低位数值
int rightPart = x%10;
// 易错点：除以10，相当于移动最低位,
x /= 10;
// 易错点：此处可能会溢出
//result = result * 10 + rightPart;
if (result > max/10 || (result == max/10 && rightPart > 7)) {
return 0;
}
if (result < min/10 || (result == min/10 && rightPart < -8)) {
return 0;
}
result = result * 10 + rightPart;
}
return result;
}
}
``````

## 知识点分析

• 逐位处理

`tmp = x%10`

• 位增减处理

`x /= 10`

• 整型溢出原理

``````// 易错点：此处可能会溢出
result = result * 10 + rightPart;
``````
1. 如果上述语句溢出，由于rightPart是取值为[0,9],不可能溢出，因此溢出的一定是`result * 10`
2. 如果`result * 10`溢出，则`result * 10 >= max` 或者 `result * 10 =< min`，等于不一定会溢出，需要结合rightPart的取值；

1. 如果`result * 10 >= max`，则`result >= max/10`
2. `result > max/10`时，一定是溢出的
3. `result == max/10`时，需要结合rightPart，即两者只和如果大于max就溢出
4. 因为max的个位数是7，所以`rightPart > 7`时溢出
5. 整合起来就是第一个if的逻辑块

1. 如果`result * 10 <= min`，则`result <= min/10`
2. `result < min/10`时，一定是溢出的
3. `result == min/10`时，需要结合rightPart，即两者只和如果小于min就溢出
4. 因为min的个位数是8，所以`rightPart < -8`时溢出
5. 整合起来就是第二个if的逻辑块

``````System.out.println("max = " + max + "，" + Integer.toBinaryString(max) + ", "+ max%10);
System.out.println("min = " + min + "，" + Integer.toBinaryString(min) +"，"+min%10);
``````

``````max = 2147483647，1111111111111111111111111111111, 7
min = -2147483648，10000000000000000000000000000000，-8
``````
{"ft_lang_label":"__label__zh","ft_lang_prob":0.6506457,"math_prob":0.99567735,"size":2531,"snap":"2020-34-2020-40","text_gpt3_token_len":1543,"char_repetition_ratio":0.1436486,"word_repetition_ratio":0.02631579,"special_character_ratio":0.34413275,"punctuation_ratio":0.1155914,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9853341,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-26T01:31:30Z\",\"WARC-Record-ID\":\"<urn:uuid:001b2bce-0435-4c88-a836-1ae0c9d097b3>\",\"Content-Length\":\"63617\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2a21d1fd-a13f-4a30-9f4f-214cfcac9716>\",\"WARC-Concurrent-To\":\"<urn:uuid:3cf515e3-260f-4f8c-9918-9664073ff133>\",\"WARC-IP-Address\":\"104.27.178.221\",\"WARC-Target-URI\":\"https://hacktons.cn/books/dev/leetcode/reverse-integer.html\",\"WARC-Payload-Digest\":\"sha1:RBE24PZVUUHG5JZGUEWK4I6VBRZKGR2G\",\"WARC-Block-Digest\":\"sha1:IR5FWC44KDRUXO2RZPAZD4IFPQUKQ7QC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400232211.54_warc_CC-MAIN-20200926004805-20200926034805-00570.warc.gz\"}"}