How do we handle the overflow case?


  • 29
    C

    Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).


  • -8
    S

    As a cpp coder, I will use the long long to handle the overflow case. Since all input number is in int range, there is no way to overflow the long long.


  • -12
    J
                                 int reverse(int x) {
                                 // IMPORTANT: Please reset any member data you declared, as
                                // the same Solution instance will be reused for each test case.
                                 int r=0;
                                 for (; x; x /= 10)
                                   {
                                        r = r * 10 + x % 10;
                                   }
                                 if((r>INT_MAX)||(r<INT_MIN))
                                 return 0;
                                  return r;
    }};//if overflow, return 0;
    

  • 0

    Could you please format your code properly? Select your code and click on the {} button to format it. Also, it would be great to add a few sentences to explain your approach.


  • 2
    S

    how come if((r>INT_MAX)||(r<INT_MIN))is coming in play , if r ever cross integer limit it will automatically convert in limit, eg. if input is 2147483646 , so output should be 6463847412 (which mean error should return 0) but this code will result -2126087180.
    correct me if i am wrong


  • 0
    O

    you are right, so if the reversed int is negative, I tried to return -1 and got accepted.


  • 0
    S

    even this code is getting accepted due to lack of good test cases. if your input is negative then your code will fails.


  • 0
    J

    function should return int, but not long long


  • 0
    S

    actually , you can assign a long long value to a int, if it can be reversed.


  • 4
    E

    I will just treat them as strings.


  • 0
    L

    Could you elaborate on that answer?


  • 43
    W

    I will let it return 0. Unfortunately, leetcode does not have the test case which covers the overflow. @1337, please consider to add test case "0x7fffffff". It should return 0. If there is no overflow check, it will return a garbage.
    Here is my code:

    class Solution {
    public:
    	int reverse(int x) {
    		bool neg=false;
    		if (x < 0) neg = true, x = -x;
    		unsigned int ret = 0;
    		while (x / 10){
    			ret = ret*10+ x%10;
    			x = x / 10;
    		}
    		ret =ret*10+x;
    		if (ret>0x7fffffff) return 0; //overflow check.
    		if (neg) return -(int)ret;
    		return (int)ret;
    	}
    };
    

  • 0
    H

    if( result < result*10+temp )

        		result = result*10+temp;
    
        	else
    
        		exit(1);

  • 0
    H

    did you test your method and test any cases, I'm afraid a long long data will be truncated when transformed into a int data.


  • 0
    S

    Nope, I just use it in the transforming process. When returning result, I will compare it with Max_int to handle the overflow case.


  • 0
    E

    How about use INT_MAX to replace 0x7fffffff? Maximum value could vary from computer to computer, INT_MAX specifies the max value that can be stored in local computer, it's defined in "limits.h".


  • 0
    D

    Using unsigned int is unsafe and may also be overflow after reversing ret. long long is safe.


  • 0
    S

    Yeah I did it just by using stringstream


  • 0
    Z

    you mean you will redesign the function to make it return a string? sounds good.


  • 1
    Z

    I think returning a 0 on overflow works.

    You can refer to below code.
    It's using a larger data type (long ) to check smaller data type's overflow.

    public static int checkedAdd(int a, int b) {
        long result = (long) a + b;
        checkNoOverflow(result == (int) result);
        return (int) result;
    }

Log in to reply
 

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