|
|
@ -68,13 +68,23 @@ comments: true
|
|
|
|
=== "JavaScript"
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
|
|
```js title="hashing_search.js"
|
|
|
|
```js title="hashing_search.js"
|
|
|
|
|
|
|
|
/* 哈希查找(数组) */
|
|
|
|
|
|
|
|
function hashingSearch(map, target) {
|
|
|
|
|
|
|
|
// 哈希表的 key: 目标元素,value: 索引
|
|
|
|
|
|
|
|
// 若哈希表中无此 key ,返回 -1
|
|
|
|
|
|
|
|
return map.has(target) ? map.get(target) : -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="hashing_search.ts"
|
|
|
|
```typescript title="hashing_search.ts"
|
|
|
|
|
|
|
|
/* 哈希查找(数组) */
|
|
|
|
|
|
|
|
function hashingSearch(map: Map<number, number>, target: number) {
|
|
|
|
|
|
|
|
// 哈希表的 key: 目标元素,value: 索引
|
|
|
|
|
|
|
|
// 若哈希表中无此 key ,返回 -1
|
|
|
|
|
|
|
|
return map.has(target) ? map.get(target) : -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
=== "C"
|
|
|
@ -151,13 +161,23 @@ comments: true
|
|
|
|
=== "JavaScript"
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
|
|
```js title="hashing_search.js"
|
|
|
|
```js title="hashing_search.js"
|
|
|
|
|
|
|
|
/* 哈希查找(链表) */
|
|
|
|
|
|
|
|
function hashingSearch1(map, target) {
|
|
|
|
|
|
|
|
// 哈希表的 key: 目标结点值,value: 结点对象
|
|
|
|
|
|
|
|
// 若哈希表中无此 key ,返回 null
|
|
|
|
|
|
|
|
return map.has(target) ? map.get(target) : null;
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="hashing_search.ts"
|
|
|
|
```typescript title="hashing_search.ts"
|
|
|
|
|
|
|
|
/* 哈希查找(链表) */
|
|
|
|
|
|
|
|
function hashingSearch1(map: Map<number, any>, target: number) {
|
|
|
|
|
|
|
|
// 哈希表的 key: 目标结点值,value: 结点对象
|
|
|
|
|
|
|
|
// 若哈希表中无此 key ,返回 null
|
|
|
|
|
|
|
|
return map.has(target) ? map.get(target) : null;
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
=== "C"
|
|
|
|