[LeetCode]Palindrome Number 判断二进制和十进制是否为回文

class Solution {
public:
	bool isPalindrome2(int x) {//二进制
        int num=1,len=1,t=x>>1;
		while(t){
			num<<=1;
			t>>=1;
			len++;
		}
		len/=2;
		while(len--){
			if((num&x==0)&&(x&1)!=0){
				return 0;
			}
			x&=(~num);
			x>>=1;
			num>>=2;
		}
		return 1;
    }
    bool isPalindrome(int x) {//十进制
        if(x<0)return 0;
        int num=1,len=1;
        while(x/num>=10){
            num*=10;
            len++;
        }
        len/=2;
        while(len--){
            if(x%10!=x/num){
                return 0;
            }
            x=x-(x/num)*num;
            num/=100;
            x/=10;
        }
        return 1;
    }
};
时间: 2024-10-09 12:27:34

[LeetCode]Palindrome Number 判断二进制和十进制是否为回文的相关文章

9. Palindrome Number(判断整型数字是否是回文,直接暴力即可)

Determine whether an integer is a palindrome. Do this without extra space. 暴力的时候,注意奇数偶数. 1 class Solution: 2 def isPalindrome(self, x): 3 """ 4 :type x: int 5 :rtype: bool 6 """ 7 x =str(x) 8 9 if(len(x)%2!=0):#数 10 for i in

(LeetCode)Palindrome Number -- 判断回文数

Determine whether an integer is a palindrome. Do this without extra space. 解题分析: 题目很简单,但是对于要求需要看清楚. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ex

LeetCode——Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

[LeetCode] Palindrome Number [13]

题目 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r

判断输入的字符串是否是回文数

<?phpfunction yuanyincount($str){ $str_len=strlen($str); $a_count=0; $e_count=0; $i_count=0; $o_count=0; $u_count=0; $other_count=0; //五种原因字母的数组,没写输出 $a_arr=array(); $e_arr=array(); $i_arr=array(); $o_arr=array(); $u_arr=array(); $other_arr=array();

【leetcode 简单】 第九十六题 最长回文串

给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不会超过 1010. 示例 1: 输入: "abccccdd" 输出: 7 解释: 我们可以构造的最长的回文串是"dccaccd", 它的长度是 7. class Solution(object): def longestPalindrome(self, s): &quo

Leetcode 9. Palindrome Number(判断回文数字)

Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You

[leetcode] Palindrome Number(不使用额外空间)

本来判断回文串是一件很容易的事情,只需要反转字符串后在与原字符串相比较即可.这道题目明确说明不能使用额外的空间,那么使用将其分解连接成字符串的方法便不是可行的.只好采用数学的方式: 每次取最高位和最低位相比较,总的位数可以用一个while先处理出来,循环直至取余和除数相等. 具体见代码: class Solution { public: bool isPalindrome(int x) { if(x<0) //special due return false; if(x<10) return