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- ...
随机推荐
- java中获取类加载路径和项目根路径的5种方法
import java.io.File; import java.io.IOException; import java.net.URL; public class MyUrlDemo { publi ...
- 在.NET中使用iTextSharp创建/读取PDF报告: Part I [翻译]
原文地址:Create/Read Advance PDF Report using iTextSharp in C# .NET: Part I By Debopam Pal, 27 Nov 20 ...
- html css float left与 float right的使用说明
点评: CSS中很多时候会用到浮动来布局,也就是经常见到的float:left或者float:right,简单点来说,前者是左浮动(往左侧向前边的非浮动元素飘,全是飘得元素的话,就按照流式来浮动从左到 ...
- jquery append 动态添加的元素事件on 不起作用的解决方案
用jquery添加新元素很容易,面对jquery append 动态添加的元素事件on 不起作用我们该如何解决呢?on方法中要先找到原选择器(如例.info),再找到动态添加的选择器(如列.delet ...
- bootstrap简章
系统整理一遍bootstrap 的东西 1/ 设置页面为H5文档类型 <!DOCTYPE html> <html lang="zh-CN"> ... &l ...
- mysql_config_editor程序的用法
1.mysql_config_editor程序的作用: 它只是用来在用户的家目录下生成一个.mylogin.cnf 里面保存有用于登录mysql-server端的password,host,user信 ...
- python--执行文件的绝对路径
1.__file__属性对应文件名. 2.os.path.realpath(__file__)这样就可以得到文件的绝对路径.
- secedit
secedit /export /cfg mytemplate.inf /log mylog.txt http://www.oracle-base.com/dba/script.php?categor ...
- 为Delphi程序增加UAC功能
相关资料:http://bbs.csdn.net/topics/320071356# 操作方法: 在Source\VCL目录下应该有这样两个文件sample.manifest和WindowsXP.rc ...
- android NDK jni下的c文件 Unresolved inclusion
原因是在eclipse编辑环境中没有找到对应的include中的文件.解决方法是将包含该文件的include目录作为新的linked folder加入工程中.具体方法如下:1. 右击工程->Ne ...