leetcode笔记:Single Number 异或的使用

一.题目描述

二.解题思路

题目提到,一个数组中除了一个数只出现一次之外,其他数都出现了两次,找出这个特别的数。

这道题对时间和空间有要求,面对这种情况,一般是暗示有十分轻巧而简便的方法进行求解。在一些场景下,使用基本的逻辑运算是个不错的选择。自己简单写了一下,再参照网上部分解法,基本都是使用了异或运算(XOR),任何数与自己进行按位异或都等于0,而任何数与0进行按位异或都等于本身

在C/C++中,按位异或运算符为:“^”

基于以上规则,我们可以将整个数组的元素都按位进行异或,最终返回那个只出现一次的数了。

三.示例代码

// 时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
    int FindSingleNumber(int A[], int n)
    {
        int x = 0;
        for (size_t i = 0; i < n; ++i)
        x ^= A[i];  // XOR
        return x;
    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

四.总结

本题使用了bool运算中的异或,除此之外没有大的难点,不知是否有更高效的算法。

因为使用到了异或,结合网上相关博文的总结,在这记录一下异或运算的性质和常见用法,其实这些性质很容易验证:

一个数和自己做异或的结果是0; 
0做异或保持原值不变,和1做异或得到原值的相反值; 
如果a1^a2^a3^…^an的结果为1,则表示a1an之中1的个数为奇数,否则为偶数。这一性质可用于奇偶校验; 
x^x^y == y,推导很简单:x^x == 0,而 0^y == y。这一性质可用于在不允许使用中间变量来交换两个数的情况下,实现两个数的交换,如以下swap函数,交换a和b的值:

void swap(int &a,int &b)
{
    a ^= b;
    b ^= a;
    a ^= b;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然而,该办法并不是没有漏洞。如果输入ab的值相等,得到的结果就不是我们想要的,原因如下:

a ^= a;
a ^= a;
a ^= a;
  • 1
  • 2
  • 3

对自身异或结果自然是0了。

因此,将数值交换函数改成以下形式,可避免这种错误发生:

void swap(int &a,int &b)
{
    if(a == b) return; // 防止a,b指向同一个地址
    a ^= b;
    b ^= a;
    a ^= b;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 15:09:25

leetcode笔记:Single Number 异或的使用的相关文章

【LeetCode】Single Number

原文: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解答: 常规解法:先对数组进行排序,然后通过按顺序判断每相邻两个数是否相同即可

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

LeetCode之Single Number以及拓展

Problem 1:一个数组中有一个数字a只出现一次,其他数字都出现了两次.请找出这个只出现一次的数字? 考察知识点:异或运算 思路:比如数字 b^b = 0     a^0 = a 因此,可以将数组中的所有数字进行异或,而最终异或的结果即为所求只出现一次的数字a. 代码: 1 def SingleNumber1(Array): 2 ret = 0 3 for i in Array: 4 ret ^= i 5 return ret ================================

[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int i,j; 5 for(i=0; i<n; i++) 6 { 7 for(j=i+1; j<n; j++) 8 { 9 if(A[j]==A[i]) 10 { 11 int temp = A[i+1]; 12 A[i+1] = A[j]; 13 A[j] = temp; 14 i++; 15 break; 16 } 17 } 18 if(j==n) 19

Leetcode 137 Single Number II 仅出现一次的数字

原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element appears three times except for one. Find that single one. 给出一个整数数组,除了某个元素外所有元素都出现三次.找出仅出现一次的数字. Note: 注意: Your algorithm should have a linear runtime co

LeetCode 136 Single Number(只出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次.找出这个只出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度.你可以不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you i

LeetCode 137 Single Number II(只出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次.找出这个元素. 备注: 你的算法应该是线性时间复杂度.你可以不用额外的空间来实现它吗? 原文 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you im

[LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int bits = sizeof(int)*8; 5 int result=0; 6 for(int i=1; i<=bits; i++) 7 { 8 int w=0; 9 int t=1; 10 11 for(int j=0; j<n; j++) 12 w += (A[j]>>(i-1))&t; 13 result+= (w%3)<

LeetCode 136 Single Number(仅仅出现一次的数字)

翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could yo

LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you