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 ...
随机推荐
- dedecms使用
使用到了dedecms内容管理系统,遇到两个问题: 1.点击登录后,没有登录进去也没有任何提示,一片空白 解决办法:我是把网站从云主机拷贝下来的,但是忘了云主机上的数据库密码和自己本地的数据库密码不一 ...
- myeclipse之完全破解
并不是所有的破解都是成功的,就如并不是所有的战争都会胜利一样,我们在做事情的时候,总会遇到些问题,比如Activate不成功,需要手动激活. 激活不成功就是不成功,来回的破解.卸载.重装,都还是不可能 ...
- JavaScript学习日志(2)
javascript数据类型: 字符串string.数字number.未定义Undefined.空Null.布尔Boolean.数组Array.对象Object.javascript对象: 对象由花括 ...
- 简单的异步Socket实现——SimpleSocket_V1.1
简单的异步Socket实现——SimpleSocket_V1.1 笔者在前段时间的博客中分享了一段简单的异步.net的Socket实现.由于是笔者自己测试使用的.写的很粗糙.很简陋.于是花了点时间自己 ...
- Java动态代理机制小结
因为最近学习hadoop中用到了动态代理的相关知识,之前AOP编程也碰到过,所以在这里特地总结一下. 在java的动态代理机制中,有两个重要的类或接口,一个是 InvocationHandler(In ...
- java 利用同步工具类控制线程
前言 参考来源:<java并发编程实战> 同步工具类:根据工具类的自身状态来协调线程的控制流.通过同步工具类,来协调线程之间的行为. 可见性:在多线程环境下,当某个属性被其他线程修改后,其 ...
- 第一百四十六节,JavaScript,百度分享保持居中--下拉菜单
JavaScript,百度分享保持居中--下拉菜单 百度分享保持居中 效果图 html代码 <div id="share"> <h2>分享到</h2& ...
- crontab读不来环境变量
无法识别java nohup: failed to run command `java': No such file or directory 那么在shell中加两行即可解决 . /etc/prof ...
- 【JavaEE】SSH+Spring Security整合及example
到前文为止,SSH的基本框架都已经搭建出来了,现在,在这基础上再加上权限控制,也就是Spring Security框架,和前文的顺序一样,先看看需要加哪些库. 1. pom.xml Spring Se ...
- linux more less cat
在使用和维护Linux系统时,常常需要查看文件的相关内容,那么如何才能做到呢?下面小编就以CentOS6.4系统为例演示查看文件内容的几种常用的方法. 工具/原料 CentOS6.4 查看文件内容 ...