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. Axure 快捷方式

    基本快捷键: 打开:Ctrl + O新建:Ctrl + N保存:Ctrl + S退出:Alt + F4打印:Ctrl + P查找:Ctrl + F替换:Ctrl + H复制:Ctrl + C剪切:Ct ...

  2. Cocos2d-X学习之Ref类

    先看看定义该类的头文件——CCRef.h /**************************************************************************** C ...

  3. PSAM SAM

    第一个问题: 为什么要用SAM? 究竟谁最开始使用SAM这个词,已经无从考证,能够确认的是:这个世界上先有了PSAM,然后才有了SAM.由于网络状况的原因,或者是应用环境的要求,使用IC卡作为支付介质 ...

  4. OTG中的ID脚风波释疑

    1. 概要 OTG设备使用插头中的ID引脚来区分A/B Device,ID接地被称作为A-Device,充当USB Host,A-Device始终为总线提供电力,ID悬空被称作为B-Device,充当 ...

  5. CC++初学者编程教程(4) 安装Oracle12c于Windows Sever2012

    我们开启虚拟机 Windows Sever2012启动中. 3.看到WindowsSever2012的桌面. 我们解压缩两个文件, winx64_12c_database_1of2.zip,winx6 ...

  6. reids客户端 redis-cli用法

    Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用.在编程时,比如使用Redis 的Java语言包,这些命令都有对应的方法.下面将R ...

  7. denoising autoencoder

    神经网络的挑战和关键技术: 1.神经网络结构决定(层,神经元,连接)    加入特定领域的知识(CNN 图片处理) 2.模型复杂度高    大的数据量:    regularization:  dro ...

  8. AndroidUI 侧滑菜单 DrawerLayout的使用

    直接上代码: activity_main.xml: <android.support.v4.widget.DrawerLayout xmlns:android="http://sche ...

  9. Linux oracle 11g r2 安装前检查及安装

    Linux环境配置 [c-sharp] view plaincopy OS:Fedora 15 DB:Oracle 11gR2 将Oracle安装到home/oracle_11目录 配置过程:本文来自 ...

  10. iOS 开发的几种手势

    今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: // 初始化一个UIimageView UIImageView *imageView ...