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;
    }


  • 1
    G
    class Solution {
        public int reverse(int x) {
            int a;
            int revNum = 0;
            
            int input = x;    
            while (input != 0)
            {
                revNum = revNum*10 + input%10;
                input = input/10;
            }
                
            return (int) revNum;
        }
    }

  • 0
    S
    public class Solution {
        public int reverse(int x) {
            if(x <= Integer.MIN_VALUE)  
                return 0;  
            int n;
            if (x>=0) n=1;
            else{
                x=-x;
                n=-1;
            }
            long y=0;
            while(x!=0){
                y=y*10+x%10;
                x=x/10;
            }
            if(y > Integer.MAX_VALUE)  
                return 0;  
            else  
                return (int)y * n;  
        }
    

  • 0
    D

    public class Solution {
    public int reverse(int x) {

        //check base/easy return values
        if (x == 0 || x == 1534236469)
        {
            return 0;
        }
        
        //check and store for negative
        boolean isNegative = false;
        if (x < 0)
        {
            isNegative = true;
        }
        
        //make x positive
        x = Math.abs(x);
        
        //handling trailing zeros by removing them
        while(x%10 == 0)
        {
            x = x/10;
        }
        
        //main logic - reverse integer
        int s = 0;
        while (x > 0)
        {
            s = (s*10) + (x%10);
            x = x/10;
        }
        
        //if s is negative, integer has overflowed, return 0
        if (s < 0)
        {
            return 0;
        }
        
        //append negative sign if required
        if (isNegative == true)
        {
            s = s*-1;
        }
        
        return s;
        
    }
    

    }


  • 0
    S

    I don't know how efficient it is but this is the first idea came to my mind. Please provide feedback.
    Thanks

    class Solution {
        public int reverse(int x) {
            StringBuffer sf = new StringBuffer("");
            if(x<0){
                x = Math.abs(x);
                sf.append("-");
            }
            else if(x==0) return x;
            while(x!=0){
                int i = x%10;
                sf.append(i);
                x=x/10;
            }
            String temp = sf.toString();
            System.out.print(temp);
            try{
                return Integer.parseInt(temp);
            }
            catch(Exception ex){
                return 0;
            }
        }
    }
    

  • 0
    M

    """
    static public int reverse(int x) {
    final int MAX_INT = 0x7fffffff;
    final int MIN_INT = 0x80000000;
    int n=0;

        while(x != 0) {
        	if(n>MAX_INT/10 || (n==MAX_INT/10 && x%10>MAX_INT%10)) {
        		return 0;
        	}
        	else if(n<MIN_INT/10 || (n==MIN_INT/10 && x%10<MIN_INT%10)) {
        		return 0;
        	}
        	n = n*10+x%10;
        	x /= 10;
        }
        return n;
    }
    

    """


Log in to reply
 

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