Solution In Java


  • 0
    P

    class Solution {
    public int reverse(int x) {
    long reversed = 0;
    long num = x;
    num = Math.abs(num);
    if(x==0){
    return x;
    }
    while(num!=0){
    long digit = num%10;
    num = num/10;
    reversed = reversed*10 + digit;
    }
    System.out.println(reversed);
    if(x<0){
    reversed = reversed * (-1);
    System.out.println(reversed);
    if(reversed < Integer.MIN_VALUE){
    return 0;
    }
    }
    else{
    if(reversed > Integer.MAX_VALUE){
    return 0;
    }
    }
    return Math.toIntExact(reversed);
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.