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 ...
随机推荐
- Objective-C中.h文件、.m文件中@interface、@synthesize及其它
很多开发iOS好几年的老鸟,可能都不太分的清.h文件和.m文件里各种结构的用途和区别.最近仔细研究了一下,写一篇文章记下来. 一般的,写一个Class的时候,经常是这种格式(以UIViewContro ...
- Zookeeper中的选举机制
Zookeeper虽然在配置文件中并没有指定master和slave,但是,zookeeper工作时,是有一个节点为leader,其他则为follower.leader是通过内部的选举机制临时产生的. ...
- libxl库的介绍,对Excel操作封装得很好的一个库,兼容2007版和多字节字符(最后有破解版下载)
前段时间忙着毕业论文,终于有时间写博客了. 早些时候老大给我的一个任务需要对excel进行读表操作,研究了一下c++对excel的操作. 对Excel的操作基本有com,ODBC,AD等,其中ODBC ...
- servlet里面拿到common.property的属性
---------------------common.property文件----------------------- kongxc_wx_dinghuo_orderSendMusic=http: ...
- UML类图简单说明,学习编程思路的必会技能
摘抄记录学习用 先看一张图,图片资源来自于大话设计模式,接下来我要跟着这本书一起走,如侵权,立即删除. 看见上图估计没学过或者不是本专业的没接触过的人,看这一堆估计就不想看了,但别怕一个一个分析. 一 ...
- Java异常框架设计
什么是异常? 异常(exception)应该是异常事件(exceptional event)的缩写.异常定义:异常是一个在程序执行期间发生的事件,它中断正在执行的程序的正常的指令流.当在一个方法中发生 ...
- node js 读取mysql
1.新版node自带npm 2.下载npm不需要node命令 3.懒得配环境变量.直接把生成的npm复制到报错目录,再把mysql模块复制回来 var mysql = require('mysql') ...
- 【python】获取网页中中文内容并分词
# -*- coding: utf-8 -*- import urllib2 import re import time import jieba url="http://www.baidu ...
- VC++ 在Watch窗口显示GetLastError值以及详细信息
You can display the value GetLastError() will return by putting "@err" in your watch windo ...
- nginx搭建文件服务器
在部署了各种应用后,产生的日志文件,需要在线下载查看,不用每次登陆服务器去拿: 这里,因为服务器部署了很多的应用程序,可以建一个主目录mylog,在主目录里用软连接将需要的各个日志文件夹都建好连接 l ...