Leetcode_136_Single Number
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42713315
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?
思路:
(1)题意为给定一个整形数组,其中数组中除了一个元素之外,其它任意元素都出现两次,求只出现一次的元素。
(2)由于题目限制了时间复杂度为线性的,即不能出现多次for循环,且建议最好不要申请额外的空间。这样,我们就需要思考,如何在遍历数组一次的情况下找出出现一次的元素。考虑到能否想办法把相同的元素都消除掉,这里我们就需要运用不常见的特殊运算符“^”— 按位异或运算。我们知道异或运算相同的位会消除,例4^4=(二进制)10^(二进制)10=(二进制)00,这样就消除了相同的数字。即使数组中相同数字是非连续的,根据加法的交换律,能够得到同样的结果。
(3)希望本文对你有所帮助。
算法代码实现如下:
/**
* @author liqq
*/
public static int singleNumber(int[] A) {
if (A.length == 0)
return A[0];
int x = A[0];
for (int i = 1; i < A.length; i++) {
x = x ^ A[i];
}
return x;
}
Leetcode_136_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 ...
随机推荐
- Web Worker Best Practices
使用Web Worker可以把一些比较计算量相对大的阻塞浏览器响应的计算放在单独的线程里计算. 请求优化 构造Worker的时候需要给定js的链接URL,worker内部请求js运行代码.假如work ...
- Switch控件详解
Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...
- 从Stage角度看cassandra write
声明 文章发布于CSDN cassandra concurrent 具体实现 cassandra并发技术文中介绍了java的concurrent实现,这里介绍cassandra如何基于java实现ca ...
- linux 防火墙操作
root/12345 (只能用ROOT操作)iptables -I INPUT -s x.x.x.x -p tcp --dport 8091 -j ACCEPT #允许x.x.x.x访问本机的80 ...
- Oracle常用语句语法汇总
第一篇 基本操作 --解锁用户 alter user 用户 account unlock; --锁定用户 alter user 用户 account lock; alter user sco ...
- eclipse properties 插件
eclipse properties 插件安装,分享牛,分享牛原创.eclipse properties 编辑器使用. eclipse因为是原生的,可能集成的插件不多,需要自己手动安装.eclipse ...
- MongoDb 用 mapreduce 统计留存率
MongoDb 用 mapreduce 统计留存率(金庆的专栏)留存的定义采用的是新增账号第X日:某日新增的账号中,在新增日后第X日有登录行为记为留存 输出如下:(类同友盟的留存率显示)留存用户注册时 ...
- Leetcode解题-链表(2.2.6)RotateList
1 题目:Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Fo ...
- PHP学习(3)—在HTML中嵌入PHP
我们以一个提交订单和显示订单信息的例子为学习PHP的开始.这个例子包含两个文件.一个提交订单的html文件:orderform.html,一个显示订单信息的php文件:processorder.php ...
- 从一个简洁的进度刻度绘制中了解自定义View的思路流程
先看效果(原谅我的渣像素),进度的刻度.宽度.颜色可以随意设定: [项目github地址: https://github.com/zhangke3016/CircleLoading] 实现起来并不难, ...