【每日一题1207】共同比特

:mage:t2: 已知两个非负整数x 和 y,请编写一个函数,找出它们二进制形式中,在相同位上同为1的个数。当个数大于等于2时返回True,否则返回False。

示例:
输入:7,10,返回:False。
解释:数字7的二进制形式是0111,10的二进制是1010,它们同位同为1的只在第三个位置出现1次,因此是False。

题目难度:简单
题目来源:codewars: Shared Bit Counter

def solution(x: int, y:int) -> bool:
    pass

assert solution(7, 10) is False
assert solution(7, 15) is True
assert solution(1, 2) is False
def solution(x: int, y: int) -> bool:
    return len([i for i in [i_x for i_x in range(len(bin(x))) if bin(x)[-i_x] == '1'] if
                i in [i_y for i_y in range(len(bin(x))) if bin(y)[-i_y] == '1']]) >= 2


assert solution(7, 10) is False
assert solution(7, 15) is True
assert solution(1, 2) is False
1 Like

@nobugs 属你最快,优秀!

扩展思路:有空尝试下位运算,也许有惊喜哦~

果然,有惊喜 :grin:

def solution(x: int, y: int) -> bool:
    return bin(x & y).count('1') >= 2


assert solution(7, 10) is False
assert solution(7, 15) is True
assert solution(1, 2) is False
1 Like

当个数大于等于2的时候,才返回True,而你是大于2返回True。另外我在你的逻辑中没有看到同位的概念。
举一个例子:
5 101
14 1110

1 Like

好的,已经修改了

def solution(x: int, y: int):
x_len = len(bin(x))
num = 0
for i in range(0, x_len):
binary_system_x = bin(x)[i]
binary_system_y = bin(y)[i]
if binary_system_x == binary_system_y and binary_system_x == 1 and binary_system_y == 1:
num += 1
if num >= 2:
return True
else:
return False

吼吼,可能是最菜的方式了

def solution(x: int, y:int) -> bool:
    xL=list(bin(x))[2:]
    yL=list(bin(y))[2:]
    minLen = min(len(xL),len(yL))
    compareResult = sum([1 for i in range(minLen) if xL[i] == yL[i] and xL[i] == '1'])
    return compareResult >= 2

assert solution(7, 10) is False
assert solution(7, 15) is True
assert solution(1, 2) is False
def solution(x: int, y: int) -> bool:
    countx = 0
    count = 0
    for i in str(bin(x))[::-1]:
        countx += 1
        county = 0
        if i == 'b': break
        for j in str(bin(y))[::-1]:
            county += 1
            if county == countx:
                if j == i:
                    count += 1

    if count >= 2:
        return True
    else:
        return False


if __name__ == '__main__':
    assert solution(7, 10) is False
    assert solution(7, 15) is True
    assert solution(1, 2) is False

大兄弟第一个就没通

  • 0b111

  • 0b1010
    比较的是111和010

不同位数的二进制数, & 会在位数少的二进制上补0,不会影响结果吗?

补0 与上之后 仍然是0 我觉得不会影响 :joy:如果有失败的case可以发出来看一下:thinking:

1 Like
def solution(x: int, y:int) -> bool:
    x1=str(bin(x))[2:]
    x2=str(bin(y))[2:]
    s=int(x1)+int(x2)
    if str(s).count('2')<2:
        return False
    else:
        return True

assert solution(7, 10) is False
assert solution(7, 15) is True
assert solution(1, 2) is False
def solution(x: int, y:int) -> bool:
    nor_b = str(format(x^y,'b'))
    max_b = str(format(x,'b')) if x>y else str(format(y,'b'))
    i = 0
    num = 0
    while i<len(nor_b):
        index = nor_b.find('0',i)
        if index == -1: break
        if index < len(max_b) and max_b[index] == '1':
            num += 1
        i = index+1
    return num>=2

嗯,我知道,但是我不知道 7 的二进制为啥是 0111 啊,‘0b111’ 去掉’0b’不就是 111 :sleepy:

不显示多位0的,头部的0都清空了,你做的是从头比较,111和101比了

是的,因为他们长度不一样,就比较最小长度那部分了。主要还是因为 bin(7)是 111,不是 0111

def solution1207(x: int, y: int) -> bool:
    x = bin(x).replace('0b', '')
    y = bin(y).replace('0b', '')
    length = min(len(x), len(y))
    count = 0
    for i in range(1,length+1):
        if x[-i] == '1' and y[-i] == '1':
            count += 1
        continue

    return True if count >= 2 else False


assert solution1207(7, 10) is False
assert solution1207(7, 15) is True
assert solution1207(1, 2) is False
关闭