Leetcode 之 Set Mismatch
645. Set Mismatch
1.Problem
The set S
originally contains numbers from 1 to n
. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.
Given an array nums
representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.
Example 1:
Input: nums = [1,2,2,4]
Output: [2,3]
Note:
- The given array size will in the range [2, 10000].
- The given array's numbers won't have any order.
2.Solution
题目的大意是原本集合S,包含1到n中的所有数,但1~n中的某个数出现了错误,可以理解为1~n中的某个数x变成了另一个数y,这样集合S中就出现了2个y,且丢失了x,返回数组[x,y]
3.Code
//先找出 出现重复的那个数,再找出缺失的那个数,时间复杂度为O(N),空间复杂度为O(N)
class Solution {
public int[] findErrorNums(int[] nums) {
int[] temp = new int[nums.length + 1];
int[] res = new int[2];
for ( int i = 0 ; i < nums.length ; i++ ) {
if ( temp[nums[i]] == 0 ) {
temp[nums[i]] = 1;
} else {
temp[nums[i]] = 0;
res[0] = nums[i];
}
} for ( int i = 1 ; i <= nums.length ; i++ ) {
if ( temp[i] == 0 && i != res[0] ) {
res[1] = i;
}
}
return res;
}
}
//时间复杂度O(N),空间复杂度O(1)
class Solution {
public int[] findErrorNums(int[] nums) {
int duplicates = 0;
int miss = 0;
for (int i = 0 ; i < nums.length ; i++ ) {
if ( nums[ Math.abs(nums[i]) - 1 ] < 0 ) {
duplicates = Math.abs(nums[i]);
} else {
nums[ Math.abs(nums[i]) - 1 ] *= -1;
}
} for ( int i = 0 ; i < nums.length ; i++ ) {
if ( nums[i] > 0 ) {
miss = i + 1;
}
}
return new int[]{duplicates,miss};
}
}
//时间复杂度O(N),空间复杂度O(1),利用位操作思想,将duplicate和miss找出来,注释已经很详细了,此处不赘述解题思想
class Solution {
public int[] findErrorNums(int[] nums) {
int xor = 0 , xora = 0 , xorb = 0; for ( int i = 0 ; i < nums.length ; i++ ) {
xor ^= nums[i];
} for ( int i = 1 ; i <= nums.length ; i++ ) {
xor ^= i;
}
//xor = duplicates ^ miss;
//想办法从xor中分离出duplicate 和 miss //先从xor中分离出左右的为1的那位,利用这个可以将duplicate和miss分开
int separate = xor & ( ~( xor - 1 ) ); for ( int i = 0 ; i < nums.length ; i++ ) {
if ( (separate & nums[i]) != 0 ) {
xora ^= nums[i]; //此处写xora 还是 xorb都可以,只要跟下面一个循环对应起来就ok
} else {
xorb ^= nums[i];
}
} for ( int i = 1 ; i <= nums.length ; i++ ) {
if ( (separate & i) != 0 ) {
xora ^= i; //跟上面对应
} else {
xorb ^= i;
}
} //此时的xora、xorb 中一个是duplicates,另一个是miss
//如果xora出现在了nums数组中,那其一定是duplicate,反之则是miss
for ( int a : nums ) {
if ( a == xora ) {
return new int[]{xora,xorb};
}
}
return new int[]{xorb,xora};
}
}
Leetcode 之 Set Mismatch的更多相关文章
- LeetCode 645. Set Mismatch (集合不匹配)
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- leetcode 645. Set Mismatch——凡是要节约空间的题目 都在输入数据上下功夫 不要担心破坏原始的input
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- leetcode算法总结
算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...
- [LeetCode] Set Mismatch 设置不匹配
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- LeetCode算法题-Set Mismatch(Java实现)
这是悦乐书的第279次更新,第295篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第147题(顺位题号是645).集合S最初包含从1到n的数字. 但不幸的是,由于数据错误 ...
- 【LeetCode】645. Set Mismatch 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Hash方法 直接计算 日期 题目地址: https ...
- 645. Set Mismatch - LeetCode
Question 645. Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数 ...
- C#LeetCode刷题之#645-错误的集合(Set Mismatch)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3887 访问. 集合 S 包含从1到 n 的整数.不幸的是,因为数 ...
- [LeetCode] Find the Duplicate Number 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
随机推荐
- 架设HmailServer邮件服务器以及webmail
参考:http://www.it0355.com/a/201207/31/a9275.htm 在安裝Hmailserver前先安裝Apache.php.mysql,如果你想懶點直接到http://ww ...
- :(23, 7) in class Queen, multiple overloaded alternatives of constructor Queen define default arguments. class Queen private(val name:String,prop:Array[String],private[scala02] val age:Int = 18){
- redis存储对象与对象序列化详解
redis主要存储类型最常用的五种数据类型: String Hash List Set Sorted set redis存储对象序列化和反序列化 首先来了解一下为什么要实现序列化 为什么要实现序列化接 ...
- libubox
lbubox是openwrt的一个核心库,封装了一系列基础实用功能,主要提供事件循环,二进制格式处理,linux链表实现和一些JSON辅助处理. 它的目的是以动态链接库方式来提供可重用的通用功能,给其 ...
- 卧槽! JavaScript JVM运行Java!!
由于任何计算机语言都具有巨大的灵活性,软件世界变得有点疯狂.一旦你已经吸收了用这种语言编写的编译器的想法,那么它会编译还有什么可以留下来的?但是......用JavaScript编写的Java虚拟机J ...
- Android.mk介绍
Secrets of Android.mk Intro to Android.mk Simple example NDK Usage Defining Modules Simple APK APK D ...
- Mysql数据库存储是乱码问题(或者在查询时无法加载数据)
在连接数据库时添加一行代码即可解决:?useUnicode=true&characterEncoding=utf8 截图如下:
- 简单的php基于curl的反向代理程序
起因: 经理:需要实现一个反向代理? 我: 简单,nginx分分钟配置好. 经理:嗯?没有nginx? 我: nodejs也行啊,网上有例子分分钟搞定. 经理:嗯?只有虚拟主机,只能上传php程序? ...
- 第十三篇:multimap容器和multiset容器中的find操作
前言 multimap容器是map容器的“ 增强版 ”,它允许一个键对应多个值.对于map容器来说,find函数将会返回第一个键值匹配元素所在处的迭代器.那么对于multimap容器来说,find函数 ...
- cocos3.x 实现android沉浸式模式(全屏,隐藏导航栏即虚拟键)
只有在Android 4.4及以上系统才支持沉浸式模式,修改 AppActivity代码如下: @Override public Cocos2dxGLSurfaceView onCreateView( ...