【问题标题】:Why is 0^1 = 1 in Python? [duplicate]为什么 0^1 = 1 在 Python 中? [复制]
【发布时间】:2023-01-26 18:17:59
【问题描述】:

我正在查看 Python 中的一个问题解决方案,其中返回数组中唯一没有一对的数字。解决方案如下:

def solution(A):
    n = len(A)
    if A is None or n == 0:
        return 0
    if n == 1:
        return A[0]
    result = 0
    for i in range(0, n):
        result ^= A[i]
    return result

循环逻辑如何返回唯一编号?

【问题讨论】:

  • ^ 不是 Python 的力量。

标签: python operators


【解决方案1】:

python中的^运算符就是异或运算符 这意味着对于每个 a,b:

a^b = a'b + b'a

在真值表中它看起来像这样:

| a | b |a^b|
|:--|:--|:--|
| 0 | 0 | 0
| 0 | 1 | 1
| 1 | 0 | 1
| 1 | 1 | 0

【讨论】:

    猜你喜欢
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多