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. 几种方法来实现scp拷贝时无需输入密码

    欢迎转载!转载时请注明出处:http://blog.csdn.net/nfer_zhuang/article/details/42646849 前言 我在工作中经常要将一些文件传输到另外一个服务器上, ...

  2. mysql建表基本语法

    mysql添加约束的两种条件: ------表的内部添加(约束) 列名1 数据类型 (int) primary key auto_increment,---主键默认不能为空的 列名2 数据类型 not ...

  3. IntelliJ IDEA 导入Spring源码

    第一步: 使用git 拉取代码 git 命令: git init    //创建git仓库 git clone  https://github.com/spring-projects/spring-f ...

  4. 【夯实Ruby基础】Ruby快速入门

    本文地址: http://www.cnblogs.com/aiweixiao/p/6664301.html 文档提纲 扫描关注微信公众号 1.Ruby安装 1.1)[安装Ruby] Linux/Uni ...

  5. Java 8 新特性:3-函数(Function)接口

    (原) 以前,在创建泛型时,是这么写的: List<String> list = new ArrayList<String>(); 现在,可以这么写了: List<Str ...

  6. 16.ajax_case01

    # 抓取北京市2018年积分落户公示名单 # 'http://www.bjrbj.gov.cn/integralpublic/settlePerson' import csv import json ...

  7. Java学习笔记(四)——好记性不如烂键盘(答答租车)

    根据所学知识,编写一个控制台版的租车系统. 功能: 1. 展示所有可租车辆 2. 选择车型.租车辆 3. 展示租车清单,包含:总金额.总载货量及其车型.总载人量及其车型 代码参考imooc中Java课 ...

  8. TensorFlow的主要依赖库

    Protool Buffer 处理结构化数据的工具 Name:张飞 id: 123456 email: 10000@qq.com 上面信息 就是一个结构化数据(这里说的结构化数据和大数据的结构化数据概 ...

  9. VM虚拟机ubantu自适应屏幕大小

    1.菜单栏安装VMware-Tool sudo ./wmware-install.pl 2.sudo apt-get install open-vm-tools装完这两个就可以,有些人只安装了第一个, ...

  10. WiFi-ESP8266入门http(3-4)网页一键配网(1若为普通wifi直连 2若为西电网页认证自动网页post请求连接)+网页按钮灯控+MQTT通信

    网页一键配网(1若为普通wifi直连  2若为西电网页认证自动网页post请求连接)+网页按钮灯控+MQTT通信 工程连接:https://github.com/Dongvdong/ESP8266_H ...