[LeetCod] Single Number
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的更多相关文章
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- [LintCode] Single Number 单独的数字
Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- LeetCode 【Single Number I II III】
Given an array of integers, every element appears twice except for one. Find that single one. 思路: 最经 ...
- 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 ...
随机推荐
- Mybatis全部查询遇到的返回类型的小问题
在学习Mybatis3过程中遇到一个小问题,觉得需要注意就把它写下来了 在查询所有数据的时候方法是这样的 public List<User> findAll(){ ..... } 在它的u ...
- Entity Freamwork 6连接PostgreSql数据库
原文 Entity Freamwork 6连接PostgreSql数据库 开发环境 VS 2015 Update 1 Postgre Sql 9.4 使用过程 1.使用Nuget在项目中添加对E ...
- IDEA查找功能小结
查找类:Ctrl + N 支持模糊查询
- JCIFS读取远程服务器文件过慢的解决方法
JCIFS读取远程服务器文件过慢的解决方法 发表于3年前(2013-07-12 11:23) 阅读(1174) | 评论(0) // 我要收藏"; var favor_del = &qu ...
- Java API —— Set接口 & HashSet类 & LinkedHashSet类
1.Set接口 1)Set接口概述 一个不包含重复元素的 collection,无序(存储顺序和取出顺序不一致),唯一. (List有序,即存储顺序和取出顺序一致,可重复) ...
- (六)CSS伪元素
CSS伪元素用于向某些选择器设置特殊效果. 伪元素的用法和伪类相似: selector:pseudo-element {property:value;} CSS类也可以与伪元素配合使用: select ...
- IOS基础框架
GameKit 为游戏提供网络功能:点对点互联和游戏中的语音交流 AddressBook 提供访问用户联系人信息的功能 AddressBookUI 提供一个用户界面,用于显示存储在地址簿中的联系人信息 ...
- [leetcode72]Edit Distance(dp)
题目链接:https://leetcode.com/problems/edit-distance/ 题意:求字符串的最短编辑距离,就是有三个操作,插入一个字符.删除一个字符.修改一个字符,最终让两个字 ...
- CSS+DIV问题!DIV的最小高度问题!
DIV层的最小高度问题!就是一个DIV有个最小高度,但是如果DIV层中的内容很多,DIV的高度会根据内容而进行拉长!要求IE6.IE7还有firefox都要兼容!我试了很多网上的方法都不好用!请测试后 ...
- ArrayList和List之间的转换
开发中不免碰到List与数组类型之间的相互转换,举一个简单的例子: package test.test1; import java.util.ArrayList; import java.util.L ...