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?

看到这种题第一个想法就是hashset啊哈哈哈。

根据hashset的特性,如果hashset.add()失败的话,证明这里面已经有了一个相同的值,就是说这个number不是single number了。

所以我们判断出【不是single number】的number再将它们从hashset里面移除,那么剩下的就是我们要找的single number了。

因为最后答案single number是在hashset中,我们并不能直接返回hashset,所以这里我们要借助iterator中的iterator().next()method。

代码如下。~

public class Solution {
public int singleNumber(int[] nums) {
HashSet<Integer> set = new HashSet<Integer>();
for(int i=0;i<nums.length;i++){
if(!set.add(nums[i])){
set.remove(nums[i]);
}
}
return set.iterator().next();
}
}

[LeetCod] Single Number的更多相关文章

  1. [LeetCode] Single Number III 单独的数字之三

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  2. [LeetCode] Single Number II 单独的数字之二

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

  3. [LeetCode] Single Number 单独的数字

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  4. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

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

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

  6. [LintCode] Single Number 单独的数字

    Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in ...

  7. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  8. LeetCode 【Single Number I II III】

    Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...

  9. Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference

    最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...

随机推荐

  1. git的学习网站

    git官网:http://git-scm.com/ http://gitref.org/index.html http://edu.51cto.com/lesson/id-33751.html     ...

  2. jQuery编程基础精华01(jQuery简介,顶级对象$,jQuery对象、Dom对象,链式编程,选择器)

    jQuery简介 什么是jQuery? jQuery就是一个JavaScript函数库,没什么特别的.(开源)联想SQLHelper类 jQuery能做什么?jQuery是做什么的? jQuery本身 ...

  3. ArcGIS 10 影像去黑边

    在作卫片执法项目中,需要多个影像叠加截图,这就会出现影像黑边叠加的情况,这时就需要对多幅影像进行处理.主要有两种处理方式:以ArcGIS10.1为例,操作如下:     1.acrtoolbox——& ...

  4. 使用 Async 和 Await 的异步编程

    来自:http://msdn.microsoft.com/library/vstudio/hh191443   异步对可能起阻止作用的活动(例如,应用程序访问 Web 时)至关重要. 对 Web 资源 ...

  5. sqort函数用法总结

    qsort包含在<stdlib.h>头文件中,此函数根据你给的比较条件进行快速排序,通过指针移动实现排序.排序之后的结果仍然放在原数组中.使用qsort函数必须自己写一个比较函数. 函数原 ...

  6. git branch

    使用git管理工具,branch 应该是我们接触最多的.不论我们是修复bug,还是做项目,都会新开branch,工作完成后再合并. 然而对一些初学者,对git的一些命令知之甚少,这里,给大家写一些常用 ...

  7. Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note (vector 替换)

    题目链接 题意:给m个数字, 这些数字都不大于 n,  sum的值为相邻两个数字 差的绝对值.求这n个数字里把一个数字 用 其中另一个数字代替以后, 最小的sum值. 分析:刚开始以为两个for 最坏 ...

  8. 用net匹配并替换iOS标准的emoji表情符号

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespaceCommo ...

  9. 命名空间“System.Web”中不存在类型或命名空间名称“Script”(是缺少程序集引用吗?)

    网上有些资料说,在项目上鼠标右键,添加引用→.Net→System.Web.Entensions就可以了. 实际上很多时候在项目中的添加引用窗口上,根本找不到System.Web.Entensions ...

  10. 图形数据库、NOSQL和Neo4j

    简介 在众多不同的数据模型里,关系数据模型自80年代就处于统治地位,而且有不少实现,如Oracle.MySQL和MSSQL,它们也被称为关系数据库管理系统(RDBMS).然而,最近随着关系数据库使用案 ...