[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. ...
随机推荐
- spring autoWire注解和@resource注解区别
1.autoWire注解主要是按类型匹配.因为autowire的扫描机制,是按照接口类型来扫描bean的. 而JSR250 @resource注解是通过名称扫描注入的. @autowire注解的扫描方 ...
- JavaWeb项目开发案例精粹-第2章投票系统-001设计
1.项目结构 2.数据库设计 # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET ...
- 如何在Android应用程序中使用传感器模拟器SensorSimulator
原文地址; 如何在Android应用程序中使用传感器模拟器 - 移动平台应用软件开发技术 - 博客频道 - CSDN.NET http://blog.csdn.net/pku_android/arti ...
- Maven那点事儿(Eclipse版)
Maven那点事儿(Eclipse版) 前言: 由于最近工作学习,总是能碰到Maven的源码.虽然平时工作并不使用Maven,但是为了学习一些源码,还是必须要了解下.这篇文章不是一个全面的Mave ...
- FileObverse文件观察者的Debug报告
FileObverse文件观察者的Debug报告 2014年9月18日 9:03
- 用paint 计算字符串的像素宽度
方法1: //直接返回参数字符串所占用的像素宽度 Paint paint = new Paint(); width = paint.measureText(str); 有一些view可以直接得到pai ...
- BZOJ 3143 游走(高斯消元)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3143 题意:一个无向连通图,顶点从1编号到n,边从1编号到m.小Z在该图上进行随机游走, ...
- Java 包装类 自动装箱和拆箱
包装类(Wrapper Class) 包装类是针对于原生数据类型的包装. 因为有8个原生数据类型,所以对应有8个包装类. 所有的包装类(8个)都位于java.lang下. Java中的8个包装类分别是 ...
- Xmanager Enterprise Linking VM Redhat Linux AS4.7 X64–Server Configuration
[root@localhost ~]# vi /etc/inittab ## inittab This file describes how the INIT process shou ...
- Android Material Design-TabLayout的使用
TabLayout 位于 android.support.design.widget.TabLayout. 一般与 ViewPager 结合在一起使用.以前有开源库 viewpagerindicato ...