27.leetcode136_single_number

1.题目描述

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?

在给定数组中除了一个元素外每个元素出现两次,找到那个孤独的元素(注意:在线性时间内解决,而且不使用额外空间)

2.题目分析

典型的位运算,异或操作,遇到相同的数结果为0

3.解题思路

 1 class Solution(object):
 2     def singleNumber(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: int
 6         """
 7         result=0  
 8         i=0
 9         while i<len(nums):
10             result ^= nums[i] #异或操作
11             i+=1
12         return result

 

posted @ 2018-02-13 21:37  vlice  阅读(92)  评论(0编辑  收藏  举报