LeetCode【7】.Reverse Integer--java实现

Reverse Integer

题目要求:给定一个int 类型值,求值的反转,如下:

     Example1: x = 123, return 321
     Example2: x = -123, return -321

     简单问题一般蕴含细节的处理。思路很简单,直接贴Java程序:

public class Solution {
    public int reverse(int x) {
        
        int head = x/10;
        int tail = x%10;
        long re = 0;
        
        while(head!=0||tail!=0)
        {
            re = re*10 + tail;
            tail = head%10;
            head = head/10;
        }
        
        re = re < Integer.MIN_VALUE? 0:re;
        re = re > Integer.MAX_VALUE? 0:re;
        
        return (int)re;
    }
}


你可能感兴趣的:(LeetCode-Java)