Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Geekhyt opened this issue Sep 20, 2021 · 0 comments
Open

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

Geekhyt opened this issue Sep 20, 2021 · 0 comments
Labels

Comments

@Geekhyt
Copy link
Owner

Geekhyt commented Sep 20, 2021

原题链接

位运算

  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)
@Geekhyt Geekhyt added the 简单 label Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant