Determine whether the integer is a palindrome

        Determine whether an integer is a palindrome. The palindrome number refers to the same integer in both positive order (from left to right) and reverse order (from right to left).

Example 1:

        Input: 121
        Output: true

Example 2:
        Input: -121
        Output: false
        Explanation: Reading from left to right, it is -121. Reading from right to left, it is 121-. Therefore it is not a palindrome.

Example 3:
        Input: 10
        Output: false
        Explanation: Reading from right to left, it is 01. Therefore it is not a palindrome.

Advanced:
        Can you solve this problem without converting integers to strings?

       1) Solution: Borrow an array of 32 elements to store each number, then halve it and compare the left and right elements.
        The core code is as follows:
        //myHuiWei.cpp

#define MaxSize 32

bool IsHuiWen(int x) {
    
    
	if (x < 0)
		return false;
	else if (x >= 0 && x <= 9) {
    
    
		return true;
	}
	else {
    
    
		int arry[MaxSize];
		int nLen = 0;
		while (x != 0) {
    
    
			arry[nLen++] = x % 10;
			x /= 10;
		}
		for (int j = 0; j < nLen/2; j++) {
    
    
			if (arry[j] != arry[nLen - j -1])
				return false;			
		}

		return true;
	}
}

class Solution {
    
    
public:
	bool isPalindrome(int x) {
    
    
		return IsHuiWen(x);
	}
};

        3.
        Source of remarks : LeetCode question number: 009
        link: LeetCode palindrome number No009

Guess you like

Origin blog.csdn.net/sanqima/article/details/106867798