First. On display at some of nature "exclusive OR"
The exclusive OR operation based on the binary bit, XOR or symbol ^ indicates which algorithm is the number of sides of each operator a bit, take the same value 0, 1 take different values.
It differs in that the Boolean operation, when the operator both are 1, the result is the result of a Boolean operation, the XOR operation is zero.
nature
1, commutative
2, associativity (i.e., (a ^ b) ^ c == a ^ (b ^ c))
3, for any number x, have x ^ x = 0, x ^ 0 = x
4, the self reaction of A ^ B ^ B = A ^ 0 = A
application
One, two exchange values with integers instead of the third parameter
A =. 9;
B =. 11;
A = A ^ B; 1011 = 0010 1001 ^
B ^ A = B; 0010 = 1001 1011 ^
A ^ B = A ; 0010 ^ 1001 = 1011
A =. 11;
B =. 9;
Second, the parity judgment
Third, determine the two numbers are equal
a ^ b == 0 ?
Fourth, the Gray code (Gray code)
V. odd division
Second. Leetcode136. Appears only once digital
Given a non-empty array of integers, in addition to an element appears only once, the rest of each element appears twice. To find out that only appears once in the elements.
Description:
Your algorithm should have linear time complexity. You can not use the extra space to achieve it?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
Answer the following codes:
/ * Given a non-empty array of integers once except that an element appears only remaining each element appears twice to find out that there was only one element * / / * ------------ ----------------------------head File-------------------- ---------------------------------- * / #include <the iostream> #include <Vector> #include <unordered_map > the using namespace STD; / * ------------------------------------- using an exclusive property or - --------------------------------------------- * / class Solution1 { public : int singleNumber (Vector < int > & the nums) { int RES = 0; For ( int I = 0 ; I <nums.size (); I ++ ) { RES ^ = the nums [I]; // using the XOR of the same nature as 0, is different from 1. 0 ^ X = XX ^ X 0 = } return RES; } }; / * --------------------------------------- - use hash table -------------------------------------------- --- * / class Solution2 { public : int singleNumber (Vector < int > & the nums) { unordered_map < int , int > mm; for (auto n : nums) { if (mm.count(n)) { mm[n]++; } else { mm.emplace(n, 1); } } for (auto n : nums) { if (mm[n] == 1) { return n; } } } }; /*--------------------------------------程序结束----------------------------------------------------*/
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/single-number
Third. Leetcode136. Ⅱ numbers appear only once
Given a non-empty array of integers, in addition to an element appears only once, the rest of each element appears three times. To find out that only appears once in the elements.
Description:
Your algorithm should have linear time complexity. You can not use the extra space to achieve it?
Example 1:
Input: [qCW]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/single-number-ii
About XOR, OR, NOT operation has the following properties:
0 X = X ^, X ^ X = 0, X = X & ~ 0, ~ X & X = 0.
-
After the first occurrence of x, the following procedure can be obtained through a = x, b = 0;
-
X After the second time, the following procedure can be obtained through a = 0, b = x;
-
X After the third time, the following procedure can be obtained through a = 0, b = 0;
The number appears only once, after the program to give a = x, b = 0; and finally it is the function returns a.
Answer the following codes:
/ * Given a non-empty array of integers, in addition to an element appears only once outside of each of the remaining elements have appeared three times to find out that there was only one element * / #include <iostream> #include <the Vector> a using namespace std ; class Solution2to1 { public : int singleNumber (Vector < int > & the nums) { int A = 0 , B = 0 ; for (Auto NUM: the nums) { A = (A ^ NUM) & ~ B; B = (B ^ NUM) & ~ A; } return A; } };
Forth. Leetcode136. Ⅲ numbers appear only once
Given an array of integers nums, of which there are exactly two elements appear only once, and the remaining elements all appear twice. Find out only once those two elements.
Example:
Input: [1,2,1,3,2,5]
Output: [3,5]
Note:
The results output order is not important, for the example above, [5, 3] is the correct answer.
Your algorithm should have linear time complexity. You can be implemented using only constant space complexity?
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/single-number-iii
The exclusive property or
If only two numbers is the number 1 appears in the array, respectively A and B, other number of occurrences is 2, then the first step is first of all exclusive data directly or eventually obtained result of A ^ B!
Then find A ^ num & flag number all = 0 and B bits for local 1, obtained by flag << 1, so that it may be the entire array is divided into two portions, a first portion comprising num & flag number all = 1, and the other part comprising .
The trick of:
If A 1001 B 1101
A^B = 0010
Beginning flag = 1; 1 a value obtained by 0010 while loop and left <<
And 0010, respectively, and A and B above to give bitwise 0 and 1, can be divided into two arrays so that
Code Answer:
/ * Given an integer array nums in which exactly two elements appear only once all other elements are present twice to find out only once those two elements * / #include <iostream> #include <the Vector> a using namespace std; class Solution3to1 { public : Vector < int > singleNumber (Vector < int > & the nums) { int RES = 0 ; for (Auto NUM: the nums) { RES ^ = NUM; } int In Flag = . 1 ; the while ((RES & In Flag) ==0) { flag = flag << 1; } vector<int> result(2, 0); for (auto num : nums) { if (num & flag == 1) { result[0] ^= num; } else { result[1] ^= num; } } return result; } };
Have questions please leave a message exchange to learn from each other Ha! ! ! !