LeetCode.540.Single Element in a Sorted Array

 

恩, 沙比提

 1 class Solution(object):
 2     def singleNonDuplicate(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: int
 6         """
 7         xorsum = 0
 8         for i in nums:
 9             xorsum = xorsum ^ i
10         return xorsum
11         

 

发现要logn时间, 唔, 那就是二分咯

如果mid左右和mid相等的数的下标为 "奇偶", 那就表示single在右边

相反, 如果下标为 "偶奇", 那就表示single在左边

直到mid左右没有相等的数, 就是答案

不写了

posted @ 2017-04-03 12:21  HexSix  阅读(319)  评论(0编辑  收藏  举报