LeetCode Set Mismatch

原题链接在这里:https://leetcode.com/problems/set-mismatch/description/

题目:

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.

题解:

Find the Duplicate Number类似. 把Math.abs(nums[i])-1 当index找array中对应的值,如果为正数就改成负数。若已经是负数,说明之前这个位置已经被flip过了, 所以这个index+1就是dumplicate.

再iterate nums array, 若遇到没有变负的值就说明该位置缺失.

Time Complexity: O(nums.length). Space: O(1).

AC Java:

 1 class Solution {
 2     public int[] findErrorNums(int[] nums) {
 3         if(nums == null || nums.length < 2){
 4             throw new IllegalArgumentException("Input array is not valid.");
 5         }
 6
 7         int [] res = new int[2];
 8
 9         for(int n : nums){
10             if(nums[Math.abs(n)-1] < 0){
11                 res[0] = Math.abs(n);
12             }else{
13                 nums[Math.abs(n)-1] *= -1;
14             }
15         }
16
17         for(int i = 0; i<nums.length; i++){
18             if(nums[i] > 0){
19                 res[1] = i+1;
20             }
21         }
22         return res;
23     }
24 }

使用bit manipulation, 把nums中的每个数和[1,n]区间的每个数XOR, 得到的res就是 x XOR y, x 和 y分别是duplicate 和 missing number.

res最右侧为1的bit上 x 和 y的值时不同的, 根据这点把 nums分成两组, 把[1,n]区间内的数也分成两组, 一组是这个bit位上是0, 另一组是这个bit位上是1.

把对应的组互相XOR就能找出x 和 y的具体值. 最后能在nums中出现的就是duplicate number, 另一个就是misssing number.

Time Complexity: O(num.length). Space: O(1).

AC Java:

 1 class Solution {
 2     public int[] findErrorNums(int[] nums) {
 3         if(nums == null || nums.length < 2){
 4             throw new IllegalArgumentException("Input array is not valid.");
 5         }
 6
 7         int xor = 0;
 8         int xor1 = 0;
 9         int xor2 = 0;
10         for(int n : nums){
11             xor ^= n;
12         }
13         for(int i = 1; i<=nums.length; i++){
14             xor ^= i;
15         }
16
17         int bit = xor & ~(xor-1);
18         for(int n : nums){
19             if((bit&n) == 0){
20                 xor1 ^= n;
21             }else{
22                 xor2 ^= n;
23             }
24         }
25         for(int i = 1; i<=nums.length; i++){
26             if((bit&i) == 0){
27                 xor1 ^= i;
28             }else{
29                 xor2 ^= i;
30             }
31         }
32
33         for(int n : nums){
34             if(xor1 == n){
35                 return new int[]{xor1, xor2};
36             }
37         }
38         return new int[]{xor2, xor1};
39     }
40 }
时间: 2024-12-23 15:40:01

LeetCode Set Mismatch的相关文章

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

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

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

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

(Easy) Set Mismatch -LeetCode

Description: 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.

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个