问题:

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. struts2类型转换器、 类型转换错误 以及INPUT view

    1.1.1    Struts2中的类型转换器 Struts2内置了常见数据类型多种转换器 boolean 和 Boolean char和 Character int 和 Integer long 和 ...

  2. yum安装所需要的开发库

    yum groupinstall "Development tools" -y yum install zlib-devel bzip2-devel openssl-devel n ...

  3. Appium+Robotframework实现Android应用的自动化测试-6:一个简单的例子

    万事具备,只欠编码! 下面看一个简单的示例,这个示例验证Android手机自带的通讯录的添加联系人的操作是否成功.这个例子是Appium官网自带的示例,有兴趣的同学也可以自己下载来研究和学习,下载地址 ...

  4. cxLookupComboBox 控件

    cxLookupComboBox cxLookupComboBox1.Properties.ListSource        //显示数据源     dtsTmnList cxLookupCombo ...

  5. perform

    public class ArgsTest { private List <Object> args; private ArgsTestCheckPoint checkPoint; pub ...

  6. percona-toolkit 之 【pt-deadlock-logger】说明

    摘要: 死锁:是指两个或则多个事务在同一个资源上相互占用,并请求锁定对方占用的资源,而导致恶性循环的现象:当产生死锁的时候,MySQL会回滚一个小事务的SQL,确保另一个完成.上面是死锁的概念,而在M ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. 使用a标签删除进行提示

    一句话搞定: <a href="Login.aspx" target="mainFrame" class="STYLE4" oncli ...

  9. 模拟操作网页 webBrowser

    C# 获取IFrame中body元素 (winform) 方法1. 找出iframe的b.html的src , 利用webbrowser去加载b.html HtmlElementCollection ...

  10. September 27th 2016 Week 40th Tuesday

    Friends are lost by calling too often and calling seldom. 交往过密过疏,都会失去朋友. Please mind your own busine ...