Skip to content

Conversation

@SuranjanDaw
Copy link
Contributor

No description provided.

@@ -0,0 +1,35 @@
int reverse(int x){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So many checking here. I have a good solution for it:

int reverse(int x){
    long res = 0;
    while (x != 0)
    {
        int tail = x % 10;
        res = res * 10 + tail;
        /* Check overflow */
        if (res > INT_MAX || res < INT_MIN)
            return 0;
        x = x / 10;
    }
    return (int)res;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are using long int. What if the system we are using, supports up to 32-bit? What if the OS does not support long int?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is not limit only for 32-bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah if we don't put that restriction then it will work. So do you want me to change and re-push?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can put the comment on your way, it works only for 32-bit system. And then adding my solution below. Thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Committed the changes. Thanks

leetcode/src/9.c Outdated
@@ -0,0 +1,50 @@


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a better solution:

int reverse(int x){
    long res = 0;
    while (x != 0)
    {
        int tail = x % 10;
        res = res * 10 + tail;
        /* Check overflow */
        if (res > INT_MAX || res < INT_MIN)
            return 0;
        x = x / 10;
    }
    return (int)res;
}

bool isPalindrome(int x) {
    if (x < 0)
        return 0;
    else
        return x == reverse(x);
}

@danghai danghai merged commit e063abd into TheAlgorithms:master Oct 6, 2019
fengjixuchui added a commit to fengjixuchui/C that referenced this pull request Oct 6, 2019
Merge pull request TheAlgorithms#310 from SuranjanDaw/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants