leetcode9 number of palindrome

Yep

Observe the input is int X

Illustrate the range

And there are positive and negative

 Special circumstances and special anti-overflow can be sentenced

Negative, xxx0 not be a palindrome.

0 is a palindrome

Thinking

First think itoa char * and then turn the head and tail contrast. But there is time and space overhead.

If you are interested int mod then take first place but the head is not good to take.

It starts directly from the rear, half of the inversion after taking the x, compare the first half.

What determines how spicy to half of Austria

Can engage in a general table determination first determines the number of bits x:

final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
                                      99999999, 999999999, Integer.MAX_VALUE };

// Requires positive x
static int stringSize(int x) {
    for (int i=0; ; i++)
        if (x <= sizeTable[i])
            return i+1;
}

Here's a simple process

while (rest> tail) can be, because the middle digit to reach one more

After this need only consider the tail a few odd bits

When even bits, tail two will be more than the rest (if not a palindrome)

class Solution {
public:
    bool isPalindrome(int x) {
        int tail=0;
        if(x==0)
            return true;
        if(x<0||(x%10==0))
            return false;
        
        while(x > tail) {
            tail = tail * 10 + x % 10;
            x /= 10;
        }
        //if()
        return x == tail || x == tail/10;  
    }
};

 

Guess you like

Origin www.cnblogs.com/lqerio/p/11746285.html