问题:

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?

 

分析:

数组中的数除了一个只出现了一次之外,其它都出现了两次,

要找出只出现了一次的数我们想到可以用XOR,

代码如下:

public class Solution {
public int singleNumber(int[] a) {
int result = a[0];
for(int i=1;i<a.length;i++){
result ^= a[i];
}
return result;
}
}

LeetCode——Single Number(找出数组中只出现一次的数)的更多相关文章

  1. LeetCode——Single Number II(找出数组中只出现一次的数2)

    问题: Given an array of integers, every element appears three times except for one. Find that single o ...

  2. 287. Find the Duplicate Number 找出数组中的重复数字

    [抄题]: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...

  3. 一个整型数组里除了一个数字之外,其他的数字都出现了两次。要求时间复杂度是O(n),空间复杂度是O(1),如何找出数组中只出现一次的数字

    思路分析:任何一个数字异或它自己都等于0,根据这一特性,如果从头到尾依次异或数组中的每一个数字,因为那些出现两次的数字全部在异或中抵消掉了,所以最终的结果刚好是那些只出现一次的数字. 代码如下: #i ...

  4. [LeetCode136]Single Number寻找一个数组里只出现一次的数

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  5. [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  6. 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.

    找出数组中的单身狗: 1. OddOccurrencesInArray Find value that occurs in odd number of elements. A non-empty ze ...

  7. 【Java】 剑指offer(1) 找出数组中重复的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字 ...

  8. 《剑指offer》第三_一题(找出数组中重复的数字,可改变数组)

    // 面试题3(一):找出数组中重复的数字 // 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, // 也不知道每个数字重复了几次.请 ...

  9. 一起来刷《剑指Offer》-- 题目一:找出数组中重复的数字(Python多种方法实现)

    数组中重复的数字 最近在复习算法和数据结构(基于Python实现),然后看了Python的各种"序列"--比如列表List.元组Tuple和字符串String,后期会写一篇博客介绍 ...

随机推荐

  1. static cross compile gtk-2.16.6+gtk-directfb+arm-linux (arm-linux-gcc-3.4.4+glib-2.3.5)

    ----------------------------------------------------------------------- In Ubuntu 10.4 Desktop and & ...

  2. cairo-1.14.6 static compiler msys mingw32

    gtk2.x 静态编译时 需要注意的是 cairo cairo 1.14.x 使用了 mutex , 用动态方式时 DllMain 中调用了 CAIRO_MUTEX_INITIALIZE () 在静态 ...

  3. java 入门 第三季1

    异常和异常体系 java异常体系 throwable:error:线程死锁,内存溢出 excepiton:rumtimeException运行时异常:非检查异常 检查异常:文件异常IOExceptio ...

  4. ajax 删除一条数据

    代码: 对这一段话的理解:先找到需要删除的节点,以及节点里的文本:用Ajax 发送请求,请求方式为POST ,请求内容为需要删除记录的文件,dataType定义数据类型Json,通常都是Json,da ...

  5. Class和ClassLoader的getResourceAsStream区别

    这两个方法还是略有区别的, 以前一直不加以区分,直到今天发现要写这样的代码的时候运行 错误, 才把这个问题澄清了一下. 基本上,两个都可以用于从 classpath 里面进行资源读取,  classp ...

  6. 【python】入门学习(五)

    字符串: 正索引,从0开始 和 负索引,从-1开始 >>> s = 'apple' >>> s[0] 'a' >>> s[1] 'p' >& ...

  7. jquery this 与javascript的this

    <div class="list"> <table> <thead> <tr> <th width="110&quo ...

  8. 在CMD窗口中使用javac和java命令进行编译和执行带有包名的具有继承关系的类

    一.背景 最近在使用记事本编写带有包名并且有继承关系的java代码并运行时发现出现了很多错误,经过努力一一被解决,今天我们来看一下会遇见哪些问题,并给出解决办法. 二.测试过程 1.父类代码 pack ...

  9. Centos7 设置Swap分区

    1.使用dd命令创建一个swap交换文件 dd if=/dev/zero of=/home/swap bs=1024 count=1024000 2.制作为swap格式文件: mkswap /home ...

  10. perl检查变量是否定义

    my $label = defined($pieces[0]) ? $pieces[0] : ""; my @alreadyAddedCol = $node1->{DB}-& ...