Solution in C


  • 4
    N
    int reverse(int x) {
        int digit;
        long rnum = 0;
        int n = x;
        
        while(n != 0)
        {
            digit = n % 10;
            rnum = rnum*10 + digit;
            n = n/10;
           
      // Checking the overflow
            if((rnum > INT_MAX) || (rnum < INT_MIN))
                return 0;
        }       
        return rnum;
    }
    

  • 0
    C

    not at all compiling ..needed lot of correction


  • 0
    F

    @NealC how to find INT_MAX and INT_MIN?


  • 0
    N

    @camelsa This is a running code. Could you please tell me what errors are you getting ?


  • 0
    N

    @fakke These are predefined in C under <limits.h>. You could also refer to the below link for other data types upper and lower limits, if interested.
    https://en.wikibooks.org/wiki/C_Programming/limits.h


  • 0
    A

    rnum*10 maybe overflow and will return a wrong value ,


  • 0
    N

    @a0412comwg I have put the check for overflow of rnum. Do you suspect anything else. Let me know.


  • 0
    A

    @fakke #include <limits.h>


  • 0
    R

    @NealC "rnum = rnum*10 + digit;" maybe overflow, which means your "Checking the overflow" is not credible;


  • 0
    P

    #include <stdio.h>
    #include <limits.h>
    int reverse(long);
    int main()
    {
    printf("%d ", reverse(-123467900111));
    }

    int reverse(long value)
    {
    int flag = 0;
    int digit = 0;
    int results = 0;

    if((value >= INT_MIN) && (value <= INT_MAX))
            return 0;
    
    if (value < 0)
    {
    	flag++;
    	value = ~value + 1;
    }
    	
    while (value > 0)
    {   
    	digit = value % 10;
    	results = (results * 10) + digit;
    	value = value / 10;
    
    	if (results < INT_MIN)
    		return 0;
    }
    
    if (flag > 0)
    	return results * -1;
    else
    	return results;
    

    }

    We need a bigger input size to even try checking overflow or string for that matter. here is a quick fix.


  • 0
    B
    int reverse(int x) {
        long sum = 0;
            
        while (0 != x)
        {
            sum = (x%10) + (sum * 10);
            x /= 10;
        }
        if ((sum > 2147483647) || (sum < -2147483648))
            sum = 0;
        
        return sum;
    }
    

  • 0
    M

    I thought you can't use longs.


  • 0
    H

    You can speed up the function by putting a check for out of range (32 bit Int) or equal to 0. Also, I noticed that no solution explicitly returns an integer even though the function defines the return of an integer (Actually Parsike does). Therefore, casting the long as an int in the return statement is a good solution.

    int reverse(int num) {
        // Test for number out of range or zero
        if ((num > 2147483647) || (num < -2147483648) || (num == 0))
        {
            return 0;
        }
    
        long reversed = 0;
        while (num != 0)
        {
            reversed = (reversed * 10) + (num % 10);
            num /= 10;
        }
    
        // Is reversed number bigger than 32 bit int?
        if ((reversed > 2147483647) || (reversed < -2147483648))
        {
            return 0;
        }
    
        return (int)reversed;
    }
    

  • 0
    B

    int reverse(int x) {
    int flag = 0;
    int res = 0, mod = 0;
    int prev_num;
    if (x < 0)
    {
    x = 0-x;
    flag = 1;
    }

    while (x)
    {
        mod = x%10;
        res = res * 10 + mod;
        if( (res -mod)/10 != prev_num)
            return 0;
        prev_num = res;    
        x = x/10;
    }    
    if (flag)
        return -res;
    return res;
    

    }


Log in to reply
 

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