LeetCode——Single Number III
Description:
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?
在线性时间复杂度下,找到两个只出现一次的元素。结果没有先后顺序。
既然是只出现一次且不考虑顺序,那么就可以机智的使用set的无序不可重复的特性。
public class Solution {
public int[] singleNumber(int[] nums) {
Set<Integer> set = new HashSet<Integer>(); for(int i : nums) {
if(!set.add(i)) {
set.remove(i);
}
}
int[] res = new int[2];
int i = 0;
for(int e : set) {
res[i++] = e;
} return res;
}
}
LeetCode——Single Number III的更多相关文章
- [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 ( a New Questions Added today)
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- 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 共享内存shm_open实现进程间大数据交互
linux 共享内存shm_open实现进程间大数据交互 read.c #include <sys/types.h> #include <sys/stat.h> #includ ...
- SpringAOP 通知(advice)
@Aspect @Order(1) public class AopOne { /** * 目标方法执行之前 * @param joinPoint */ @Before("executi ...
- hdu6199 gems gems gems dp+博弈
/** 2017 ACM/ICPC Asia Regional Shenyang Online 解题报告 题目:hdu6199 gems gems gems 链接:http://acm.hdu.edu ...
- PHP7新特性的介绍
关于PHP 20年的发展历史: 迄今为止最流行的WEB开发语言: 超过82%的网站都会使用PHP作为他们的服务端开发语言: 新特性介绍 PHP NG – Zend Engine 3 抽象语法树 64位 ...
- CSS边框-属性详解
图解CSS padding.margin.border属性 W3C组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落.列表.标题.图片以及层 ...
- Linux命令 cat命令
这个命令可不是“猫”的意思,而是catenate的缩写.顾名思义,是把东西串起来.比如:cat file1 file2就是把文件file1和file2连在一起,然后输出到屏幕上.注意,输出到屏幕上是c ...
- 【Java面试题】44 java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?
字节流,字符流.字节流继承于InputStream OutputStream,字符流继承于InputStreamReader OutputStreamWriter.在java.io包中还有许多其他的流 ...
- u3d发布成全屏的方式
using UnityEngine; using System.Collections; public class example : MonoBehaviour { public voi ...
- Jmeter零起点学习
什么是JMeter Apache JMeter是一个开源的Java桌面软件.设计的目的就是进行C/S架构软件的负载测试.随着发展,有很多人也用来进行一些静态资源或者动态资源的性能测试.可以支持的测 ...
- MathType如何编辑手写体l
MathType在编辑公式不仅方便而且规范,并且能够根据自己的需要选择不同的字体进行使用,可以是正体也可以是斜体,可以是新罗马体,也可以是花体,这些用word公式编辑器MathType都是可以的.还有 ...