JAVA-LeetCode simple 09

Palindrome number judgment

1. Title

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.

Source: LeetCode

Link: Judging the number of palindrome

2. Analysis

By storing each decimal digit of the input into an array, using the characteristics of the array, it is judged by the double pointer method until the two subscripts are equal, or the values ​​of adjacent subscripts are equal

3. Code example

 public boolean isPalindrome(int x) {
    
    
            
        int num = 0;
        int t =x;
        if (t<0){
    
    
            return false;
        }
        while(t!=0){
    
    
            t/=10;
            num+=1;
        }
        int [] tmp = new int[num];
        for (int i=0;i<=num;i++){
    
    
            if (x==0){
    
    
                break;
            }
            tmp[i]=x%10;
            x/=10;
        }
        int b =num-1;
        for (int a=0;a<num;a++){
    
    

                if(((a+1)==b && tmp[a]==tmp[b])||a==b){
    
    
                    return true;
                }else if (tmp[a]==tmp[b]){
    
    
                    b--;
                    continue;
                }else{
    
    
                    return false;
                }





        }
        return true;

    }

Guess you like

Origin blog.csdn.net/weixin_44712669/article/details/111563144
09