首页 > 其他 > 详细

27.leetcode136_single_number

时间:2018-02-13 22:01:52      阅读:203      评论:0      收藏:0      [点我收藏+]

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

 

27.leetcode136_single_number

原文:https://www.cnblogs.com/19991201xiao/p/8447450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!