LeetCode编程练习 - Single Number学习心得

题目:

       Given an array of integers, every element appearstwice 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学习心得LeetCode编程练习 - Single Number学习心得


    查看解决方案,解决方案中用到了XOR(异或)运算,n表示数组中的元素,0^N=N,N^N=0,N1^N1^N2^……^Nx^Nx^N=(N1^N1)^(N2^N2)^……^^N(Nx ^ Nx)= 0^0^……^0^N=N,把相同项消去。我的思路应该算是对的,但是我没有考虑到指定的数与所有的元素作对比,只是实现了前后元素的比较,解决方案中在定义变量的时候就已经定义了条件。

    LeetCode编程练习 - Single Number学习心得 LeetCode编程练习 - Single Number学习心得


    显示结果:

LeetCode编程练习 - Single Number学习心得