LeetCode 7. Reverse Integer

xiaoxiao2021-02-28  130

Reverse digits of an integer.

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

click to show spoilers.

Note: The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

Subscribe to see which companies asked this question.

反转一个整数。

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

转载请注明原文地址: https://www.6miu.com/read-39424.html

最新回复(0)