[LeetCode] 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:

  1. The given array size will in the range [2, 10000].
  2. The given array's numbers won't have any order.

这道题给了我们一个长度为n的数组,说里面的数字是从1到n,但是有一个数字重复出现了一次,从而造成了另一个数字的缺失,让我们找出重复的数字和缺失的数字。那么最直接的一种解法就是统计每个数字出现的次数了,然后再遍历次数数组,如果某个数字出现了两次就是重复数,如果出现了0次,就是缺失数,参见代码如下:

解法一:

public:
    vector<int> findErrorNums(vector<int>& nums) {
        vector<int> res(2, 0), cnt(nums.size(), 0);
        for (int num : nums) ++cnt[num - 1];
        for (int i = 0; i < cnt.size(); ++i) {
            if (res[0] != 0 && res[1] != 0) return res;
            if (cnt[i] == 2) res[0] = i + 1;
            else if (cnt[i] == 0) res[1] = i + 1;
        }
        return res;
    }
};

我们来看一种更省空间的解法,这种解法思路相当巧妙,遍历每个数字,然后将其应该出现的位置上的数字变为其相反数,这样如果我们再变为其相反数之前已经成负数了,说明该数字是重复数,将其将入结果res中,然后再遍历原数组,如果某个位置上的数字为正数,说明该位置对应的数字没有出现过,加入res中即可,参见代码如下:

解法二:

public:
    vector<int> findErrorNums(vector<int>& nums) {
        vector<int> res(2, -1);
        for (int i : nums) {
            if (nums[abs(i) - 1] < 0) res[0] = abs(i);
            else nums[abs(i) - 1] *= -1;
        }
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] > 0) res[1] = i + 1;
        }
        return res;
    }
}; 

下面这种方法也很赞,首先我们把乱序的数字放到其正确的位置上,用while循环来不停的放,直到该数字在正确的位置上,那么一旦数组有序了,我们只要从头遍历就能直接找到重复的数字,然后缺失的数字同样也就知道了,参见代码如下:

解法三:

public:
    vector<int> findErrorNums(vector<int>& nums) {
        for (int i = 0; i < nums.size(); ++i) {
            while (nums[i] != nums[nums[i] - 1]) swap(nums[i], nums[nums[i] - 1]);
        }
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] != i + 1) return {nums[i], i + 1};
        }
    }
};

参考资料:

https://discuss.leetcode.com/topic/96808/java-o-n-time-o-1-space

https://discuss.leetcode.com/topic/97091/c-6-lines-solution-with-explanation

本文转自博客园Grandyang的博客,原文链接:[LeetCode] Set Mismatch 设置不匹配

,如需转载请自行联系原博主。

相关文章
|
26天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 内置命令】⭐⭐⭐Linux 设置或修改shell环境变量set命令 使用指南
【Shell 命令集合 系统设置 内置命令】⭐⭐⭐Linux 设置或修改shell环境变量set命令 使用指南
29 0
|
8月前
|
Shell
10.4.4 终端机的环境设置: stty, set
10.4.4 终端机的环境设置: stty, set
67 0
|
10月前
|
测试技术 Windows
IDEA的LeetCode插件的设置和使用
IDEA的LeetCode插件的设置和使用
633 0
|
11月前
|
关系型数据库 MySQL 数据安全/隐私保护
mysql_config_editor 设置密码set --login_path
mysql_config_editor可以给指定的连接和密码生成一个加密文件.mylogin.cnf
103 0
|
12月前
设计一个长方形类,成员变量包括长度和宽度,成员函数除包括计算周长和计算面积外,还包括用 Set 方法设置长和宽,以及用 get 方法来获取长
设计一个长方形类,成员变量包括长度和宽度,成员函数除包括计算周长和计算面积外,还包括用 Set 方法设置长和宽,以及用 get 方法来获取长
137 0
|
存储 PHP
PHP为什么需要设置session_set_cookie_params()函数来配置session的cookie参数?
PHP为什么需要设置session_set_cookie_params()函数来配置session的cookie参数?
102 0
|
关系型数据库 MySQL
Mysql外键设置中的CASCADE、NO ACTION、RESTRICT、SET NULL
Mysql外键设置中的CASCADE、NO ACTION、RESTRICT、SET NULL
125 0
Mysql外键设置中的CASCADE、NO ACTION、RESTRICT、SET NULL
|
移动开发 JSON JavaScript
weex-使用Vue.set设置属性和使用this.xxx设置属性的区别
weex-使用Vue.set设置属性和使用this.xxx设置属性的区别
89 0
weex-使用Vue.set设置属性和使用this.xxx设置属性的区别
|
存储 索引
LeetCode 73. Set Matrix Zeroes
给定一个m * n 的矩阵,如果当前元是0,则把此元素所在的行,列全部置为0. 额外要求:是否可以做到空间复杂度O(1)?
76 0
LeetCode 73. Set Matrix Zeroes
LeetCode 524. 通过删除字母匹配到字典里最长单词
LeetCode 524. 通过删除字母匹配到字典里最长单词
57 0

热门文章

最新文章