LeetCode Single Number

来源:互联网 发布:二木淘宝店 编辑:程序博客网 时间:2024/06/06 04:45

LeetCode解题之Single Number


原题

一个数组中除了一个数字出现过一次外,其余的数字都出现了两次,找出那个只出现一次的数字。

注意点:

  • 算法时间杂度要求为O(n)
  • 空间复杂度为O(1)

例子:

输入: nums = [1, 2, 3, 4, 3, 2, 1]

输出: 4

解题思路

非常常见的一道算法题,将所有数字进行异或操作即可。对于异或操作明确以下三点:

  • 一个整数与自己异或的结果是0
  • 一个整数与0异或的结果是自己
  • 异或操作满足交换律,即a^b=b^a

所以对所有数字进行异或操作后剩下的就是那个只出现一次的数字。

AC源码

class Solution(object):    def singleNumber(self, nums):        """        :type nums: List[int]        :rtype: int        """        result = nums[0]        for i in nums[1:]:            result ^= i        return resultif __name__ == "__main__":    assert Solution().singleNumber([1, 2, 3, 4, 3, 2, 1]) == 4

欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。

0 0
原创粉丝点击