Solution in Java.


  • 0
    K

    '''
    int reverseInteger(int input){
    int output=0;
    int sign=1;
    if (input<0) sign =-1;
    input = input*sign; //loosing the sign of input number. we will later multiply output and
    //sign to generate appropriately signed integer.

    while (input>0){  //if we hadn't lost the sign of input above, negative input would not enter this loop ever!
       output = output*10 + input%10;
       input = input/10;
      } // end of while loop. At this point, input=0, output has all reversed digits. We just need to multiply output 
               //and sign to get the correct sign of the output
    

    System.out.println("output");
    return output*sign;

    }// end of solution
    '''

    0_1503275563658_Image_super.jpeg


  • 0
    N

    '''
    public int reverse(int x) {
    int num =0;
    long temp=0;
    double val = 0;
    if(x == 0)
    return 0;
    while(x>0)
    {
    temp = temp10+x%10;
    if(temp >Integer.MAX_VALUE)
    return 0;
    else
    num = (int) temp;
    x= x/10;
    }
    while(x<0)
    {
    val = val
    10+x%10;
    if(val < (double)Integer.MIN_VALUE)
    return 0;
    else
    num = (int) val;
    x = x/10;
    }
    return num;
    }
    '''


  • 0
    S

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


Log in to reply
 

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