SingleNumber python实现
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实现的更多相关文章
- [LeetCode]题解(python):137-Single Number II
题目来源: https://leetcode.com/problems/single-number-ii/ 题意分析: 给定一个数组,数组里面每一个数字都出现了3次除了一个,找出那个数.要求时间复杂度 ...
- [LeetCode]题解(python):136-Single Number
题目来源: https://leetcode.com/problems/single-number/ 题意分析: 给定一个数组,每个数都出现了2次,只有一个出现了一次,找出这个数.要求时间复杂度O(n ...
- 【LeetCode】【Python题解】Single Number & Maximum Depth of Binary Tree
今天做了三道LeetCode上的简单题目,每道题都是用c++和Python两种语言写的.由于c++版的代码网上比較多.所以就仅仅分享一下Python的代码吧,刚学完Python的基本的语法,做做Lee ...
- LeetCode Python 位操作 1
Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 & ...
- Python 1.1数字与字符基础
一. 基础数字操作 1.加减乘除以及内置函数: min(), max(), sum(), abs(), len() math库: math.pi math.e, math.si ...
- LeetCode初级算法的Python实现--数组
LeetCode初级算法的Python实现--数组 # -*- coding: utf-8 -*- """ @Created on 2018/6/3 17:06 @aut ...
- Leetcode with Python
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
随机推荐
- C# 1作业 2广场砖面积 护栏长度
作业1输入圆柱体的底面半径和高求体积 static void Main(string[] args) { //输入圆柱体的底面半径, ...
- hdu1358Period
Problem Description For each prefix of a given string S with N characters (each character has an ASC ...
- 安装MYSQL出现的问题
安装MYSQL接近三天了还是没有安装好,原因1:我觉得错误日志产生的原因是因为 计算机的名称是中文名 :小石头 瓦卡卡,果然是这个原因,折腾了三四天,最后换了系统!!!!把计算机名命名成了英文,果然一 ...
- 表达式求值(河南省第四届ACM试题-C题)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定指定的一个由3种函数组成的表达式,计算其数值. [题目分析] 一开始以为是后缀表达式,后来抽了没想出来,最后用了递归的方法解 ...
- yum node.js
很久之前安装过windows下以及Mac下的node,感觉还是很方便的,不成想今天安装linux下的坑了老半天,特此记录. 首先去官网下载代码,这里一定要注意安装分两种,一种是Source Code源 ...
- js,jQuery数组常用操作小结
一.js中数组常用操作小结 (1) shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift() ...
- Flink Program Guide (7) -- 容错 Fault Tolerance(DataStream API编程指导 -- For Java)
false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-n ...
- 【自学php】第三天 - 读写文件
这次的例子是把订单的数据保存起来,一般是用数据库来进行数据的存储最好,但是今天目的是为了学习读写文件,所以这次把数据存在文件里. 读写文件有一般有三个步骤: 1)打开文件.如果文件不存在,需要先创建它 ...
- .net c# 正则表达式 平衡组/递归匹配
原文 http://www.cnblogs.com/qiantuwuliang/archive/2011/06/11/2078482.html 平衡组/递归匹配 这里介绍的平衡组语法是由.Net Fr ...
- php set env
SYSTEM WARNING: 'date_default_timezone_get(): It is not safe to rely on the system's timezone settin ...