645. Set Mismatch

Problem statement

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.

Solution one:

This problem is similar with .

Since all the value in the array ranges from [1, n]. They are all positive. Each time when we loop the number, we can use the value as index to set the corresponding position value as negative.

And the number is the duplicated one if we use the index to get the value is negative.  Find the duplicated.

Loop the array again, if the value is positive, the index is the missing number.

Time complexity is O(2 * n), space complexity is O(1).

class Solution {
public:
    vector<int> findErrorNums(vector<int>& nums) {
        vector<int> error;
        for(auto idx : nums){
            if(nums[abs(idx) - 1] > 0){
                nums[abs(idx) - 1] = -nums[abs(idx) - 1];
            } else {
                error.push_back(abs(idx));
            }
        }
        for(int i = 0; i < nums.size(); i++){
            if(nums[i] > 0){
                error.push_back(i + 1);
            }
        }
        return error;
    }
};

Solution two: Set + sum calculation.

We use a set to find the duplicated number.

There is a math equation to calculate the sum, duplicated number and the missing number.

The original sum is 1 + 2 + 3 + ... + n   = sum1;

The missing number array is sum2 = sum1 + duplicated - missing

We just loop once, putting the number in set to find the duplicated, and sum all the numbers to calculate the missing value at the end of loop.

Time complexity is O(n), space complexity is O(n).

class Solution {
public:
    vector<int> findErrorNums(vector<int>& nums) {
        set<int> s;
        int dup = 0;
        int sum = (nums.size() + 1) * nums.size() / 2;
        for(auto num : nums){
            if(s.find(num) != s.end()){
                dup = num;
            }
            sum -= num;
            s.insert(num);
        }
        return {dup, sum + dup};
    }
};
时间: 2024-10-25 02:58:36

645. Set Mismatch的相关文章

645. Set Mismatch - LeetCode

Question 645.?Set Mismatch Solution 思路: 遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数,将其将入结果res中,然后再遍历原数组,如果某个位置上的数字为正数,说明该位置对应的数字没有出现过,加入res中即可 Java实现: public int[] findErrorNums(int[] nums) { /* int a = 0; for (int i : nums) { if (nu

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 arr

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 arr

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

[抄题]: he 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 a

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

leetcode 645. 错误的集合(Set Mismatch)

目录 题目描述: 示例 1: 解法: 题目描述: 集合 S 包含从1到 n 的整数.不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复. 给定一个数组 nums 代表了集合 S 发生错误后的结果.你的任务是首先寻找到重复出现的整数,再找到丢失的整数,将它们以数组的形式返回. 示例 1: 输入: nums = [1,2,2,4] 输出: [2,3] 注意: 给定数组的长度范围是 [2, 10000]. 给定的数组是无序的.

8个节点,每个节点上布置6个ROS,组装都用250,读出都用251,事例率为645.3Hz

组装都用250网段,读出都用251网段.除了黄色部分以外的节点都是cmm03节点. 平均事例率为:645.26Hz, ros所在节点的cpu idle 为17%.

Jar mismatch! Fix your dependencies的解决方案

最近在unbuntu中编译android项目的时候遇到了很多问题,最近遇到的问题是Jar mismatch! Fix your dependencies好像是缺少依赖,要解决其中的依赖关系. 在开发Android项目的时候,有时需要引用多个项目作为library.在引用项目的时候,有时会出现"Jar mismatch! Fix your dependencies"错误.这是因为两个项目的jar包(android-support-v4.jar)不一致. 解决方法是把2个jar都删除,然后