当前位置:网站首页>Force buckle 9 palindromes

Force buckle 9 palindromes

2022-07-06 01:25:00 zjsru_ Beginner

Give you an integer x , If x Is a palindrome integer , return true ; otherwise , return false .

Palindrome number refers to positive order ( From left to right ) Reverse order ( From right to left ) Read all the same integers . for example ,121 It's palindrome. , and 123 No .

My thoughts :

So let's define a long Variable of type y To simulate integers x In reverse order ,

( Why do I use long Type instead of int Type? ? This is because it is easy to encounter the overflow of signed integer numbers ),

Use one while loop , take x The low order of is converted to the high order , take x The high order of is converted to the low order and assigned to y, Last comparison y,x Whether the two are equal .

My code :

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

 

Digital media 202 Pan ZY

原网站

版权声明
本文为[zjsru_ Beginner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140126454723.html