First week (2021 / 01 / 18 ~ 24)

Time:2021-4-18

Simply record the following C + + knowledge points and algorithms used in this week’s brush questions.

Knowledge point 1: XOR algorithm\(\bigoplus\)

concept

If the two corresponding bits of the two values participating in the operation are the same, the result is 0, otherwise it is 1, and the C + + operation symbol is 0^
such as
0^ 0=0, 1^ 0=1, 0^ 1=1, 1^1=0

nature

1. Any number and 0 do XOR operation, the result is still the original number, that is\(a \oplus 0=a\)
2. Any number and itself do XOR operation, the result is 0, that is\(a \oplus a=0\)
3. XOR operation satisfies commutative law and associative law
\(a \oplus b \oplus a=b \oplus a \oplus a=b \oplus (a \oplus a)=b \oplus0=b\)
Based on the above properties, look at the title leetcode 136
Each element in the given array can be operated by XOR operation. When the number of elements is 2, XOR operation can be performed(\(\oplus\))The result is 0
0 and each element XOR operation, the result is 0, so the final result is only 1 elements.

Knowledge point 2: C + + STL hash table (unordered)_ map)

Reference blog C + + STL hash table | unordered_ map

summary

Hash table is a data structure that can be accessed directly according to the key value. In other words, it accesses records by mapping key values to a position in the table to speed up the search. This mapping function is called hash function.
template:key And T are required, corresponding to the key and value in HashMap respectively

template < class Key,                                    // unordered_map::key_type
           class T,                                      // unordered_map::mapped_type
           class Hash = hash,                       // unordered_map::hasher
           class Pred = equal_to,                   // unordered_map::key_equal
           class Alloc = allocator< pair >  // unordered_map::allocator_type
           > class unordered_map;

In STL, the data structure of map is red black tree. Red black tree is a kind of balanced binary search tree, in which the data isOrderYes. The time complexity of searching red black tree is O (logn).
unordered_ Map corresponds to hash table, which is characterized by high efficiency, constant time complexity O (1), and much higher extra space complexity. Therefore, we use unordered to query efficiently_ Map container. If the memory size is sensitive or the data storage is ordered, you can use the map container. unorder_ Map isdisorderSo in C + +, pointer is usually used to locate.

usage

Header document to be introduced:

\C++
\Initialization
std::unordered_map map = {{"key","value"}};
\Insert element
Map ['key '] = value; / * map allocates a large space during initialization*/
\Remove element
map.erase ( map.begin ()); / * parameter: pointer to map*/
\Empty element
map.clear();
\Search element: search by key. If it exists, the pointer corresponding to the key will be returned. If it does not exist, the pointer to the last unit + 1 of the map will be returned( map.end ())
map.find('key')

Knowledge point 3: vector < > container

Analysis of C + + vector container of reference blog

summary

It can be simply considered that a vector is a dynamic array that can store any type, similar to the array list in Java and python.

usage

Common usage
1.push_ Back () adds a data at the end of the array

2.pop_ Back () removes the last data from the array

3. Size () the size of the currently used data

4. Reserve changes the space allocated by vecotr,The method has no return value

5. Erase delete the data item pointed by the pointer

6. Empty to determine whether the vector is empty

7. Clear clear clear the current vector

8. At () to get the number position data

9. Begin() gets the pointer to the array header

10. End() gets the pointer to the last cell + 1 of the array

11. Front() gets the reference of the array header

12. Back() gets the reference of the last cell of the array

13. Swap exchanges data with another vector

Knowledge point 4: binary search

concept

Binary search is applied to the sorted array. Each time, the number in the middle of the array is searched and compared with the number to be searched. The subscript of the number in the middle of the array can be divided into the following two cases: assume that the length of the array is n;

(1)When n is even, the middle number is the subscript\((\frac{0+n}{2}-1)、(\frac{0+n}{2})\)The average value of the sum of the two;
(2)When n is odd, the middle number is the subscript\(\frac{n+1}{2}\)The value of.
Example: leetcode 4

usage

Time complexity: o(\(n\ln(n)\))

/*
*Implementation of C + + code
*/
public binarySearch(vector nums,int target)
{
	int low = 0;
	int high = num.size();
	int mid = (low+high)/2
	while(low<=nums.size()-1&&high<=nums.size()
	&&low<=high)
	{
	mid = (low+high)/2
	if(nums[mid]=target){return mid;}
	else if(nums[mid]

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]