Leetcode: 645. Wrong solution to set problem [Super detailed]

topic

The set  s contains   integers from 1 to  . nUnfortunately, due to data errors, a number in the set was copied into the value of another number in the set, causing the set  to lose a number  and  have a duplicate number  .

Given an array  nums representing  S the results of a collection of errors.

Please find the recurring integers, find the missing integers, and return them in the form of an array.

Could it be: simple

topic

Link: 645. Collection of errors

Example 1:

Input: nums = [1,2,2,4]
 Output: [2,3]

Example 2:

Input: nums = [1,1]
 Output: [1,2]

hint:

  • 2 <= nums.length <= 104
  • 1 <= nums[i] <= 104

Question analysis

According to the question, we can know that one element in the set s [1, n] is repeated and one element is missing. The question requires us to return an array (i.e. one repeated element and one missing element). We can first find the repeated elements, and then find the missing elements. The solution is relatively simple this way.

Code display

int* findErrorNums(int* nums, int numsSize, int* returnSize) {
    int* arr = (int*)malloc(sizeof(int) * 2);
    int i = 0; int j = 0;
    //找出复的数组元素
    for (i = 0; i < numsSize; i++)
    {
        for (j = i + 1; j < numsSize; j++)
        {
            if (nums[i] == nums[j])
            {
                arr[0] = nums[i];
            }
        }
    }
    //找出丢失的元素
    int arr2[10001] = { 0 };//创建临时数组
    //元素置零
    for (i = 0; i < numsSize; i++)
    {
        arr2[i] = 0;
    }
    int x = 0;
    for (i = 0; i < numsSize; i++)
    {
        x = nums[i] % (numsSize + 1);
        arr2[x - 1] = x;
    }
    j = 0;
    for (i = 1; i <= numsSize; i++)
    {
        if (arr2[i - 1] == 0)//遍历临时数组
        {
            arr[1] = i;
        }
    }
    *returnSize = 2;
    return arr;
}

 Super detailed analysis of the question

1. Find the repeated elements

What is used here is a two-layer for loop. The outer loop traverses the first element, and the initial condition of the inner loop is the second element.

This is our idea, which is to compare the first element with the second element and the third element; and then compare the next element with other elements below (until a duplicate element is found)

Illustration

2. Find the missing elements 

 The missing element sought here is that the simple idea is to use a temporary array (all elements in the array are set to 0) to store the array elements [1, n] into the corresponding array (that is, with the corresponding array subscript Correspondingly) [For example] Element 1 is stored in temporary array subscript 1; element 5 is placed in temporary array subscript 5.

Then traverse the array of size numsSize. Which element is zero (because only 1 element is missing) is the missing array.

If the above text here is very understandable, you can read it. 

Leetcode:【448. Find all missing numbers in the array】Solution

Illustration

Guess you like

Origin blog.csdn.net/qq_72505850/article/details/132595170