Given an array of integers, every element appears twice except for one. Find that single one.

本题利用XOR的特性, X^0 = X, X^X = 0, 并且XOR满足交换律。

 class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
s = 0
for x in nums:
s= s^x return s

single number II/III可以用位操作。用Hash table也可以通过OJ

class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dict = {}
for i in range(len(nums)):
if nums[i] not in dict:
dict[nums[i]] = 1
else:
dict[nums[i]] += 1 for word in dict:
if dict[word] == 1:
return word

Leetcode 137. Single Number I/II/III的更多相关文章

  1. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  2. LeetCode 【Single Number I II III】

    Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...

  3. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  4. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  5. Leetcode 137 Single Number II 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...

  6. LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

    翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...

  7. [LeetCode] 137. Single Number II 单独数 II

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  8. [LeetCode] 137. Single Number II 单独的数字之二

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  9. 详解LeetCode 137. Single Number II

    Given an array of integers, every element appears three times except for one, which appears exactly ...

随机推荐

  1. Js倒计时程序

    Js倒计时程序 点击下载

  2. StringBuffer and StringBuilder

    As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a ...

  3. PAT 1012. 数字分类 (20)

    给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...: ...

  4. 【转】Sql Server参数化查询之where in和like实现详解

    转载至:http://www.cnblogs.com/lzrabbit/archive/2012/04/22/2465313.html 文章导读 拼SQL实现where in查询 使用CHARINDE ...

  5. Ros学习注意点

    编译问题 回调函数不能有返回类型,严格按照实例程序编写 第三方库的问题,packet.xml里面必须加上自己的依赖文件 之前文档里面介绍的有点问题. 主要表现在:当你建立包的时候就写入了依赖,那就不需 ...

  6. Fragment生命周期

  7. Android -- ActivityLifeCycleCallbacks

    ActivityLifeCycleCallbacks Application通过此接口提供了一套回调方法,用于让开发者对Activity的生命周期事件进行集中处理. 为什么用ActivityLifec ...

  8. Spire.Doc组件读取与写入Word

    之前写了一篇开源组件DocX读写word的文章,当时时间比较匆忙选了这个组件,使用过程中还是有些不便,不能提前定义好模版,插入Form表单域进行替换.最近无意中发现Spire.Doc组件功能很强大,目 ...

  9. Theano2.1.3-基础知识之更多的例子

    来自:http://deeplearning.net/software/theano/tutorial/examples.html More Examples 现在,是时候开始系统的熟悉theano的 ...

  10. CI(CodeIgniter)框架入门教程——第二课 初始MVC

    本文转载自:http://www.softeng.cn/?p=53 今天的主要内容是,使用CodeIgniter框架完整的MVC内容来做一个简单的计算器,通过这个计算器,让大家能够体会到我在第一节课中 ...