Leetcode-Exam Bank-7-Integer Inversion-c++

integer inversion

1. Topic

Given a 32-bit signed integer x, return the result of inverting the numeric portion of x.

Returns 0 if the inverted integer exceeds the range [−2 31 , 2 31 − 1] of a 32-bit signed integer.

Assume the environment does not allow storing 64-bit integers (signed or unsigned).

Example 1:

输入:x = 123
输出:321

Example 2:

输入:x = -123
输出:-321

Example 3:

输入:x = 120
输出:21

Example 4:

输入:x = 0
输出:0

hint:

  • -231 <= x <= 231 - 1

Second, through the code

Execution time: 8 ms, beat 48.62% of all C++ submissions
Memory consumption: 6.3 MB, beat 5.06% of all C++ submissions

class Solution {
public:
    int reverse(int x) {
        vector<int> num;
        long long result = 0,a = 0;
        if(x>=0){
            for(int i = 1; ;i++){
                a = x%10;
                x = x/10;
                if(a || x)
                    num.push_back(a);
                if(x == 0) break;
            }
            for(int i = num.size()-1; i>-1; i--){
                result += num[i]*pow(10,num.size()-i-1);
            }
            if(result > pow(2,31)-1 || result < -pow(2,31))
                    result = 0;
            return result;
        }else{
            for(int i = 1; ;i++){
                a = abs(x)%10;
                x = abs(x)/10;
                if(a || x)
                    num.push_back(a);
                if(x == 0) break;
            }
            for(int i = num.size()-1; i>-1; i--){
                result += num[i]*pow(10,num.size()-i-1);
            }
            if(result > pow(2,31)-1 || result < -pow(2,31))
                    result = 0;
            return -result;
        }
    }
};

I like cooking. . . What to do with the dishes, replay! ! !

  • It is not necessary to create an array to store each bit, it can be obtained directly in the loop, for example
int rev = 0;
while(...){
	int digit = x % 10;
	x /= 10;
	rev = rev * 10 + digit;
}
  • The title gives "assuming that the environment does not allow the storage of 64-bit integers (signed or unsigned)", so I think long long should not be used. Therefore, here involves a usage of INT_MIN and INT_MAX, which represent the minimum lower bound and maximum upper bound of int respectively.
  • OK, so how can the final result not exceed the boundary (be careful not to judge the boundary after calculating the result, because you have already crossed the boundary, and an error will be reported at this time), for example
//仔细思考一下,是不是很妙啊!!!
while(){
	if (rev < INT_MIN / 10 || rev > INT_MAX / 10) {
		return 0;
	}
	int digit = x % 10;
	x /= 10;
	rev = rev * 10 + digit;
}
  • Do not forget how to find the modulus of negative numbers

Three, the official solution

class Solution {
public:
    int reverse(int x) {
        int rev = 0;
        while (x != 0) {
            if (rev < INT_MIN / 10 || rev > INT_MAX / 10) {
                return 0;
            }
            int digit = x % 10;
            x /= 10;
            rev = rev * 10 + digit;
        }
        return rev;
    }
};

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/reverse-integer/solution/zheng-shu-fan-zhuan-by-leetcode-solution-bccn/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin blog.csdn.net/Red_Elf/article/details/117046909