LeetCode 645. 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 }
原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/7529969.html