Leetcode_num1_Single Number
好久没有做题啦。从今天開始刷Leetcode的题。希望坚持的时间能长一点。
先从ac率最高的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?
刚開始的代码是介样的:
def singleNumber(A):
rs=0
for i in range(len(A)):
if A[i] not in A[0:i]+A[i+1:]:
rs=A[i]
return A[i]
python中推断是否在数组的in 事实上也是o(n)的复杂度。所以总体算法复杂度是o(n^2)
为了达到o(n)的复杂度。必定不能使用两两比較的方法,仅仅能遍历一次数组,从整型位操作的角度出发,将数组中全部的数进行异或,同样的数异或得零。能AC的代码是介样的:
class Solution:
# @param A, a list of integer
# @return an integer
def singleNumber(self, A):
rs=0
for i in A:
rs=rs^i
return rs
又长见识了。感动得流泪了
Leetcode_num1_Single Number的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
随机推荐
- muduo总结
总结说的有的过大,算是对自己学习的一个总结.兴许会不断补充. 模型总结 muduo是基于非堵塞的IO和事件驱动的网络库. muduo的总体结构时one loop per thread+threadpo ...
- android setCookie 免登录
CookieSyncManager.createInstance(getActivity()); CookieManager cookieManager = CookieManager.getInst ...
- Methods Collection of Enumerating Com Port in Windows, by C
According to this stack overflow thread, PJ Naughter has implemented 9 methods to emunerate com port ...
- 错误 'Cannot run program "/home/uv/IDE/adt/sdk/platform-tools/adb": error=2, No such file or directory
转 Linux下Android SDK中adb找不到的解决方案 2013年04月22日 20:41:48 阅读数:7621 在Linux平台下配置Android SDK开发环境过程中,Eclipse会 ...
- qq邮箱的SMTP服务器是什么
qq邮箱的SMTP服务器是什么 QQ邮箱POP3 和 SMTP 服务器地址设置如下:POP3服务器地址为“pop.qq.com”,SMTP服务器地址为“smtp.qq.com”注:1.SMTP服务器需 ...
- checkbox的使用总结
1 checkbox如何选中时显示内容,不被选中时隐藏内容 <!DOCTYPE html> <html> <head> <meta name="vi ...
- linux 安装redis zookeeper
安装redis: http://www.redis.cn/download.html 安装的前提条件: 需要安装gcc:yum install gcc-c++ wget http://download ...
- Tomcat配置自签名https
从JDK中找到keytool.exe,随便复制到一个方便的目录,在命令行中进入这个目录. 第一步:为服务器生成证书 tomcat.keystore,命令中如果是IP方式访问用-ext SAN=ip:1 ...
- 使用 Travis-CI 的五个理由
I use the service of travis-ci now for a year. In that time the continuous integration has often poi ...
- Navicat for Mysql 关于1130错误,无法正常方法解决的解决办法。
本人因为失误操作,不小心将mysql 玩崩了.导致一直报1130错误,无法进入. 看了很多网上的帖子,但是那些办法都行不通.最后通过好友的指点,解决了这个问题. 1.停止MySQL服务,执行net ...