问题:

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. MDI窗体

    1.设置父窗体 使用MDI窗体,需要先将父窗体的IsMdiContainer属性设置为True 2.生成用于MDI子窗体的窗体 1 frmTemp f1 = new frmTemp(); f1.Tex ...

  2. POJ 2051

    http://poj.org/problem?id=2051 这个题目的大题意思就是给你一些ID,和ID所对应的周期,每隔它所对应的周期,它的任务就会执行,就会输出所对应的ID Register 20 ...

  3. MongoDB 数据库管理(不定时更新)

    之前的几篇文章大致说了副本集的搭建.副本集的管理,现在说下MongoDB数据库的管理.数据库管理包括:备份.还原.导入.导出.服务器管理等. 一:查看服务器状态,查看命令行参数.db.serverSt ...

  4. Java for LeetCode 208 Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

  5. Django~Views

    In Django, web pages and other content are delivered by views. To get from a URL to a view, Django u ...

  6. javascript中元素的scrollLeft和scrollTop属性说明

    再说意义之前,前说一下这两个属性的适用范围: 注意:这两个属性只能用于元素设置了overflow的css样式中.否者这两个属性没有任何意义.且overflow的值不能为visible,但可以为hidd ...

  7. jquery的基本事件大全

    ].name); });jQuery.getScript( url, [callback] ) 使用GET请求javascript文件并执行. $.getScript(”test.js”, funct ...

  8. HTML标记之a标签

    一.a标签的语法 <a href=”链接目标地址” title=”注释” target=”打开链接窗口的形式”>链接显示内容</a> target值: _blank在新窗口中打 ...

  9. python基础——使用元类

    python基础——使用元类 type() 动态语言和静态语言最大的不同,就是函数和类的定义,不是编译时定义的,而是运行时动态创建的. 比方说我们要定义一个Hello的class,就写一个hello. ...

  10. Mac怎么读写NTFS格式?Mac读写NTFS格式硬盘教程

    我们都知道NTFS 格式的 Windows 硬盘在Mac OS X系统下只能读取不能写入,这一问题一直困扰着很多新老Mac 用户,一般的的解决办法就是安装 NTFS 插件来让 OS X 支持 NTFS ...