Set Mismatch问题及解法
来源:互联网 发布:ubuntu软件中心 编辑:程序博客网 时间:2024/06/04 23:34
问题描述:
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.
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.
我们可以利用映射关系,记录每个数出现的次数,在n范围内,出现0次的就是缺失的数,出现两次的就是重复的数。
过程详见代码:
class Solution {public: vector<int> findErrorNums(vector<int>& nums) { vector<int> used(10001, 0);vector<int> res;for (int i = 0; i < nums.size(); i++){used[nums[i]]++;if (used[nums[i]] > 1){res.push_back(nums[i]);}}for (int i = 1; i <= nums.size(); i++){if (used[i] == 0){res.push_back(i);break;}}return res; }};
阅读全文
0 0
- Set Mismatch问题及解法
- leetcode[Set Mismatch]//待整理多种解法
- Set Matrix Zeroes问题及解法
- Heaters问题及解法
- Permutations问题及解法
- Combinations问题及解法
- Subsets问题及解法
- Triangle问题及解法
- Minesweeper问题及解法
- 问题及原因:IllegalArgumentException:argument type mismatch
- LeetCode 645 Set Mismatch
- 645. Set Mismatch
- leetcode 645. Set Mismatch
- [LeetCode]645. Set Mismatch
- 【LeetCode】645. Set Mismatch
- 645. Set Mismatch
- leetcode645. Set Mismatch
- LeetCode 645. Set Mismatch
- kali linux 2016 配置ssh 实现远程登录
- 理解高并发(4).死锁监测方法
- 线程同步之关键代码段
- 骨骼蒙皮动画(Skinned Mesh)的原理解析(一)
- 我的第一篇CSDN博文
- Set Mismatch问题及解法
- form表单用js获取值
- MFC—ComboBox用法小结
- springmvc rest风格的实例代码demo讲解
- dell笔记本关闭触摸屏
- python:面向过程和面向对象编程思想
- 在公司已有的成熟产品(C/S)中新增功能的心得
- 2145: 2017湖南多校第十三场-20170723
- java丶JsonObject 的使用