6. leetcode 136. Single Number

mac2022-06-30  81

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?

思路:将数组元素逐个异或。

 举个栗子:2  3  4  2  3

           所有数字依次异或运算:2 xor 3 xor 4 xor 2 xor 3 = (2 xor 2) xor (3 xor 3) xor 4= 0 xor 0 xor 4 = 4

          最终结果4就是我们要找的那个只出现一次的数字。

转载于:https://www.cnblogs.com/vincent93/p/6686476.html

最新回复(0)