zoukankan      html  css  js  c++  java
  • 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};
        }
    };
  • 相关阅读:
    话说Hibernate和ADO.NET —练习随笔小记
    二次开发WinWebMail邮件系统接口 企业邮件服务器解决方案
    一个Windows后台服务(.Net的C#版) 定时访问数据库循环发送手机短信
    SQL UPDATE 联合表更新的问题
    2009新的篇章,惠海→时代财富→广佛都市网
    在WebService中使用Session或Cookie实现WebService身份验证(客户端是Flex)
    门户网站的形成—CMS内容管理系统
    CSS实现0.5px的边框或线
    《后人诗》
    CentOS6下docker的安装和使用
  • 原文地址:https://www.cnblogs.com/wdw828/p/7226886.html
Copyright © 2011-2022 走看看