Force buckle 137. The number II that appears only once + 260. The number III that appears only once

Topic 1

Force buckle: 137. The number II appears only once

thinking

Hash algorithm

We do not consider anything. We directly count the number of occurrences of each number, and then find the one that occurs once. It is implemented with a hash table. It is the most basic and best to think about.

Bit operation (traversal Statistics)

Well, now we have a new demand. For some reason, the space is seriously insufficient. Let's calculate it in the space of O(1), so we think of bit operation.
1. Because most of them appear m times, and the target (only one) appears n times, m and N are both odd or even, and M > n. Because of the characteristics of M and N, XOR cannot be used (only the target appears odd times, most scores appear even times, and the direct XOR can retain the target). Therefore, the direct decimal method is invalid, so sneak into the binary bit by bit.
2. Take {3,3,3,5} as an example, as follows:

<1> Through the above figure, it is not difficult to find that, for example, for the first place, because 3 appear three times in a row, that is, there will be three 1s in a row. Our goal is to keep the last 1. What can we do to kill the three 1s in a row?
<2> Very simply, add all the first bits, and the elements that appear three times are multiples of 3. Then the target appears n times (n < m) and naturally becomes the remainder, so% will get the target.
(if n=1, then directly% m is OK, but when n > 1 & & n < m, finally get% m, and then / N)

To sum up: the above method is applicable to the solution of any problem that "most of them occur m times, the target (only one) occurs n times, m and N are both odd or even, and M > n"

Automata solution

(this method is more efficient and evolved from the above bit operation problem, which is more difficult)
(I will draw a picture to illustrate the analysis method)
We observe the above bit operation method. For a bit, the state will change every time a 1 is received. When three 1s are received, it will return to the origin, so we can use finite state automata to simulate this process.

1. Because int has 32 bits in total, each bit has the same algorithm. Finally, it can be directly integrated. The following analysis is based on a certain bit.

2,




The above is the complete analysis process.

code

1,Hash

class Solution {
public:
	int singleNumber(vector<int>& nums) {
		unordered_map<int, int> item;
		int ans = 0;
		for (int i = 0; i < nums.size(); i++) {
			item[nums[i]]++;//Statistics
		}
		for (int i = 0; i < nums.size(); i++) {
			if (item[nums[i]] == 1) {//seek
				ans = nums[i];
			}
		}
		return ans;
	}
};

All codes pass the buckle test
(after multiple tests, the shortest time is):

Time complexity: O(N)
Space complexity: O(N)

2. Bit operation

class Solution {
public:
	int find_single_num(vector<int> nums, int remainder) {//Incoming remainder
		int bits[32] = {};
		for (int i = 0; i < nums.size(); i++) {
			for (int j = 0; j < 32; j++) {//Count the sum of each bit
				bits[j] += nums[i] & 1;
				nums[i] >>= 1;
			}
		}
		for (int i = 0; i < 32; i++) {//Take mold
			bits[i] %= remainder;
		}
		int ans = 0;
		for (int i = 0; i < 32; i++) {//reduction

			ans |= bits[31 - i];
			ans <<= 1;
		}
		return ans;
	}
	int singleNumber(vector<int>& nums) {
		return find_single_num(nums, 3);
	}
};

All codes pass the buckle test
(after multiple tests, the shortest time is):

Time complexity: O(N)
Space complexity: O(1)

3. Automata

class Solution {
public:
	int singleNumber(vector<int>& nums) {
		int ones = 0, twos = 0;//It consists of 32 one and two respectively
		for (int tmp : nums) {//Equivalent to 32-bit concurrent operation
			ones = ones ^ tmp&~twos;
			twos = twos ^ tmp&~ones;
		}
		return ones;
	}
};

All codes pass the buckle test
(after multiple tests, the shortest time is):

Time complexity: O(N)
Space complexity: O(1)

Topic 2

Force buckle: 260. The number III that appears only once

Idea: bit operation

Type of this question: most of them appear m times, and the target appears n times. M is an even number and N is an odd number!
1. First review n^n=0, 0 ^ n =n. therefore, even numbers are mutually exclusive or 0, and odd numbers are mutually exclusive or to get themselves.
2. Therefore, in this way, there is no need to use the bit operation algorithm in the previous question. There is a more convenient method. According to "2" above, we put all the numbers together, XOR, and kill all the non target directly, leaving only the target.
3. If the goal is the only one, get the answer directly.
4. Like this question, there are two goals, so we have to face a problem, that is, to separate the two numbers. To separate, we need to find different points, but now we get the XOR result of two goals. How to judge the difference?
<1> Therefore, the first bit in the XOR result is 1, which represents that the two target numbers are inconsistent in this bit, one is 0 and the other is 1
<2> Now that we know that the k-th bit is inconsistent, we all XOR again, but now I know how to separate the target, that is, the k-th bit, one is 0 and one is 1, so we put the 0 in a pile and the 1 in a pile to separate

code

Note: be sure to manually add brackets for priority to prevent errors!!!!!

class Solution {
public:
	vector<int> singleNumber(vector<int>& nums) {
		vector<int> answers(2);
		int item = 0;
		for (int tmp : nums) {
			item ^= tmp;
		}
		int ans1 = 0, ans2 = 0;
		int point = 1;//Find the bit number. The binary numbers of the two targets are inconsistent, that is, 1
		while (1) {
			if ((item & point) != 0) {//&If the operation priority is low, brackets must be added
				break;
			}
			point <<= 1;//Keep going left
		}
		for (int tmp : nums) {//Group XOR to separate the two targets
			if ((tmp & point) == 0) {
				ans1 ^= tmp;
			}
			else {
				ans2 ^= tmp;
			}
		}
		answers[0] = ans1;
		answers[1] = ans2;
		return answers;
	}
};

All codes pass the buckle test
(after multiple tests, the shortest time is):

Time complexity: O(N)
Space complexity: O(1)

Tags: Algorithm leetcode array

Posted on Fri, 26 Nov 2021 12:16:38 -0500 by Arya