Leetcode: 9. Number of palindrome

topic:

Given an integer x, return true if x is a palindromic integer; otherwise, return false.
A palindromic number is an integer that reads the same in forward order (from left to right) and in reverse order (from right to left). For example, 121 is a palindrome, but 123 is not.

Example 2:
Input: x = -121
Output: false
Explanation: Read from left to right, it is -121. Read from right to left, it is 121-. Therefore it is not a palindromic number.

Solution: This question is similar to the integer inversion of question 7, it is relatively simple, so I won’t go into details

class Solution {
    
    
    public boolean isPalindrome(int x) {
    
    
        int sum = x, y = 0;
        if(x < 0){
    
    
            return false;
        }
        while(sum != 0){
    
    
            int temp =   sum % 10;
            y = y * 10 + temp;
            sum =  sum / 10;
        }
        if(y == x){
    
    
            return true;
        }
         return false;
    }
}

Guess you like

Origin blog.csdn.net/qq_40436854/article/details/120472953