【每日一题20220801】交替位二进制数

题目难度:简单
题目来源:693. 交替位二进制数 - 力扣(LeetCode)

描述

:mage:‍给定一个正整数,检查它的二进制表示是否总是 0、1 交替出现:换句话说,就是二进制表示中相邻两位的数字永不相同。

编写一个函数, 完成上述操作

class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
    # your code here

示例 1:

输入:n = 5
输出:true
解释:5 的二进制表示是:101

示例 2:

输入:n = 7
输出:false
解释:7 的二进制表示是:111

示例 3:

输入:n = 11
输出:false
解释:11 的二进制表示是:1011

提示:

  • 1 <= n <= 231 - 1

面向题目编程 :smile:

class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        if(n==1):return True
        if(n==2):return True
        if(n==5):return True
        if(n==10):return True
        if(n==21):return True
        if(n==42):return True
        if(n==85):return True
        if(n==170):return True
        if(n==341):return True
        if(n==682):return True
        if(n==1365):return True
        if(n==2730):return True
        if(n==5461):return True
        if(n==10922):return True
        if(n==21845):return True
        if(n==43690):return True
        if(n==87381):return True
        if(n==174762):return True
        if(n==349525):return True
        if(n==699050):return True
        if(n==1398101):return True
        if(n==2796202):return True
        if(n==5592405):return True
        if(n==11184810):return True
        if(n==22369621):return True
        if(n==44739242):return True
        if(n==89478485):return True
        if(n==178956970):return True
        if(n==357913941):return True
        if(n==715827882):return True
        if(n==1431655765):return True
        return False

respect!

class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        # your code here
        bin_number = bin(n)[2:]
        target_number = ['01', '10']
        for i in target_number:
            if bin_number == (i * (((n := len(bin_number)) + 1) // 2))[:n]:
                return True
        return False
class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        res = bin(n)[2:]
        while res:
            if res[-1:-3:-1] == '01'or res[-1:-3:-1] == '1':
                res = res[:-1]
            elif res[-1:-3:-1] == '10':
                res = res[:-1]
            else:
                return False
        return True
def hasAlternatingBits(n: int) -> bool:
        if n in (1,2):return True
        str_n=bin(n)[2:]
        return '10'* (len(str_n)//2)==str_n or '10'*(len(str_n)//2)+'1'==str_n
        

def hasAlternatingBits( n: int) -> bool:
	if n==1:
		return True
	else:
		a=bin(n)[2:]
		a_1=a[0::2]
		a_2=a[1::2]
		return True if len(set(a_1))==1 and len(set(a_2))==1 and set(a_1)!=set(a_2) else  False
1 个赞
class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        # your code here
        bin_str = str(bin(n))[2:]
        if len(set(bin_str[::2])) == 1 and len(set(bin_str[1::2])) == 1:
            return True

        return False

obj = Solution()
assert obj.hasAlternatingBits(5) == True
assert obj.hasAlternatingBits(42) == True
assert obj.hasAlternatingBits(43) == False
assert obj.hasAlternatingBits(21) == True
assert obj.hasAlternatingBits(9) == False
assert obj.hasAlternatingBits(22) == False
assert obj.hasAlternatingBits(975473525356352354353447457) == False
def hasAlternatingBits(n: int) -> bool:
    str_n=bin(n)[2:]
    if len(set(str_n[0::2]))==1 and len(set(str_n[1::2]))==1 and set(str_n[0::2])!=set(str_n[1::2]):
        return True
    else:
        return False
assert hasAlternatingBits(5)==True
assert hasAlternatingBits(7)==False
assert hasAlternatingBits(11)==False
    public static boolean hasAlternatingBits(int it) {
        String str=Integer.toBinaryString(it);
        String[] arr=str.split("");
        for (int i = 0; i < arr.length-1; i++) {
            if(arr[i] .equals(arr[i + 1])){
               return false;
            }
        }
        return true;
    }
class Solution:
    def hasAlternatingBits(self, n: int) -> bool:
        bin_str = str(bin(n))[2:]
        while bin_str:
            if bin_str[0:2] == "10" or bin_str[0:2] == "1":
                bin_str = bin_str[2:]
            else:
                return False
        return True