LeetCode-Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example:

Input: 123
Output: 321

Example:

Input: -123
Output: -321

Example:

Input: 120
Output: 21

代码如下:

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        
    }
    
    public static int reverse(int x){
        int result = 0;
        while(x!=0){
            int tail = x%10;
            int newResult = result*10+tail;
            if((newResult-tail)/10!=result){
                return 0;
            }
            result = newResult;
            x = x/10;
        }
        return result;
    }
}

 

posted @ 2018-04-13 10:38  alittlecomputer  阅读(117)  评论(0编辑  收藏  举报