Leetcode(3) - number of palindromes

Determines whether an integer is a palindrome. A palindrome is an integer that reads the same in positive order (from left to right) and in reverse order (from right to left).

Example 1:

Input: 121
output: true

Example 2:

Input: -121
output: false
Explanation: Read from left to right, -121. Reading from right to left, it is 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
output: false
Explanation: Read from right to left, it is 01. Therefore it is not a palindrome.
class Solution {
public:
    bool isPalindrome(int x)
    {
        if(x<0) return false;
        char num[20]={'\0'};
        int t=0;
        while(x)
        {
            num[t ++]=x% 10 + ' 0 ' ;
            x=x/10;
        }
        //itoa(x,num,10);
        int i=0,j=0,k=0;
        while(num[i]!='\0')
        {
            j++;
            i++;
        }
        if(j==1)
        {
            return true;
        }
        while(k!=(j-1)&&j>1)
        {
            if(num[k]!=num[j-1])
            {
                return false;
            }
            k++;
            j--;
        }
        return true;
    }
};

My own idea is: first turn the input number into a string, here there is itoa function in C language that can be implemented, itoa is a widely used non-standard C language extension function. Since it is not a standard C language function , it cannot be used in all compilers. The itoa() function has 3 parameters: the first parameter is the number to be converted, the second parameter is the target string to which the conversion result is to be written, and the third parameter is the base used to transfer the number, indicating the base. But it is generally not available in OJ, so I chose another conversion method. I traverse each digit of the number from the lower digit (remainder operation), turn it into a character, put it into a new character array, and record the total How many bits are there, and then judge whether they are equal.

When writing a program, the problems that need to be paid attention to are: (1) If it is a negative number, then it must not be a palindrome (2) The array storing each bit must be initialized, otherwise the unused part will be a random number, which is not good. Judgment (3) Special circumstances should be considered, such as numbers with only one digit, such as 0, 1. The beginning and the end are equal from the beginning, so it can be directly judged that it is a palindrome number

Good program example:

class Solution {
public:
   bool isPalindrome(int x) {
        if(x < 0){
            return false;
        }
        int y = x;
        int cmp = 0;
        do{
            cmp *= 10;
            cmp += y % 10;
            and /= 10 ;
        }while(y != 0);
        
        return cmp == x;
    }
};

The advantage of this program is that it does not use auxiliary space, does not convert numbers into strings to operate, and only uses digital operations to judge. The principle is (1) if it is a negative number, it must not be a palindrome (2) a palindrome number, Turning the ones digit into the highest digit backwards is equal. There is no need to consider the special case of only one.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324855597&siteId=291194637