More test cases needed


  • 6
    S

    I have the following code w/o taking care of the overflow, but it's got AC.

    int reverse(int x) {
        int sign=x>0?1:-1;
        x=x*sign;
        int rev=0;
        while(x!=0) {
            rev=10*rev+x%10;
            x/=10;
        }
        return rev*sign;
    }

  • 0
    A

    Me too. I do not know how to handle the overflow case

    int reverse(int x) {
    		int result = x % 10;
    		while((x = x/10)){
    			result = result * 10 + x % 10;
    		}
    		return result;
    }

  • 0
    G
    This post is deleted!

  • 0
    G

    Me too. This is my code.

    public boolean isPalindrome(int x) {
        if(x<0)
            return false;
        String pal = Integer.toString(x);
        for(int i =0;i<=(pal.length()-1)/2;i++){
            if(pal.charAt(i)!=pal.charAt(pal.length()-i-1)){
                return false;
            }
        }
        return true;
    }

  • 0
    A

    I think you upload wrong code...


  • 0
    V

    Even this overflow unhandled code got accepted. There is no overflow check in the test cases used.

    public class Solution{
    public int reverse(int x) {
        boolean flag = false;
        if (x < 0) {
            x = 0 - x;
            flag = true;
        }
        int res = 0;
        int p = x;
    
        while (p > 0) {
            int mod = p % 10;
            p = p / 10;
            res = res * 10 + mod;
        }
        if (flag) {
            res = 0 - res;
        }
        return res;
    }
    }
    

  • 0

    Thanks for pointing this out. I have modified the problem statement to require returning 0 for overflow cases. I have also added additional test cases to test overflow behavior.


  • 0

    Your code should get Wrong answer now due to additional test cases for overflow behavior.


Log in to reply
 

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