好久没有做题啦。从今天開始刷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的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. 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.免费应用程序调试最 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

随机推荐

  1. POJ 1201 & HDU1384 & ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  2. https 证书 certbot-auto执行错误

    报错:ImportError: /root/.local/share/letsencrypt/lib/python2.7/site-packages/cryptography/hazmat/bindi ...

  3. Codeforces Round #338 (Div. 2) B. Longtail Hedgehog 记忆化搜索/树DP

    B. Longtail Hedgehog   This Christmas Santa gave Masha a magic picture and a pencil. The picture con ...

  4. SQL Server loop - how do I loop through a set of records

    SQL Server loop - how do I loop through a set of records By using T-SQL and cursors like this : DECL ...

  5. SYN-Flood防御方法之一Synproxy

    SYN-Flood攻击: 攻击者发送大量的SYN给服务器. 服务器必须针对每一个SYN请求回送一个SYN-ACK 应答包,此时服务器就必须保持一条半开放的连接,直到接收到一个对应的ACK应答包为止. ...

  6. linux下通用Makefile写法

    linux编译多个源文件的程序比较麻烦,这下就需要通用的Makefile了,编译的时候执行一下make命令就OK,下面介绍通用makfile的写法. 假设现在有以下源文件:file1.h file1. ...

  7. Oracle 新手语法记录

    一.用户 1. 创建用户 语法:create user 用户名 identified by 口令; create user test identified by test; 2. 修改用户 语法:al ...

  8. PHP配置优化:php-fpm配置解读

    PHP-FPM是一个PHP FastCGI管理器,php-fpm.conf配置文件用于控制PHP-FPM管理进程的相关参数,比如工作子进程的数量.运行权限.监听端口.慢请求等等. 我们在编译安装PHP ...

  9. Firefox Quantum:开发者版本 推荐

    为生民,不谋利 欢迎您使用 Firefox 开发者版本.使用此版本可获得最新功能.高速性能,以及您打造开放 Web 所需的开发工具. https://www.mozilla.org/zh-CN/fir ...

  10. Hibernate框架学习(七)——多对多关系

    一.关系表达 1.表中的表达 2.实体中的表达 3.orm元数据中的表达 在User.hbm.xml中添加: 在Role.hbm.xml中添加(与上相反): 二.操作关联属性 1.保存员工及角色 pu ...