Leetcode-645 Set Mismatch
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.
上来很直接的思路就是:开一个数组做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的更多相关文章
- 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 ...
- 645. Set Mismatch - LeetCode
Question 645. Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数 ...
- 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 = ...
- Leetcode 之 Set Mismatch
645. Set Mismatch 1.Problem The set S originally contains numbers from 1 to n. But unfortunately, du ...
- 【Leetcode_easy】645. Set Mismatch
problem 645. Set Mismatch 题意: solution1: 统计每个数字出现的次数了,然后再遍历次数数组,如果某个数字出现了两次就是重复数,如果出现了0次,就是缺失数. 注意:如 ...
- 【LeetCode】645. Set Mismatch 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Hash方法 直接计算 日期 题目地址: https ...
- Java实现 LeetCode 645 错误的集合(暴力)
645. 错误的集合 集合 S 包含从1到 n 的整数.不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复. 给定一个数组 n ...
- 645. Set Mismatch
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 645. Set Mismatch挑出不匹配的元素和应该真正存在的元素
[抄题]: he set S originally contains numbers from 1 to n. But unfortunately, due to the data error, on ...
随机推荐
- mysqldump 参数--lock-tables浅析
mysqldump有一个参数--lock-tables,以前对这个参数也没有详细了解过,直到上次有个网友问"参数lock-tables 是一次性锁定当前库的所有表,还是锁定当前导出表?&qu ...
- js实现复选框的全选、全不选和反选
js实现复选框的全选.全不选和反选 主要是用遍历的方法查找元素,然后通过改变checked的属性来选择,为true则是选中状态,为false则是未选状态 实现代码 <!DOCTYPE html& ...
- SQL 事务隔离级别
转载来源:https://www.cnblogs.com/chenmh/p/3998614.html 标签: SQL SEERVER/MSSQL SERVER/SQL/事务隔离级别选项/设置数据库事务 ...
- Dapp已来,如何把握区块链创富模式变化的趋势
区块链技术持续升温,Dapp迅速成为焦点,未来区块链市场上新的蓝海.这么说当然不是空想出来的,而是从区块链市场的发展趋势中推断而来.区块链市场由最初的挖矿炒币到如今尝试投入行业应用,随着技术和应用模式 ...
- 同步下的资源互斥:停运保护(Run-Down Protection)机制
背景 近期在学习ProcessHacker的源码,Process Hacker是一个免费的.功能强大的"任务管理器",可用于监听系统资源的使用情况,调试软件以及检测恶意程序.使用中 ...
- 【爬坑】远程连接 MySQL 失败
问题描述 远程连接 MySQL 服务器失败 报以下错误 host 192.168.23.1 is not allowed to connect to mysql server 解决方案 在服务器端打开 ...
- GitHub-标签管理
参考博文:廖雪峰Git教程 1. 创建标签 切换到需要打标签的分支上,之后打标签 [root@mini05 zhangtest]# git branch dev * master [root@mini ...
- Ecto 总结
ecto 简介 ecto 相当于 elixir 的 ORM,但是得益于 elixir 语言,和传统的 ORM 相比,更加简洁和强大. ecto 主要分为 4 部分: Repo: 这是和真正数据库交互的 ...
- puppet 和 apache passenger的配置
目录 1. 概要 2. apache passenger 安装测试 2.1. 什么是 apache passenger 2.2. 安装 apache passenger 2.3. 配置 apache ...
- 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, ...