## Title Link

1. Two Sum difficulty: $/ color {00965e} {easy}$

## Knowledge points

#### 1. Hash (hash) function

The classic hash function times33 is widely used, and the core algorithm is as follows:

`hash(i) = hash(i-1) * 33 + str[i]`

Laruence has an article about:Hash algorithm in PHP

#### 2. Hash collision processing method

##### 2.1 chain address method (zipper method)（**Widely used**)

One way linked list is used to deal with the key of hash conflict;`PHP and Java use this method`

advantage:

- It is easy to deal with conflicts without accumulation;
- The average search length is short;
- The nodes in the linked list are applied dynamically, which is suitable for the situation that the length of the construction table cannot be determined;
- Compared with the open address method, the pointer field of the zipper method can be ignored, so it saves more space than the open address method.
- It is convenient to insert and delete. The insertion node should be at the beginning of the chain. It is convenient to delete the node. Only adjust the pointer and do not need to adjust other conflict elements.
Tips: if the linked list is too long, the query efficiency will change from O (1) to traversing the linked list, and the efficiency will be reduced. At this time, the tree structure can be used instead of the linked list to improve the query efficiency.

This leads to another knowledge pointBinary treeAndBinary search tree）I will not repeat here.

##### 2.2 open addressing

`Hi=(H(key)+di) MOD m i=1,2,…,k(k<=m-1)`

Among them,`m`

Is the length of the hash table.`di`

It’s the incremental sequence at the time of conflict. If`di`

Value may be`1,2,3,…m-1`

It is called linear probe hashing.

If`di`

take`1`

After each conflict, move backward`1`

Location, scale**Linear probe hashing**；

If`di`

The value may be`1,-1,2,-2,4,-4,9,-9,16,-16,…k*k,-k*k(k<=m/2)`

, said**Second probe hashing**；

If`di`

The value may be a pseudo-random sequence. call**Pseudo random probe hashing**。

Disadvantages:

- It is easy to deal with conflicts without accumulation;
- The average search length is short;

##### 2.3 rehash

When a conflict occurs, the second, third, and hash functions are used to calculate the address until there is no conflict. Disadvantages: increased computing time. For example, for the first time, the first hash is based on the first letter of the last name. If there is a conflict, the second digit of the first letter of the last name can be hashed, and then the third digit will be collided until there is no conflict. This method is not easy to generate aggregation, but it increases the calculation time.

##### 2.4 establish a public overflow area

Suppose the value range of hash function is [0, M-1], then the vector hashtable [0.. M-1] is set as the basic table, and the storage space vector overtable [0.. v] is set to store the conflicting records.

## solution

### 1. The double cycle of violence is ignored;

### 2. Hash table (bucket);

For array a, construct a hash, and record the value and array subscript when traversing the array for the first time. During traversal, first judge whether there is target – a [i] in the hash table. If so, just return it directly;

##### Complexity analysis

- Time complexity: O (n), where n is the number of elements in the array. For each element x, we can find target – x O (1).
- Spatial complexity: O (n), where n is the number of elements in the array. It is mainly the cost of hash table.

C language has no hashtable structure, so it needs to implement hashtable by itself (knowledge point: how to implement hashtable insertion / deletion). Here, PHP demo code is used to implement it. Other languages are similar and will not be discussed here;

```
class Solution {
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer[]
*/
function twoSum($nums, $target) {
$len = count($nums);
$table = [];
$result = [];
//The underlying implementation of PHP array is the hash table. Here, it is not the same as C to calculate the minimum value, which is a shuttle
for($i=0;$i < $len;$i++) {
//First judge whether there is, if any, return the subscript directly
if (isset($table[$target - $nums[$i]])) {
return [$table[$target - $nums[$i]],$i];
}else{
//Otherwise, the number in hash = > subscript
$table[$nums[$i]] = $i;
}
}
}
}
```