This commit is contained in:
krahets
2023-04-09 04:34:58 +08:00
parent adcbab4d4c
commit 01d05cc1f0
26 changed files with 1501 additions and 1247 deletions
+12 -12
View File
@@ -130,18 +130,18 @@ comments: true
}
```
再比如,如果我们想要给定一个目标点值 `target` ,获取对应的链表点对象,那么也可以使用哈希查找实现。
再比如,如果我们想要给定一个目标点值 `target` ,获取对应的链表点对象,那么也可以使用哈希查找实现。
![哈希查找链表点](hashing_search.assets/hash_search_listnode.png)
![哈希查找链表点](hashing_search.assets/hash_search_listnode.png)
<p align="center"> Fig. 哈希查找链表点 </p>
<p align="center"> Fig. 哈希查找链表点 </p>
=== "Java"
```java title="hashing_search.java"
/* 哈希查找(链表) */
ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
// 哈希表的 key: 目标点值,value: 点对象
// 哈希表的 key: 目标点值,value: 点对象
// 若哈希表中无此 key ,返回 null
return map.getOrDefault(target, null);
}
@@ -152,7 +152,7 @@ comments: true
```cpp title="hashing_search.cpp"
/* 哈希查找(链表) */
ListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> map, int target) {
// 哈希表的 key: 目标点值,value: 点对象
// 哈希表的 key: 目标点值,value: 点对象
// 若哈希表中无此 key ,返回 nullptr
if (map.find(target) == map.end())
return nullptr;
@@ -165,7 +165,7 @@ comments: true
```python title="hashing_search.py"
def hashing_search_linkedlist(mapp: dict[int, ListNode], target: int) -> ListNode | None:
""" 哈希查找(链表) """
# 哈希表的 key: 目标元素,value: 点对象
# 哈希表的 key: 目标元素,value: 点对象
# 若哈希表中无此 key ,返回 None
return mapp.get(target, None)
```
@@ -175,7 +175,7 @@ comments: true
```go title="hashing_search.go"
/* 哈希查找(链表) */
func hashingSearchLinkedList(m map[int]*ListNode, target int) *ListNode {
// 哈希表的 key: 目标点值,value: 点对象
// 哈希表的 key: 目标点值,value: 点对象
// 若哈希表中无此 key ,返回 nil
if node, ok := m[target]; ok {
return node
@@ -190,7 +190,7 @@ comments: true
```javascript title="hashing_search.js"
/* 哈希查找(链表) */
function hashingSearchLinkedList(map, target) {
// 哈希表的 key: 目标点值,value: 点对象
// 哈希表的 key: 目标点值,value: 点对象
// 若哈希表中无此 key ,返回 null
return map.has(target) ? map.get(target) : null;
}
@@ -201,7 +201,7 @@ comments: true
```typescript title="hashing_search.ts"
/* 哈希查找(链表) */
function hashingSearchLinkedList(map: Map<number, ListNode>, target: number): ListNode | null {
// 哈希表的 key: 目标点值,value: 点对象
// 哈希表的 key: 目标点值,value: 点对象
// 若哈希表中无此 key ,返回 null
return map.has(target) ? (map.get(target) as ListNode) : null;
}
@@ -220,7 +220,7 @@ comments: true
ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target)
{
// 哈希表的 key: 目标点值,value: 点对象
// 哈希表的 key: 目标点值,value: 点对象
// 若哈希表中无此 key ,返回 null
return map.GetValueOrDefault(target);
}
@@ -231,7 +231,7 @@ comments: true
```swift title="hashing_search.swift"
/* 哈希查找(链表) */
func hashingSearchLinkedList(map: [Int: ListNode], target: Int) -> ListNode? {
// 哈希表的 key: 目标点值,value: 点对象
// 哈希表的 key: 目标点值,value: 点对象
// 若哈希表中无此 key ,返回 null
return map[target]
}
@@ -242,7 +242,7 @@ comments: true
```zig title="hashing_search.zig"
// 哈希查找(链表)
fn hashingSearchLinkedList(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) {
// 哈希表的 key: 目标点值,value: 点对象
// 哈希表的 key: 目标点值,value: 点对象
// 若哈希表中无此 key ,返回 null
if (map.getKey(target) == null) return null;
return map.get(target);
+17 -17
View File
@@ -167,7 +167,7 @@ comments: true
}
```
再比如,我们想要在给定一个目标点值 `target` ,返回此点对象,也可以在链表中进行线性查找。
再比如,我们想要在给定一个目标点值 `target` ,返回此点对象,也可以在链表中进行线性查找。
=== "Java"
@@ -176,12 +176,12 @@ comments: true
ListNode linearSearchLinkedList(ListNode head, int target) {
// 遍历链表
while (head != null) {
// 找到目标点,返回之
// 找到目标点,返回之
if (head.val == target)
return head;
head = head.next;
}
// 未找到目标点,返回 null
// 未找到目标点,返回 null
return null;
}
```
@@ -193,12 +193,12 @@ comments: true
ListNode* linearSearchLinkedList(ListNode* head, int target) {
// 遍历链表
while (head != nullptr) {
// 找到目标点,返回之
// 找到目标点,返回之
if (head->val == target)
return head;
head = head->next;
}
// 未找到目标点,返回 nullptr
// 未找到目标点,返回 nullptr
return nullptr;
}
```
@@ -210,10 +210,10 @@ comments: true
""" 线性查找(链表) """
# 遍历链表
while head:
if head.val == target: # 找到目标点,返回之
if head.val == target: # 找到目标点,返回之
return head
head = head.next
return None # 未找到目标点,返回 None
return None # 未找到目标点,返回 None
```
=== "Go"
@@ -223,7 +223,7 @@ comments: true
func linearSearchLinkedList(node *ListNode, target int) *ListNode {
// 遍历链表
for node != nil {
// 找到目标点,返回之
// 找到目标点,返回之
if node.Val == target {
return node
}
@@ -241,13 +241,13 @@ comments: true
function linearSearchLinkedList(head, target) {
// 遍历链表
while(head) {
// 找到目标点,返回之
// 找到目标点,返回之
if(head.val === target) {
return head;
}
head = head.next;
}
// 未找到目标点,返回 null
// 未找到目标点,返回 null
return null;
}
```
@@ -259,13 +259,13 @@ comments: true
function linearSearchLinkedList(head: ListNode | null, target: number): ListNode | null {
// 遍历链表
while (head) {
// 找到目标点,返回之
// 找到目标点,返回之
if (head.val === target) {
return head;
}
head = head.next;
}
// 未找到目标点,返回 null
// 未找到目标点,返回 null
return null;
}
```
@@ -285,12 +285,12 @@ comments: true
// 遍历链表
while (head != null)
{
// 找到目标点,返回之
// 找到目标点,返回之
if (head.val == target)
return head;
head = head.next;
}
// 未找到目标点,返回 null
// 未找到目标点,返回 null
return null;
}
```
@@ -303,13 +303,13 @@ comments: true
var head = head
// 遍历链表
while head != nil {
// 找到目标点,返回之
// 找到目标点,返回之
if head?.val == target {
return head
}
head = head?.next
}
// 未找到目标点,返回 null
// 未找到目标点,返回 null
return nil
}
```
@@ -322,7 +322,7 @@ comments: true
var head = node;
// 遍历链表
while (head != null) {
// 找到目标点,返回之
// 找到目标点,返回之
if (head.?.val == target) return head;
head = head.?.next;
}