13100 a palindrome?


  • 1
    M

    I found a fault in my code, which allows 13100 to be a palindrome. ( the algorithm ignores zeros at tail)

    but the code passes the test and is accepted.

    so maybe the test cases should be updated.

    code is here: ( originally no "return false" on "x%10 == 0", but passed the test)

    class Solution {
    public:
        bool isPalindrome(int x) {
            if (x < 0) {
                return false;
            }
            
            if (x < 10) {
                return true;
            }
            
            if (x % 10 == 0) {
                return false;
            }
            
            int rev = 0;
            while (x > 0 and x > rev) {
                rev *= 10;
                rev += x % 10;
                x /= 10;
            }
            
            if (x != 0 and ( x == rev or x == rev / 10 )) {
                return true;
            }
            else {
                return false;
            }
        }
    };
    

  • 0
    S

    Could you please paste your code and tell your algorithm? Detail why the result comes. Thanks.


  • 0
    M
    This post is deleted!

  • 0
    S
    This post is deleted!

  • 0
    C

    Your code is correct, because it returns false with 13100 when it meets the condition that (13100 % 10 == 0)


  • 0

    @msmouse Thanks for your test case. I have added this test case to the judge.


  • 0
    U
    This post is deleted!

  • 0
    H

    Will the stack algorithm be considered as O(n) space complexity or in term of auxiliary complexity? I thought “no extra space used” means you cannot use recursive or iteration, or even a single temp variable, so came up a “brute force” solution even though I don’t think it will be accepted by interviewer:

    class Solution {
    public: // 0, 9, 11, 191, 202, 6776, 53235, 234432, 1234321, 12344321, 123454321, 12345654321, ...
        bool isPalindrome(int x) { //120ms
        	#define D0(x) x%10
        	#define D1(x) x%100/10
        	#define D2(x) x%1000/100
        	#define D3(x) x%10000/1000
        	#define D4(x) x%100000/10000
        	#define D5(x) x%1000000/100000
        	#define D6(x) x%10000000/1000000
        	#define D7(x) x%100000000/10000000
        	#define D8(x) x%1000000000/100000000
        	#define D9(x) x%10000000000/1000000000
        	if (x<0) return false;
        	if (x<10) return true;
        	if (x<100) return D1(x)==D0(x);
        	if (x<1000) return D2(x)==D0(x); // 202
        	if (x<10000) return D3(x)==D0(x) && D2(x)==D1(x); //6776
        	if (x<100000) return D4(x)==D0(x) && D3(x)==D1(x); //53235
        	if (x<1000000) return D5(x)==D0(x) && D4(x)==D1(x) && D3(x)==D2(x);//234432
        	if (x<10000000) return D6(x)==D0(x) && D5(x)==D1(x) && D4(x)==D2(x);//1234321
        	if (x<100000000) return D7(x)==D0(x) && D6(x)==D1(x) && D5(x)==D2(x) && D4(x)==D3(x); //12344321
        	if (x<1000000000) return D8(x)==D0(x) && D7(x)==D1(x) && D6(x)==D2(x) && D5(x)==D3(x);//123454321
        	if (x<10000000000) return D9(x)==D0(x) && D8(x)==D1(x) && D7(x)==D2(x) && D6(x)==D3(x) && D5(x)==D4(x);//12345654321
        	return false;
        }
    };

Log in to reply
 

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