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 ...
随机推荐
- Hdu oj 1017 A Mathematical Curiosity
题目:pid=1017">点击打开链接 #include<stdio.h> int main() { int t; scanf("%d",&t) ...
- Mysql第四天 数据库设计
不考虑主备.集群等方案,基于业务上的设计主要是表结构及表间关系的设计. 而关于表中字段主要是依据业务来进行定义,我们能够指定的大概有这么几项: 存储引擎 一般用InnoDB,特殊需求特殊选用 字符集和 ...
- 2015.04.15,外语,读书笔记-《Word Power Made Easy》 10 “如何讨论交谈习惯” SESSION 27
继续学习交谈习惯的单词,本大章节完成. 1. front and back - and uncles ventriloquist,从belly发声(venter, venris + loquor). ...
- linux下的静态库创建与查看,及如何查看某个可执行依赖于哪些动态库
linux下的静态库创建与查看,及如何查看某个可执行依赖于哪些动态库 创建静态库:ar -rcs test.a *.o查看静态库:ar -tv test.a解压静态库:ar -x test.a 查 ...
- struts2-action中使用通配符
转自:https://www.cnblogs.com/ningvsban/p/3734574.html 在以前的学习中,<action>元素的配置,都是用明确的配置,其name.class ...
- 运算符 and or ont
运算符 逻辑运算: and 并且的意思. 左右两端的值必须都是真. 运算结果才是真 or 或者的意思. 左右两端有一个是真的. 结果就是真. 全部是假. 结果才能是假 not 非的意思. 原来是假. ...
- 1.Win32基本程序概念
还没学会走之前,不要跑! Message based , event driven 每个程序都应该有一个如下的循环:MSG msg;while(GetMessage(&msg,NULL,NUL ...
- 8. String to Integer[M]字符串转整数
题目 Inplement atoi which converts a string to an integer. The function first discards as many whitesp ...
- POJ 3134 Power Calculus ID-DFS +剪枝
题意:给你个数n 让你求从x出发用乘除法最少多少步算出x^n. 思路: 一看数据范围 n<=1000 好了,,暴搜.. 但是 一开始写的辣鸡暴搜 样例只能过一半.. 大数据跑了10分钟才跑出来. ...
- Combox程序中自定义数据源
有combox控件,命名为cbxPutStatus,在程序中为其手工定义数据源,并绑定 private void POrderSplitFrm_Load(object sender, EventArg ...