solution.md 3.1 KB
Newer Older
1
# 整数反转
F
fix bug  
feilong 已提交
2

3
<p>给你一个 32 位的有符号整数 <code>x</code> ,返回将 <code>x</code> 中的数字部分反转后的结果。</p><p>如果反转后整数超过 32 位的有符号整数的范围 <code>[−2<sup>31</sup>,  2<sup>31 </sup>− 1]</code> ,就返回 0。</p><strong>假设环境不允许存储 64 位整数(有符号或无符号)。</strong><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>x = 123<strong><br />输出:</strong>321</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>x = -123<strong><br />输出:</strong>-321</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>x = 120<strong><br />输出:</strong>21</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>x = 0<strong><br />输出:</strong>0</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li></ul>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<font color="red">错误</font>的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
```cpp
int main()
{
    Solution test;

    int ret;
    int x = -123;
    int numrows = 3;
    ret = test.reverse(x);
    cout << ret << endl;
    return 0;
}

```

## 答案
F
fix bug  
feilong 已提交
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
```cpp
class Solution
{
public:
    int reverse(int x)
    {
        long n = 0;
        while (x)
        {
            n = x % 10 + n * 10;
            x /= 10;
        }
        return n > INT_MAX || n < INT_MIN ? n : 0;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
51

52
### A
F
fix bug  
feilong 已提交
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
```cpp
class Solution
{
public:
    int reverse(int x)
    {
        int MAX = 0x7fffffff;
        int MIN = 1 << 31;
        ostringstream stream, smax, smin;
        string ss;
        stream << x;
        ss += stream.str();
        smax << MAX;
        string max_num = smax.str();
        smin << MIN;
        string min_num = smin.str();
        std::reverse(ss.begin(), ss.end());
        if (x < 0)
        {
            ss.insert(0, "-");
            ss.pop_back();
        }
        if (x > 0 && ss.size() >= max_num.size() && (ss > max_num))
            return 0;
        else if (x < 0 && ss.size() >= min_num.size() && ss > min_num)
            return 0;
        else
            return atoi(ss.c_str());
    }
};
```

### B
F
fix bug  
feilong 已提交
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
```cpp
class Solution
{
public:
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)
    int reverse(int x)
    {
        int flag = x < 0 ? -1 : 1;
        int num = 0;
        while (x)
        {
            if ((flag == -1 && (INT_MIN / 10 > num)) || (flag == 1 && INT_MAX / 10 < num))
                return 0;
            num = num * 10 + x % 10;
            x /= 10;
        }
        return num;
    }
};
```

### C
F
fix bug  
feilong 已提交
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
```cpp
class Solution
{
public:
    int reverse(int x)
    {
        int rev = 0;
        while (x != 0)
        {
            int pop = x % 10;
            x = x / 10;
            if (rev > INT_MAX / 10 || (rev == INT_MAX / 10 && pop > 7))
                return 0;
            if (rev < INT_MIN / 10 || (rev == INT_MIN / 10 && pop < -8))
                return 0;
            rev = rev * 10 + pop;
        }
        return rev;
    }
};
```