Missing number in Java circle

preface Learn the application of heap arrangement, hash array, hashMap and XOR through the number lost in the circle. 1,...
1. Title
2. Problem solution

preface

Learn the application of heap arrangement, hash array, hashMap and XOR through the number lost in the circle.

1, Number lost in circle

1. Title

Given an array num containing N numbers in [0, n], find the number that does not appear in the array within the range of [0, n].

Example 1:
Input: num = [3,0,1]
Output: 2
Explanation: n = 3, because there are 3 numbers, all numbers are in the range [0,3]. 2 is the missing number because it does not appear in nums.
Example 2:
Input: num = [0,1]
Output: 2
Explanation: n = 2, because there are 2 numbers, all numbers are in the range [0,2]. 2 is the missing number because it does not appear in nums.
Example 3:
Input: num = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9, because there are 9 numbers, all numbers are in the range [0,9]. 8 is the missing number because it does not appear in nums.
Example 4:
Input: num = [0]
Output: 1
Explanation: n = 1, because there is 1 number, all numbers are in the range [0,1]. 1 is a missing number because it does not appear in nums.

2. Problem solution

A. Stack row

Pile up, solve problems quickly and warm up.
Idea) first build the heap with all the numbers, and then take the small root heap top elements to judge whether the heap top elements are orderly. When there is no order, the missing elements are found.

public int missingNumber(int[] nums) { //Quick solution is sorting PriorityQueue<Integer> p = new PriorityQueue<>(); for (int i = 0; i < nums.length; i++) { p.offer(nums[i]); } int count = 0; while (!p.isEmpty()) { int pre = p.poll(); if (pre != count++) return pre - 1; } return nums.length; }

B. XOR

Preparation knowledge)
1) Commutative law: a ^ b ^ c = a ^ c ^ b
2)0 ^ n = n
3)n ^ n = 0
a ^ b ^ c ^ b ^ c = a ^ b ^ b ^ c ^ c = a ^ 0 ^ 0 = a
Idea) all numbers in nums and 0-n XOR are used once to get the missing numbers.
Example) [3,1,0], 3 ^ 1 ^ 0 ^ (0 ^ 1 ^ 2 ^ 3) = 0 ^ 0 ^ 1 ^ 2 ^ 3 ^ 3 = 0 ^ 0 ^ 2 ^ 0 = 2

//XOR public int missingNumber2(int[] nums) { //XOR int res = 0; for (int i = 0; i < nums.length; i++) { res ^= nums[i]; res ^= i; } return res ^ nums.length; }

C. Summation (mathematical thinking)

Find the rules and use mathematical equations to reduce space overhead.
Idea) regard 1 to n as an arithmetic sequence, get the sum of the arithmetic sequence, and then subtract the sum in nums to be equal to the missing number.

//Sum public int missingNumber3(int[] nums) { int len = nums.length; int res = 0; for (int i = 0; i < len; i++) { res += nums[i]; } return len * (len + 1) / 2 - res; }

D. Array hash

Array hash is also marked.
Idea) use the correspondence of array subscripts to mark which elements exist, and finally find the missing number.

//Array hash public int missingNumber4(int[] nums) { int len = nums.length; int[] mark = new int[len + 1]; for (int i = 0; i < len; i++) { mark[nums[i]] = 1; } for (int i = 0; i <= len; i++) { if (mark[i] == 0) return i; } return len + 1; }

E.HashMap

Similar to array hash, pure hash.
(1) record which elements from 0 to n have appeared, and finally find out that the number not recorded from 0 to n is the missing number.

//hashMap public int missingNumber5(int[] nums) { //Use Map to record first, and then find which number has no record. int len = nums.length; Map<Integer, Integer> mark = new HashMap<>(); for (int num : nums) { mark.put(num, 1); } for (int i = 0; i <= len; i++) { if (mark.get(i) == null) return i; } return len + 1; }
summary

1) Heap sorting, fast problem solving.
2) XOR, for this problem of finding numbers.
3) Sum, establish equations with mathematical thinking, and reduce space-time overhead.
4) hash thought, a common problem-solving idea, records first, and then finds it at the speed of O(1).

reference

[1] Original title of Leetcode
[2] Leetcode solution

6 November 2021, 18:28 | Views: 5471

Add new comment

For adding a comment, please log in
or create account

0 comments