leetcode-7. Reverse Integer

xiaoxiao2021-02-28  71

https://leetcode.com/problems/reverse-integer/#/description 注意溢出的情况

Reverse digits of an integer.

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

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

public class Solution { public int reverse(int x) { int a; long temp=0; while(x!=0) { a=x%10; temp=temp*10+a; x/=10; } return (temp>Integer.MAX_VALUE || temp<Integer.MIN_VALUE )?0:(int)temp; } }
转载请注明原文地址: https://www.6miu.com/read-85628.html

最新回复(0)