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:

  1. The given array size will in the range [2, 10000].
  2. The given array's numbers won't have any order.

上来很直接的思路就是:开一个数组做hash找重复值,然后利用等差数列求缺失值。时间O(N), 空间O(N)

// Space complexity O(n)
// Time complexity O(n)
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
vector<int> res;
int n = nums.size();
int rep, mis;
int hash[n + ] = {};
for (int i = ; i < n; i++){
if (hash[nums[i]] == ){
hash[nums[i]] = ;
}
else{
rep = nums[i];
break;
}
}
int sum = accumulate(nums.begin(), nums.end(), );
mis = ( + n) * n / - (sum - rep);
res.push_back(rep);
res.push_back(mis);
return res;
}
};

思路二:  不使用额外的空间的话,就要考虑数组值的特性了。这里的数字全为正。所以我们可以利用数字的正负做一个判断,判断有没有重复出现,以及有没有出现。要是只出现一次,那么对应位置的索引应该是负值,而如果出现两次,那么就不为负值了。

代码:

// Space complexity O(1)
// Time complexity O(n)
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
vector<int> res;
int n = nums.size();
int rep, mis;
for (int i = ; i < n; i++){
if (nums[ abs(nums[i]) - ] > ){
nums[ abs(nums[i]) - ] *= -;
}
else{
res.push_back(abs(nums[i])); }
}
for (int i = ; i < n; i++){
if (nums[i] > ){
res.push_back(i + ); // 丢失值对应的索引 - 1 就等于原来数组中没有变化的值(即大于0的值)
}
} return res;
}
};

Leetcode-645 Set Mismatch的更多相关文章

  1. LeetCode 645. Set Mismatch (集合不匹配)

    The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...

  2. leetcode 645. Set Mismatch——凡是要节约空间的题目 都在输入数据上下功夫 不要担心破坏原始的input

    The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...

  3. 645. Set Mismatch - LeetCode

    Question 645. Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数 ...

  4. 448. Find All Numbers Disappeared in an Array&&645. Set Mismatch

    题目: 448. Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = ...

  5. Leetcode 之 Set Mismatch

    645. Set Mismatch 1.Problem The set S originally contains numbers from 1 to n. But unfortunately, du ...

  6. 【Leetcode_easy】645. Set Mismatch

    problem 645. Set Mismatch 题意: solution1: 统计每个数字出现的次数了,然后再遍历次数数组,如果某个数字出现了两次就是重复数,如果出现了0次,就是缺失数. 注意:如 ...

  7. 【LeetCode】645. Set Mismatch 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Hash方法 直接计算 日期 题目地址: https ...

  8. Java实现 LeetCode 645 错误的集合(暴力)

    645. 错误的集合 集合 S 包含从1到 n 的整数.不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复. 给定一个数组 n ...

  9. 645. Set Mismatch

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  10. 645. Set Mismatch挑出不匹配的元素和应该真正存在的元素

    [抄题]: he set S originally contains numbers from 1 to n. But unfortunately, due to the data error, on ...

随机推荐

  1. java传输文件的简单方法

    假设现在已经打包了一个文件(1233444333),要将这个文件传输给另一方: package file; import java.io.*; public class F_PasswordUnPas ...

  2. The concurrent snapshot for publication 'xxx' is not available because it has not been fully generated or the Log Reader Agent is not running to activate it

    在两台测试服务器部署了复制(发布订阅)后,发现订阅的表一直没有同步过来.重新生成过snapshot ,也重新初始化过订阅,都不能同步数据,后面检查Distributor To Subscriber H ...

  3. MyBatis笔记----SSM框架mybatis3整合springmvc spring4

    上节 无springmvc框架 http://www.cnblogs.com/tk55/p/6661786.html 结构 jar包 web.xml 与index.jsp <?xml versi ...

  4. ALTER添加列后,立即UPDATE该列会报错

    SQL 2008 R2 请看语句: ALTER TABLE #t ADD col2 INT UPDATE #t SET col2 = 0 报错:列名'col2'无效. 但如果紧接的是SELECT,一切 ...

  5. 【MM系列】SAP的库存管理

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP的库存管理   前言部分 库存 ...

  6. c/c++ 通用的(泛型)算法 之 只读算法,写算法,排序算法

    通用的(泛型)算法 之 只读算法,写算法,排序算法 只读算法: 函数名 功能描述 accumulate 求容器里元素的和 equal 比较2个容器里的元素 写算法 函数名 功能描述 fill 用给定值 ...

  7. VS2017做为Unity3D的脚本编辑器需要的最精简组件

    前言 使用VS2017做为Unity的脚本编辑器,需要的最精简组件. 我的测试环境 windows 10 x64 windows 7 x64 sp1 时间:2017-4-22 更新于2018-12-4 ...

  8. git-------基础知识(本地推送项目版本---github上)

    创建Git仓库 一:初始化版本库:-git init 二:添加文件到缓存区:-git add  --添加所有文件 是:加个点-列:git add . 三:查看仓库状态:-git status 四:添加 ...

  9. jQuery入门(1)

    1.了解jQuery与JavaScript的区别 css --你的长相啦 Html --躯干 js --运动神经 jQuery就是对JavaScript的一个拓展,封装,就是让JavaScript更好 ...

  10. May 24. 2018 Week 21st Thursday

    Man errs so long as he strives. 失误是进取的代价. It is not important that the man in the arena didn't win, ...