本文是在学习中的总结,欢迎转载但请注明出处: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. Matlab—regexp正则表达式

    原文转自:http://blog.csdn.net/yf210yf/article/details/42421523 关于正则表达式的基本知识 正则表达式就是一个表达式(也是一串字符),它定义了某种字 ...

  2. FLAnimatedImage -ios gif图片加载框架介绍

    简介 FLAnimatedImage 是 Flipboard 团队开发的在它们 App 中渲染 GIF 图片使用的库. 后来 Flipboard 将 FLAnimatedImage 开源出来供大家使用 ...

  3. PHP和MySQL Web开发学习笔记介绍

    前言 从2016年2月1日开始,之后的几个月左右的时间里,我会写一个系列的PHP和MySQL Web开发的学习笔记.我之前一直从事Java语言的开发工作,最近这段时间非常想学习一门语言,就选择了PHP ...

  4. ORACLE时间日期格式使用总结(参考网上资料汇总)

    Oracle时间日期操作 sysdate+(5/24/60/60) 在系统时间基础上延迟5秒 sysdate+5/24/60 在系统时间基础上延迟5分钟 sysdate+5/24 在系统时间基础上延迟 ...

  5. springMVC源码分析--动态样式ThemeResolver(二)

    在上一篇博客springMVC源码分析--动态样式ThemeResolver(一)中我们介绍了多样式ThemeResolver的使用方法,接下来我们对源码进行简单的分析一下. ThemeResolve ...

  6. Android布局中ScrollView与ListView的冲突的最简单方法

    看到网上流行的一种使用方法是: public class Utility { public static void setListViewHeightBasedOnChildren(ListView ...

  7. 开源框架Slidingmenu的基本使用

    转载本博客请标明出处:点击打开链接      http://blog.csdn.net/qq_32059827/article/details/52464262 侧滑菜单在开发中经常用到,而Slidi ...

  8. windows下安装nginx (转载自:http://blog.163.com/njut_wangjian/blog/static/1657964252013327103716818/)

    1.  到nginx官网上下载相应的安装包,http://nginx.org/en/download.html:下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如下图 ...

  9. UE4联机烘焙

    联机烘焙就是为了利用多台电脑解决烘焙效率的问题 1.UE4的烘焙工具在安装目录下的\Engine\Binaries\DotNET,比如我这里是E:\UnrealEngine-release\Engin ...

  10. 06 Activity 4中启动模式

    前言:改变Activity的启动模式可以清单文件AndroidManifest的Activity标签添加属性android:launchMode="standard"中修改如下图: ...