当前位置:网站首页>Leetcode-9: palindromes

Leetcode-9: palindromes

2022-07-05 06:09:00 Chrysanthemum headed bat

subject

Topic linking

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 .

Example 1:

 Input :x = 121
 Output :true

Example 2:

 Input :x = -121
 Output :false
 explain : Read left to right ,  by  -121 .  Read right to left ,  by  121- . So it's not a palindrome number .

Example 3:

 Input :x = 10
 Output :false
 explain : Read right to left ,  by  01 . So it's not a palindrome number .

Problem solving

Method 1 : Double pointer

class Solution {
    
public:
    bool isPalindrome(int x) {
    
        string s=to_string(x);
        int left=0,right=s.size()-1;
        while(left<right){
    
            if(s[left]!=s[right]) return false;
            left++;
            right--;
        }
        return true;
    }
};
原网站

版权声明
本文为[Chrysanthemum headed bat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050546085145.html