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 ...
随机推荐
- 非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry
非常全面的SQL Server巡检脚本来自sqlskills团队的Glenn Berry Glenn Berry 曾承诺对这个脚本持续更新 -- SQL Server 2012 Diagnostic ...
- Wampserver或者帝国CMS安装后, 打开localhost显示IIS欢迎界面图片
我们在安装集成环境Wampserver或者帝国CMS之后,有时会遇到一个问题, 打开localhost显示一张IIS欢迎界面图片,这个问题该如何解决呢,我在这里简单整理了一下解决方法 电脑win10系 ...
- ajax调用WebService实现数据库操作
首先说下测试环境和思路: 前端收集数据转换成json格式传输到后端,处理并存入数据库 1.数据库操作: [WebMethod] public string InsertPoint(string dat ...
- MySQL数据库有哪些安全相关的参数需要修改?
https://dev.mysql.com/doc/refman/5.7/en/security-options.htmlhttps://dev.mysql.com/doc/refman/5.7/en ...
- LeetCode算法题-Fizz Buzz(Java实现)
这是悦乐书的第221次更新,第233篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第88题(顺位题号是412). 编写一个程序,输出从1到n的数字的字符串表示.但对于三的 ...
- Elixir 单元测试
概述 elixir 中自带了单元测试框架 ExUnit ,其中提供单元测试的一系列,主要包含以下模块: ExUnit: 单元测试框架 ExUnit.Assertions: 断言 ExUnit.Case ...
- 英语进阶系列-A01-再别康桥
每天必读知识 音标 发音网页 人称代词与物主代词 时态 朗读50遍词汇系列1 Number word 1 be 2 have 3 get 4 give 5 take 诗歌欣赏 [原诗] [英文版] 再 ...
- FastJSON、Gson和Jackson性能对比
Java处理JSON数据有三个比较流行的类库FastJSON.Gson和Jackson.本文将测试这三个类库在JSON序列化和反序列化的方面表现,主要测试JSON序列化和反序列化的速度.为了防止由于内 ...
- 如何学习sss和前端数据处理
1.学习scss,就看这篇:http://www.ruanyifeng.com/blog/2012/06/sass.html 就够了,因为sass的出现本来就是为了简化工作提高效率,也不算什么深奥精妙 ...
- UltraISO制作Ubuntu14.04 64bit到U盘文件载入不完整
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zinss26914/article/details/37728251 前言 今天新买的Thinkpa ...