2
头图

Find numbers that disappear in all arrays

Problem Description: You are given an array nums of n integers, where nums[i] is in the interval [1, n]. Please find all numbers in the range [1, n] that do not appear in nums and return the result as an array.

For example descriptions, please refer to the official website of LeetCode.

Source: LeetCode
Link: https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Solution 1: Hash method
First, initialize the numbers 1~n into hashSet, and then determine that the elements of the original array nums are removed if they are in hashSset, and the last remaining number is in the range of [1, n] but not in nums. .
Solution 2: In-situ Algorithm
First, traverse the original array, mark the value of the index position corresponding to the element in the corresponding position as a negative number, and finally, traverse the array again, and pick out the non-negative number that is in the range of [1, n] but does not appear in nums. number
 import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class LeetCode_448 {
    /**
     * 哈希法
     *
     * @param nums 原数组
     * @return
     */
    public static List<Integer> findDisappearedNumbers(int[] nums) {
        Set<Integer> numbers = new HashSet<>();
        // 将1~n的数字初始化到hashSet里
        for (int i = 1; i <= nums.length; i++) {
            numbers.add(i);
        }
        // 判断原数组nums的元素如果在hashSset里面,则移除,最后剩下的就是在 [1, n] 范围内但没有出现在 nums 中的数字
        for (int num : nums) {
            if (numbers.contains(num)) {
                numbers.remove(num);
            }
        }
        return new ArrayList<>(numbers);
    }

    /**
     * 原地算法
     *
     * @param nums 原数组
     * @return
     */
    public static List<Integer> findDisappearedNumbers2(int[] nums) {
        // 遍历原数组,将相应位置的元素对应的索引位置的值标记为负数
        for (int i = 0; i < nums.length; i++) {
            int index = Math.abs(nums[i]);
            nums[index - 1] = Math.abs(nums[index - 1]) * -1;
        }
        // 最后,再遍历一次数组,把非负数挑出来即为在 [1, n] 范围内但没有出现在 nums 中的数字
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                result.add(i + 1);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {4, 3, 2, 7, 8, 2, 3, 1};
        // 测试用例,期望输出: 5,6
        System.out.println(findDisappearedNumbers(nums).stream().map(e -> String.valueOf(e)).collect(Collectors.joining(",")));
        System.out.println(findDisappearedNumbers2(nums).stream().map(e -> String.valueOf(e)).collect(Collectors.joining(",")));
    }
}
[Daily Message] Being a person is more important in "practice", work is more important in "professionalism", and learning is more important in "constancy".

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!