Make writing a habit together! This is the sixth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

I. Problem description

Given an integer x, return true if x is a palindrome integer; Otherwise, return false.

Palindromes are integers that read in positive (left to right) and backward (right to left) order.

  • For example,121It’s a palindrome, and123It isn’t.

Title link: Palindromes

Two, the title requirements

Sample 1

Input: x = 121 Output: trueCopy the code

The sample 2

Input: x = -121 Output: false Description: Read from left to right. The value is -121. Read from right to left, 121-. So it's not a palindrome number.Copy the code

inspection

1. Mathematical thought, palindrome judgment 2Copy the code

Third, problem analysis

Palindrome count! Get up.

Palindrome is the result of traversal from front to back and the result of traversal from back to front corresponding equal, usually and stack combined with many applications, but this problem is relatively simple, we directly use an array to store the number on the line.

First, negative numbers print false directly, because the – sign can’t pair with other numbers.

Second, the data range is up to 10 bits, which is more than enough to store each digit in a 12-bit array.

If the numbers are not equal to each other, return false.

Finally, if none of the above conditions are met, return true.

Four, coding implementation

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0)// Returns false for negative numbers
            return false;
        int i,n=0,a[12];// Initialize the data
        while(x)// The number is stored in an array
        {
            a[n++]=x%10;
            x=x/10;
        }
        for(i=0; i<n/2; i++)// Start and end shrink inward
            if(a[i]! =a[n-i- 1])// not equal, print false
                return false;
        return true;/ / return true}};Copy the code

V. Test results