LeetCode.9- number of palindrome

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_32157579/article/details/102721526

Determine whether an integer is a palindrome. Palindrome correct order (from left to right) and reverse (right to left) reading is the same integer.

Example 1:
Input: 121
Output: true

Example 2:
Input: -121
Output: false
interpretation: read from left to right, -121. Reading from right to left, as 121-. So it is not a palindrome number.

Example 3:
Input: 10
Output: false
interpretation: reading from right to left, to 01. So it is not a palindrome number.

C # language

public static bool IsPalindrome(int x)
{
    if (x < 0) return false;
    int num = 0, y = x;
    while (x > 0)
    {
        if (num > int.MaxValue / 10 || (num == int.MaxValue / 10 && x > 7)) return false;
        if (num < int.MinValue / 10 || (num == int.MinValue / 10 && x < -8)) return false;
        num = num * 10 + x % 10;
        x /= 10;
    }
    return num == y;
}

Guess you like

Origin blog.csdn.net/qq_32157579/article/details/102721526