Single Number

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?

题意:一个整数数组中,除了一个数以外的其他数字都出现了两次,求这个只出现了一次的数。

要求:算法必须线性的时间复杂度,并且不需要额外的空间。

思路:主要利用异或的性质。交换性(a^b)^c=a^(b^c),以及a^0=a等

实现:

  

 class Solution:
# @param {integer[]} nums
# @return {integer}
def singleNumber(self, nums):
result = nums[0]
for i in range(1, len(nums)):
result ^= nums[i]
return result

SingleNumber python实现的更多相关文章

  1. [LeetCode]题解(python):137-Single Number II

    题目来源: https://leetcode.com/problems/single-number-ii/ 题意分析: 给定一个数组,数组里面每一个数字都出现了3次除了一个,找出那个数.要求时间复杂度 ...

  2. [LeetCode]题解(python):136-Single Number

    题目来源: https://leetcode.com/problems/single-number/ 题意分析: 给定一个数组,每个数都出现了2次,只有一个出现了一次,找出这个数.要求时间复杂度O(n ...

  3. 【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree

    今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...

  4. LeetCode Python 位操作 1

    Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 & ...

  5. Python 1.1数字与字符基础

    一. 基础数字操作 1.加减乘除以及内置函数: min(),  max(),  sum(),  abs(),  len()         math库: math.pi math.e, math.si ...

  6. LeetCode初级算法的Python实现--数组

    LeetCode初级算法的Python实现--数组 # -*- coding: utf-8 -*- """ @Created on 2018/6/3 17:06 @aut ...

  7. Leetcode with Python

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  8. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  9. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

随机推荐

  1. Mschart应用之曲线图表spline

    本文主要是Mschart应用之曲线图表spline,实现6个模拟数据的图表,其中数据源X轴为当前系统时间,Y轴是由随机函数产生的不同范围的随机数. 首先是自定义一个数据表,然后产生的数据添加到该数据表 ...

  2. 8QQ消息框

    1 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...

  3. document_createElement

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  4. dpkg, APT, aptitude常用命令

    Install dpkg --install, -i [deb] apt-get install [package] aptitude install [package] Remove dpkg -- ...

  5. ajax.request函数使用详解

    Ajax.Request   ? Ajax.Request( url, { method:method, parameters:para, postBody:xmlString, asynchrono ...

  6. DataGridview 填写数字

    private DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit        private void dgv ...

  7. 14-利用SVD简化数据

    参考:http://blog.csdn.net/geekmanong/article/details/50494936 http://www.2cto.com/kf/201503/383087.htm ...

  8. 可爱的 Python : Python中函数式编程,第二部分

    英文原文:Charming Python: Functional programming in Python, Part 2,翻译:开源中国 摘要:  本专栏继续让David对Python中的函数式编 ...

  9. POJ 3481 Double Queue STLmap和set新学到的一点用法

    2013-08-08 POJ 3481  Double Queue 这个题应该是STL里较简单的吧,用平衡二叉树也可以做,但是自己掌握不够- -,开始想用两个优先队列,一个从大到小,一个从小到大,可是 ...

  10. 系统学习Linux的11点建议

    一.从基础开始 常常有些朋友在 Linux 论坛问一些问题,不过,其中大多数的问题都是很基础的.例如为什么我使用一个命令的时候,系统告诉我找不到该目录,我要如何限制使用者的权限等问题,这些问题其实都不 ...