题目:

       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学习心得

相关文章:

  • 2021-06-25
  • 2021-08-04
  • 2021-11-16
  • 2021-10-03
  • 2021-04-06
  • 2021-12-11
  • 2021-06-26
  • 2021-04-24
猜你喜欢
  • 2021-09-27
  • 2021-06-15
  • 2021-09-07
  • 2021-09-26
  • 2021-05-13
  • 2021-06-05
  • 2021-11-12
相关资源
相似解决方案