问题:

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. c#.netGr idView1在div不局中

    <div style="margin:0 auto;text-align:center;" >//可以用GridView剧中 <asp:GridView ID=& ...

  2. SQL Server 2005中的分区表

    记录笔记: 转自 猪八戒学做网站 SQL Server 2005中的分区表(一):什么是分区表?为什么要用分区表?如何创建分区表? SQL Server 2005中的分区表(二):如何添加.查询.修改 ...

  3. CAD2010中文版安装问题记录

    CAD2010中文版安装到简体中文语言包的时候提示[错误1606无法访问网络位置setup],直接打开cad2010 提示 [adui18res.dll not found]: 首先,进入注册表(re ...

  4. Effective C++ -----条款02:尽量以const, enum, inline替换 #define

    class GamePlayer{private: static const int NumTurns = 5; int scores[NumTurns]; ...}; 万一你的编译器(错误地)不允许 ...

  5. codeforces 556B. Case of Fake Numbers 解题报告

    题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...

  6. 【python】An Introduction to Interactive Programming in Python(week two)

    This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.e ...

  7. OSG快速生成一个带有纹理的四边形Geometry

    可以使用Geometry头文件中的 Geometry* createTexturedQuadGeometry osg::ref_ptr<osg::Texture2D> texture = ...

  8. HTML标记语法之表格元素

    语法与语义: <table>和</table>定义表格的开始和结束 <thead>和</thead>定义表格头部的开始和结束 <tbody> ...

  9. Java大数处理类:BigInteger类和BigDecimal类

    当我们要处理非常大的数据时,平常用的数据类型已不足以表示,在Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,这两个类在理论上只要计算机内存足够大就能够表示无线 ...

  10. 报错 - Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lex failed with exit code 1

    这里有两种情况:第一种是某xxx.m文件不存在或路径不对,而且里面有5.1什么的解决方法如下:在Build Phases-->Complie Sources中找到有两个xxx.m文件,一个正常, ...