This data structure contains an integer Variable n (indicating the number of current elements) and two arrays members and position. The former is used to store the elements in the current set, and the latter is an array with a length of N, used to record the position of each number in the members array (in other words, members [position [I] I is always true ). To query whether m is in the current status
This data structure contains an integer Variable n (indicating the number of current elements) and two arrays members and position. The former is used to store the elements in the current set, and the latter is an array with a length of N, used to record the position of each number in the members array (in other words, members [position [I] = I is always true ). To query whether m is in the current status
This data structure contains an integer Variable n (indicating the number of current elements) and two arrays members and position. The former is used to store the elements in the current set, and the latter is an array with a length of N, used to record the position of each number in the members array (in other words, members [position [I] = I is always true ).
To query whether m is in the current set, you only need to check whether position [m] is in the range of 0 to n-1, and whether members [position [m] is indeed equal to m. To add an element, you only need to put the new element into members [n ++] and update the corresponding position data. To delete an element, you only need to move it to the end of the members Queue (change the nth Number of the element and the members array), update the corresponding position data, and then subtract n. To clear a set, you only need to make n equal to 0. The traversal element only needs to scan the currently valid section in the members array, which is obviously O (n. Variable n is the number of elements. If you need to query the number of elements, return n directly.