Leetcode学习(17)—— Single Number

来源:互联网 发布:左右声道测试软件 编辑:程序博客网 时间:2024/06/18 16:33

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?

class Solution(object):    def singleNumber(self, nums):        result = 0        for i in nums:            result ^= i        return result

这里写图片描述

思路:

用 ^ 运算符依次 亦或 nums 中的每一个元素,最后得到的结果即为 single number

0 0
原创粉丝点击