[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 ...
随机推荐
- 69. Sqrt(x)
题目: Implement int sqrt(int x). Compute and return the square root of x. 链接: http://leetcode.com/pr ...
- HDU5099——Comparison of Android versions(简单题)(2014上海邀请赛重现)
Comparison of Android versionsProblem DescriptionAs an Android developer, itˇs really not easy to fi ...
- Java API —— 网络编程
1.网络编程概述 1)网络编程概述 · 计算机网络 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通 ...
- JPA和Hibernate的区别
JPA Java Persistence API,是Java EE 5的标准ORM接口,也是ejb3规范的一部分. Hibernate,当今很流行的ORM框架,是JPA的一个实现,但是其功能是JPA的 ...
- Oracle数据泵导入导出数据,建立表空
Oracle11g 数据导入到oracle10g 中:1.在oracle11g 服务器命令行中用expdp 导出数据expdp ts/ts@orcl directory=expdp_dir dumpf ...
- [LeetCode#267] Palindrome Permutation II
Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- HDU 1677
Nested Dolls Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- BootStrap弹窗
效果图: 注意引入的文件,js文件要在前面 Bootstrap框架中的模态弹出框,分别运用了“modal”.“modal-dialog”和“modal-content”样式,而弹出窗真正的内容都放置在 ...
- 解决魅族USB调试无法被电脑识别的问题(含Mac OS X、Win7)
每次打开豌豆荚或者360手机助手之类手机助手后Eclipse才会检测到mx4(实际上是豌豆荚关闭eclipse的adb使用自己的驱动连接的).解决方法就是在"adb_usb.ini&qu ...
- Java [Leetcode 94]Binary Tree Inorder Traversal
题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...