Home LeetCode. 7. Reverse Integer
Post
Cancel

LeetCode. 7. Reverse Integer

Reverse Integer

image

[Link] https://leetcode.com/problems/reverse-integer/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
    public int reverse(int x) {
        if(x == 0) return 0;
        int sign = 1;
        long ln = new Long(x), ans = 0;
        if(ln < 0) sign = -1;
        while(ln != 0) {
            ans *= 10;
            ans += ln%10;
            ln = ln/10;
        }
        if(ans >= (long)Integer.MIN_VALUE && ans <= (long)Integer.MAX_VALUE) return (int)ans;
        else return 0;
    }
}
This post is licensed under CC BY 4.0 by the author.