本文是在学习中的总结,欢迎转载但请注明出处: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. java.io.FileNotFoundException: D:\Program%20Files\Apache%20Software%20Foundation\Tomcat%205.0\webapp

    慢慢把以前遇到过的问题一点点发出来,以前做的笔记比较杂: java.io.FileNotFoundException: D:\Program%20Files\Apache%20Software%20F ...

  2. 剑指Offer——记中国银行体检之旅

    剑指Offer--记中国银行体检之旅   11.23完成中国银行面试,当日回到学校.当天晚上8:39收到体检通知,自己真是又气又高兴啊.气的是自己刚从北京回来,接着又要去一次.高兴的是自己通过了面试. ...

  3. activty栈管理

    题外话:我们有时在开发中,通常会有如下的需求:屏幕1-->屏幕2-->屏幕3-->屏幕4...,现在需要直接从屏幕4-->屏幕1,很多人会想到对activity进行管理得到对应 ...

  4. Shell脚本生成网页版相册浏览器

    今天学到了一招,那就是使用脚本制作一款网页版相册浏览器.先上图吧. 必备基础 操作系统: 以linux为内核的操作系统都行 编程语言:Shell(bash)脚本,相关基础知识即可 下载工具:wget ...

  5. 19 子线程刷新UI runOnUiThread

    package com.example.com.fmyh; import java.io.BufferedReader; import java.io.File; import java.io.Fil ...

  6. 自守数算法----C语言实现

    #include <stdio.h> //自守数算法 //ep : 25 ^ 2 = 625 76 ^ 2 = 5776 9376 ^ 2 = 87909376 /*ep : * 376 ...

  7. 1076. Forwards on Weibo (30) - 记录层的BFS改进

    题目如下: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, a ...

  8. 关于weak

    #define DECLARE_WEAK_SELF __typeof(&*self) __weak weakSelf = self #define DECLARE_STRONG_SELF __ ...

  9. Tomcat集群如何同步会话

    Tocmat集群中最重要的交换信息就是会话消息,对某个tomcat实例某会话做的更改要同步到集群其他tomcat实例的该会话对象,这样才能保证集群所有实例的会话数据一致.在tribes组件的基础上完成 ...

  10. 怎样在Ubuntu 14.04中搭建gitolite git服务器

     1.   首先这里我们安装openssh-serveropenssh-client,如果你用的是VPS之类的一般都默认安装好了,不过运行一个这个命令不会有错的,如果有安装就会提示已安装. sud ...