当前位置:   article > 正文

Leetcode 136. Single Number

Leetcode 136. Single Number

Problem

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

Algorithm

XOR. (Sort the array is more faster?)

Code

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        ans = 0
        sLen = len(nums)
        for i in range(sLen):
            ans ^= nums[i]
        
        return ans
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/493418
推荐阅读
相关标签
  

闽ICP备14008679号