2014年5月13日星期二

Loop Solution - Reverse Integer

The idea is to set result to be 0, and then result * 10 + x % 10, then use next digit of x, which is x / 10 …until x == 0

public class Solution {
    public int reverse(int x) {
        int result = 0;
        while(x != 0){
            result = result * 10 + x % 10;
            x = x / 10;
        }
        return result;
    }
}

没有评论: