[LeetCode] Single Number III ( a New Questions Added today)
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
Note:
- The order of the result is not important. So in the above example,
[5, 3]is also correct. - Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
这是今天刚加上去的一道题。
个人觉得这道题和之前single number的两道差不多。依旧用hashset即可做出。
唯一要注意的就是最后return的时候不能直接return hashset。为了偷懒我直接弄了个新的int[]。
代码如下。~
public class Solution {
public int[] singleNumber(int[] nums) {
if(nums.length==2&&nums[0]!=nums[1]){
return nums;
}
HashSet<Integer> store=new HashSet<Integer>();
HashSet<Integer> result=new HashSet<Integer>();
for(int i=0;i<nums.length;i++){
if(!result.add(nums[i])){
result.remove(nums[i]);
store.add(nums[i]);
}else{
if(store.contains(nums[i])){
result.remove(nums[i]);
}
}
}
int[] print=new int[2];
print[0]=result.iterator().next();
result.remove(result.iterator().next());
print[1]=result.iterator().next();
return print;
}
}
[LeetCode] Single Number III ( a New Questions Added today)的更多相关文章
- [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 III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- LeetCode——Single Number III
Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...
- LeetCode Single Number III (xor)
题意: 给一个数组,其中仅有两个元素是出现1次的,且其他元素均出现2次.求这两个特殊的元素? 思路: 跟查找单个特殊的那道题是差不多的,只是这次出现了两个特殊的.将数组扫一遍求全部元素的异或和 x,结 ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
随机推荐
- Linux和Linux之间共享目录
1.Linux 服务器端NFS服务器的配置 以root身份登陆Linux服务器,编辑/etc目录下的共享目录配置文件exports,指定共享目录及权限等. 执行如下命令编辑文件/etc/exports ...
- AO创建IFeature的两种方法
原文地址:http://www.cnblogs.com/MyLucifer/archive/2010/12/01/1893212.html 在ArcGIS Resouce Center中,ESRI介绍 ...
- 258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- shell编程基础(3)条件判断语句
1,带参数的shellscript #this is program build 5.11 to test shell script ############ cxz ####### 5.11 ### ...
- 【C#设计模式——创建型模式】工场方法模式
工场方法模式对简单工场模式进行了乔庙的扩展,不是用一个专门的类来决定实例化哪一个子类.相反,超类把这种决定延迟到每个子类.这种模式实际上没有决策点,就是没有直接选择一个子类实例化的决策. 看书上的例子 ...
- Effective C++学习笔记 条款04:确定对象被使用前已先被初始化
一.为内置类型对象进行手工初始化,因为C++不保证初始化它们. 二.对象初始化数据成员是在进入构造函数用户编写代码前完成,要想对数据成员指定初始化值,那就必须使用初始化列表. class A { pu ...
- 信息:Could not publish server configuration for Tomcat v6.0 Server at localhost. Multiple Context
需要把server.xml更正一下,去掉重复的context.或者把整个server文件夹都删掉,重新添加服务器.也可以在server窗口中删除server,再新添加一个server.
- Codeforces_GYM Flight Boarding Optimization
(ACM ICPC 2013–2014, NEERC, Northern Subregional Contest) Flight Boarding OptimizationInput file: fl ...
- sql Server 的基本函数
--聚合函数 use pubs go select avg(distinct搜索 price) --算平均数 from titles where type='business' go use pubs ...
- UVa 10791 (唯一分解) Minimum Sum LCM
题意: 输入n,求至少两个正整数,使得这些数的最小公倍数为n且和最小. 分析: 设n的分解式为,很显然单独作为一项,和最小. 这里有两个小技巧: 从2开始不断的除n,直到不能整除为止.这样就省去了素数 ...