码迷,mamicode.com
首页 > 其他好文 > 详细

27.leetcode136_single_number

时间:2018-02-13 22:17:26      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:tco   线性时间   使用   div   思路   line   while   ber   body   

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

标签:tco   线性时间   使用   div   思路   line   while   ber   body   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!