【LeetCode】 007. Reverse Integer

来源:互联网 发布:手机二维码扫描软件 编辑:程序博客网 时间:2024/05/21 09:37

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

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


0 0
原创粉丝点击