Skip to content

136. 只出现一次的数字 #83

@Geekhyt

Description

@Geekhyt

原题链接

位运算

  1. 一个数和 0 做异或运算等于其本身
  2. 一个数和它本身做异或运算等于 0
  3. 异或运算满足交换律和结合律

数组中所有元素的异或运算结果就是数组中只出现一次的数字。

const singleNumber = function(nums) {
    let ans = 0
    for (const num of nums) {
        ans ^= num
    }
    return ans
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy