6. 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 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就是我们要找的那个只出现一次的数字。

posted @ 2017-04-09 21:24  蓦然闻声  阅读(90)  评论(0编辑  收藏  举报