leecode---07---digital division and remainder operation, take remainder and divide by remainder---flip an integer copy

 
meaning of the title
Flip an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
 
analyze
Given an integer, flip the integer and take the remainder from the last digit.
Finally, it is necessary to judge whether it is out of bounds.
 
 
code
class Solution {
    public int reverse(int x) {
        long result = 0;
        
        //Process each bit in a loop, take out each bit from the back to the front, and then loop once multiplied by 10 to move forward
        while (x != 0) {
            result = result * 10 + x % 10;
            x = x/10;
        }
        
        //Finally determine whether it is out of bounds
        if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) return 0;
        return (int)result;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324628290&siteId=291194637