本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  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. ZooKeeper之(一)ZooKeeper是什么

    1.1 产生背景 当今是个分布式.集群.云计算等名词满天飞的时代.造成这种局面的一个重要因素就是,单一机器的处理能力已经不能满足我们的需求,不得不采用由多台机器组成的服务集群.服务集群对外提供服务的过 ...

  2. 硬件模块化机器人操作系统 Hardware Robot Operating System (H-ROS)

    原文网址:http://www.ros.org/news/2016/10/hardware-robot-operating-system-h-ros.html 推荐网址:https://h-ros.c ...

  3. SSH网上商城---用户激活

    在前面的博客中,小编主要结合SSH网上商城这个项目,简单的介绍了如何实现邮件发送的这个功能,邮件发送了,接下来就是激活了,为什么呢?现在大多网站都要通过对账号进行激活,然后才能注册成功,这是防止恶性注 ...

  4. 6.4、Android Studio的GPU Monitor

    Android Monitor包含GPU Monitor,它将可视化的显示渲染窗体的时间.GPU Monitor可以帮助你: 1. 迅速查看UI窗体生成 2. 辨别是否渲染管道超出使用线程时间 在GP ...

  5. TCP的发送系列 — tcp_sendmsg()的实现(二)

    主要内容:Socket发送函数在TCP层的实现 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 在上篇blog中分析了tcp_sendmsg()这个主要函 ...

  6. [ExtJS5学习笔记]第十二节 Extjs5开发遇到的问题列表记录

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38975633 本文作者:sushengmiyan ------------------ ...

  7. UNIX网络编程——利用ARP和ICMP协议解释ping命令

    一.MTU 以太网和IEEE 802.3对数据帧的长度都有限制,其最大值分别是1500和1492字节,将这个限制称作最大传输单元(MTU,Maximum Transmission Unit)      ...

  8. 与MP3相关的技术总结

    MP3文件格式解析 Peter Lee 2008-06-05 目录 33 B7 00 0001 02 FF FF FF F4 E1 2F FF FF FFFF DF FF FF FB52 8C 12 ...

  9. 1068. Find More Coins (30)

    题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...

  10. JEECG&Dubbo Demo

    jeecg框架service与dao层类图 吴水成Dubbo Demo项目架构图