From f14e3e4c57bf0e3a161a6a5d03c1331a866bcd7f Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Sat, 4 Feb 2023 23:49:37 +0800 Subject: [PATCH] Update linear_search and hashing_search. --- .gitignore | 2 ++ .../cpp/chapter_searching/hashing_search.cpp | 8 ++--- codes/cpp/chapter_searching/linear_search.cpp | 8 ++--- .../chapter_searching/hashing_search.cs | 8 ++--- .../csharp/chapter_searching/linear_search.cs | 8 ++--- codes/go/chapter_searching/hashing_search.go | 4 +-- .../chapter_searching/hashing_search_test.go | 4 +-- codes/go/chapter_searching/linear_search.go | 4 +-- .../chapter_searching/linear_search_test.go | 4 +-- .../chapter_searching/hashing_search.java | 8 ++--- .../java/chapter_searching/linear_search.java | 8 ++--- .../chapter_searching/hashing_search.js | 8 ++--- .../chapter_searching/hashing_search.py | 8 ++--- .../python/chapter_searching/linear_search.py | 8 ++--- .../chapter_searching/hashing_search.swift | 8 ++--- .../chapter_searching/linear_search.swift | 8 ++--- .../chapter_searching/hashing_search.ts | 8 ++--- .../zig/chapter_searching/hashing_search.zig | 8 ++--- docs/chapter_array_and_linkedlist/list.md | 10 +++--- docs/chapter_searching/hashing_search.md | 32 +++++++++---------- docs/chapter_searching/linear_search.md | 22 ++++++------- mkdocs.yml | 1 + 22 files changed, 95 insertions(+), 92 deletions(-) diff --git a/.gitignore b/.gitignore index 5cc6522a2..0c4924a8e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ site/ .cache/ scripts/ docs/overrides/ + +docs/src diff --git a/codes/cpp/chapter_searching/hashing_search.cpp b/codes/cpp/chapter_searching/hashing_search.cpp index ebc2fb01c..dcd1faa04 100644 --- a/codes/cpp/chapter_searching/hashing_search.cpp +++ b/codes/cpp/chapter_searching/hashing_search.cpp @@ -7,7 +7,7 @@ #include "../include/include.hpp" /* 哈希查找(数组) */ -int hashingSearch(unordered_map map, int target) { +int hashingSearchArray(unordered_map map, int target) { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 if (map.find(target) == map.end()) @@ -16,7 +16,7 @@ int hashingSearch(unordered_map map, int target) { } /* 哈希查找(链表) */ -ListNode* hashingSearch1(unordered_map map, int target) { +ListNode* hashingSearchLinkedList(unordered_map map, int target) { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 nullptr if (map.find(target) == map.end()) @@ -36,7 +36,7 @@ int main() { for (int i = 0; i < nums.size(); i++) { map[nums[i]] = i; // key: 元素,value: 索引 } - int index = hashingSearch(map, target); + int index = hashingSearchArray(map, target); cout << "目标元素 3 的索引 = " << index << endl; /* 哈希查找(链表) */ @@ -47,7 +47,7 @@ int main() { map1[head->val] = head; // key: 结点值,value: 结点 head = head->next; } - ListNode* node = hashingSearch1(map1, target); + ListNode* node = hashingSearchLinkedList(map1, target); cout << "目标结点值 3 的对应结点对象为 " << node << endl; return 0; diff --git a/codes/cpp/chapter_searching/linear_search.cpp b/codes/cpp/chapter_searching/linear_search.cpp index 7b5092762..2b3dba090 100644 --- a/codes/cpp/chapter_searching/linear_search.cpp +++ b/codes/cpp/chapter_searching/linear_search.cpp @@ -7,7 +7,7 @@ #include "../include/include.hpp" /* 线性查找(数组) */ -int linearSearch(vector& nums, int target) { +int linearSearchArray(vector& nums, int target) { // 遍历数组 for (int i = 0; i < nums.size(); i++) { // 找到目标元素,返回其索引 @@ -19,7 +19,7 @@ int linearSearch(vector& nums, int target) { } /* 线性查找(链表) */ -ListNode* linearSearch(ListNode* head, int target) { +ListNode* linearSearchLinkedList(ListNode* head, int target) { // 遍历链表 while (head != nullptr) { // 找到目标结点,返回之 @@ -38,12 +38,12 @@ int main() { /* 在数组中执行线性查找 */ vector nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 }; - int index = linearSearch(nums, target); + int index = linearSearchArray(nums, target); cout << "目标元素 3 的索引 = " << index << endl; /* 在链表中执行线性查找 */ ListNode* head = vecToLinkedList(nums); - ListNode* node = linearSearch(head, target); + ListNode* node = linearSearchLinkedList(head, target); cout << "目标结点值 3 的对应结点对象为 " << node << endl; return 0; diff --git a/codes/csharp/chapter_searching/hashing_search.cs b/codes/csharp/chapter_searching/hashing_search.cs index 33d5fe9dc..1c324bdfa 100644 --- a/codes/csharp/chapter_searching/hashing_search.cs +++ b/codes/csharp/chapter_searching/hashing_search.cs @@ -12,7 +12,7 @@ namespace hello_algo.chapter_searching public class hashing_search { /* 哈希查找(数组) */ - static int hashingSearch(Dictionary map, int target) + static int hashingSearchArray(Dictionary map, int target) { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 @@ -20,7 +20,7 @@ namespace hello_algo.chapter_searching } /* 哈希查找(链表) */ - static ListNode? hashingSearch1(Dictionary map, int target) + static ListNode? hashingSearchLinkedList(Dictionary map, int target) { // 哈希表的 key: 目标结点值,value: 结点对象 @@ -41,7 +41,7 @@ namespace hello_algo.chapter_searching { map[nums[i]] = i; // key: 元素,value: 索引 } - int index = hashingSearch(map, target); + int index = hashingSearchArray(map, target); Console.WriteLine("目标元素 3 的索引 = " + index); /* 哈希查找(链表) */ @@ -53,7 +53,7 @@ namespace hello_algo.chapter_searching map1[head.val] = head; // key: 结点值,value: 结点 head = head.next; } - ListNode? node = hashingSearch1(map1, target); + ListNode? node = hashingSearchLinkedList(map1, target); Console.WriteLine("目标结点值 3 的对应结点对象为 " + node); } } diff --git a/codes/csharp/chapter_searching/linear_search.cs b/codes/csharp/chapter_searching/linear_search.cs index 412eb37d3..7faa47325 100644 --- a/codes/csharp/chapter_searching/linear_search.cs +++ b/codes/csharp/chapter_searching/linear_search.cs @@ -12,7 +12,7 @@ namespace hello_algo.chapter_searching public class linear_search { /* 线性查找(数组) */ - static int linearSearch(int[] nums, int target) + static int linearSearchArray(int[] nums, int target) { // 遍历数组 for (int i = 0; i < nums.Length; i++) @@ -26,7 +26,7 @@ namespace hello_algo.chapter_searching } /* 线性查找(链表) */ - static ListNode? linearSearch(ListNode head, int target) + static ListNode? linearSearchLinkedList(ListNode head, int target) { // 遍历链表 while (head != null) @@ -47,12 +47,12 @@ namespace hello_algo.chapter_searching /* 在数组中执行线性查找 */ int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 }; - int index = linearSearch(nums, target); + int index = linearSearchArray(nums, target); Console.WriteLine("目标元素 3 的索引 = " + index); /* 在链表中执行线性查找 */ ListNode head = ListNode.ArrToLinkedList(nums); - ListNode? node = linearSearch(head, target); + ListNode? node = linearSearchLinkedList(head, target); Console.WriteLine("目标结点值 3 的对应结点对象为 " + node); } } diff --git a/codes/go/chapter_searching/hashing_search.go b/codes/go/chapter_searching/hashing_search.go index 22af449f0..56e1eeb91 100644 --- a/codes/go/chapter_searching/hashing_search.go +++ b/codes/go/chapter_searching/hashing_search.go @@ -7,7 +7,7 @@ package chapter_searching import . "github.com/krahets/hello-algo/pkg" /* 哈希查找(数组) */ -func hashingSearch(m map[int]int, target int) int { +func hashingSearchArray(m map[int]int, target int) int { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 if index, ok := m[target]; ok { @@ -18,7 +18,7 @@ func hashingSearch(m map[int]int, target int) int { } /* 哈希查找(链表) */ -func hashingSearch1(m map[int]*ListNode, target int) *ListNode { +func hashingSearchLinkedList(m map[int]*ListNode, target int) *ListNode { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 nil if node, ok := m[target]; ok { diff --git a/codes/go/chapter_searching/hashing_search_test.go b/codes/go/chapter_searching/hashing_search_test.go index 18cf5c651..b4b89ad47 100644 --- a/codes/go/chapter_searching/hashing_search_test.go +++ b/codes/go/chapter_searching/hashing_search_test.go @@ -20,7 +20,7 @@ func TestHashingSearch(t *testing.T) { for i := 0; i < len(nums); i++ { m[nums[i]] = i } - index := hashingSearch(m, target) + index := hashingSearchArray(m, target) fmt.Println("目标元素 3 的索引 = ", index) /* 哈希查找(链表) */ @@ -31,6 +31,6 @@ func TestHashingSearch(t *testing.T) { m1[head.Val] = head head = head.Next } - node := hashingSearch1(m1, target) + node := hashingSearchLinkedList(m1, target) fmt.Println("目标结点值 3 的对应结点对象为 ", node) } diff --git a/codes/go/chapter_searching/linear_search.go b/codes/go/chapter_searching/linear_search.go index 256037519..f9cc1a7c2 100644 --- a/codes/go/chapter_searching/linear_search.go +++ b/codes/go/chapter_searching/linear_search.go @@ -9,7 +9,7 @@ import ( ) /* 线性查找(数组) */ -func linerSearchArray(nums []int, target int) int { +func linearSearchArray(nums []int, target int) int { // 遍历数组 for i := 0; i < len(nums); i++ { // 找到目标元素,返回其索引 @@ -22,7 +22,7 @@ func linerSearchArray(nums []int, target int) int { } /* 线性查找(链表) */ -func linerSearchLinkedList(node *ListNode, target int) *ListNode { +func linearSearchLinkedList(node *ListNode, target int) *ListNode { // 遍历链表 for node != nil { // 找到目标元素,返回其索引 diff --git a/codes/go/chapter_searching/linear_search_test.go b/codes/go/chapter_searching/linear_search_test.go index 79525aace..4c36c4175 100644 --- a/codes/go/chapter_searching/linear_search_test.go +++ b/codes/go/chapter_searching/linear_search_test.go @@ -16,11 +16,11 @@ func TestLinearSearch(t *testing.T) { nums := []int{1, 5, 3, 2, 4, 7, 5, 9, 10, 8} // 在数组中执行线性查找 - index := linerSearchArray(nums, target) + index := linearSearchArray(nums, target) fmt.Println("目标元素 3 的索引 =", index) // 在链表中执行线性查找 head := ArrayToLinkedList(nums) - node := linerSearchLinkedList(head, target) + node := linearSearchLinkedList(head, target) fmt.Println("目标结点值 3 的对应结点对象为", node) } diff --git a/codes/java/chapter_searching/hashing_search.java b/codes/java/chapter_searching/hashing_search.java index cfd015313..364d29699 100644 --- a/codes/java/chapter_searching/hashing_search.java +++ b/codes/java/chapter_searching/hashing_search.java @@ -11,14 +11,14 @@ import java.util.*; public class hashing_search { /* 哈希查找(数组) */ - static int hashingSearch(Map map, int target) { + static int hashingSearchArray(Map map, int target) { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 return map.getOrDefault(target, -1); } /* 哈希查找(链表) */ - static ListNode hashingSearch1(Map map, int target) { + static ListNode hashingSearchLinkedList(Map map, int target) { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null return map.getOrDefault(target, null); @@ -34,7 +34,7 @@ public class hashing_search { for (int i = 0; i < nums.length; i++) { map.put(nums[i], i); // key: 元素,value: 索引 } - int index = hashingSearch(map, target); + int index = hashingSearchArray(map, target); System.out.println("目标元素 3 的索引 = " + index); /* 哈希查找(链表) */ @@ -45,7 +45,7 @@ public class hashing_search { map1.put(head.val, head); // key: 结点值,value: 结点 head = head.next; } - ListNode node = hashingSearch1(map1, target); + ListNode node = hashingSearchLinkedList(map1, target); System.out.println("目标结点值 3 的对应结点对象为 " + node); } } diff --git a/codes/java/chapter_searching/linear_search.java b/codes/java/chapter_searching/linear_search.java index c33b51178..4976575d8 100644 --- a/codes/java/chapter_searching/linear_search.java +++ b/codes/java/chapter_searching/linear_search.java @@ -10,7 +10,7 @@ import include.*; public class linear_search { /* 线性查找(数组) */ - static int linearSearch(int[] nums, int target) { + static int linearSearchArray(int[] nums, int target) { // 遍历数组 for (int i = 0; i < nums.length; i++) { // 找到目标元素,返回其索引 @@ -22,7 +22,7 @@ public class linear_search { } /* 线性查找(链表) */ - static ListNode linearSearch(ListNode head, int target) { + static ListNode linearSearchLinkedList(ListNode head, int target) { // 遍历链表 while (head != null) { // 找到目标结点,返回之 @@ -39,12 +39,12 @@ public class linear_search { /* 在数组中执行线性查找 */ int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 }; - int index = linearSearch(nums, target); + int index = linearSearchArray(nums, target); System.out.println("目标元素 3 的索引 = " + index); /* 在链表中执行线性查找 */ ListNode head = ListNode.arrToLinkedList(nums); - ListNode node = linearSearch(head, target); + ListNode node = linearSearchLinkedList(head, target); System.out.println("目标结点值 3 的对应结点对象为 " + node); } } diff --git a/codes/javascript/chapter_searching/hashing_search.js b/codes/javascript/chapter_searching/hashing_search.js index ebe6a139f..5c1c51004 100644 --- a/codes/javascript/chapter_searching/hashing_search.js +++ b/codes/javascript/chapter_searching/hashing_search.js @@ -9,14 +9,14 @@ const ListNode = require("../include/ListNode"); /* 哈希查找(数组) */ -function hashingSearch(map, target) { +function hashingSearchArray(map, target) { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 return map.has(target) ? map.get(target) : -1; } /* 哈希查找(链表) */ -function hashingSearch1(map, target) { +function hashingSearchLinkedList(map, target) { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null return map.has(target) ? map.get(target) : null; @@ -32,7 +32,7 @@ function main() { for (let i = 0; i < nums.length; i++) { map.set(nums[i], i); // key: 元素,value: 索引 } - const index = hashingSearch(map, target); + const index = hashingSearchArray(map, target); console.log("目标元素 3 的索引 = " + index); /* 哈希查找(链表) */ @@ -43,7 +43,7 @@ function main() { map1.set(head.val, head); // key: 结点值,value: 结点 head = head.next; } - const node = hashingSearch1(map1, target); + const node = hashingSearchLinkedList(map1, target); console.log("目标结点值 3 的对应结点对象为" ); PrintUtil.printLinkedList(node); } diff --git a/codes/python/chapter_searching/hashing_search.py b/codes/python/chapter_searching/hashing_search.py index f49c703be..37ad95199 100644 --- a/codes/python/chapter_searching/hashing_search.py +++ b/codes/python/chapter_searching/hashing_search.py @@ -9,13 +9,13 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * """ 哈希查找(数组) """ -def hashing_search(mapp, target): +def hashing_search_array(mapp, target): # 哈希表的 key: 目标元素,value: 索引 # 若哈希表中无此 key ,返回 -1 return mapp.get(target, -1) """ 哈希查找(链表) """ -def hashing_search1(mapp, target): +def hashing_search_linkedlist(mapp, target): # 哈希表的 key: 目标元素,value: 结点对象 # 若哈希表中无此 key ,返回 -1 return mapp.get(target, -1) @@ -31,7 +31,7 @@ if __name__ == '__main__': mapp = {} for i in range(len(nums)): mapp[nums[i]] = i # key: 元素,value: 索引 - index = hashing_search(mapp, target) + index = hashing_search_array(mapp, target) print("目标元素 3 的索引 =", index) # 哈希查找(链表) @@ -41,5 +41,5 @@ if __name__ == '__main__': while head: map1[head.val] = head # key: 结点值,value: 结点 head = head.next - node = hashing_search1(map1, target) + node = hashing_search_linkedlist(map1, target) print("目标结点值 3 的对应结点对象为", node) diff --git a/codes/python/chapter_searching/linear_search.py b/codes/python/chapter_searching/linear_search.py index 8ff9e0803..8964465c6 100644 --- a/codes/python/chapter_searching/linear_search.py +++ b/codes/python/chapter_searching/linear_search.py @@ -9,7 +9,7 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * """ 线性查找(数组) """ -def linear_search(nums, target): +def linear_search_array(nums, target): # 遍历数组 for i in range(len(nums)): if nums[i] == target: # 找到目标元素,返回其索引 @@ -17,7 +17,7 @@ def linear_search(nums, target): return -1 # 未找到目标元素,返回 -1 """ 线性查找(链表) """ -def linear_search1(head, target): +def linear_search_linkedlist(head, target): # 遍历链表 while head: if head.val == target: # 找到目标结点,返回之 @@ -32,10 +32,10 @@ if __name__ == '__main__': # 在数组中执行线性查找 nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8] - index = linear_search(nums, target) + index = linear_search_array(nums, target) print("目标元素 3 的索引 =", index) # 在链表中执行线性查找 head = list_to_linked_list(nums) - node = linear_search1(head, target) + node = linear_search_linkedlist(head, target) print("目标结点值 3 的对应结点对象为", node) diff --git a/codes/swift/chapter_searching/hashing_search.swift b/codes/swift/chapter_searching/hashing_search.swift index 1ff8a5754..3ef8f239a 100644 --- a/codes/swift/chapter_searching/hashing_search.swift +++ b/codes/swift/chapter_searching/hashing_search.swift @@ -7,14 +7,14 @@ import utils /* 哈希查找(数组) */ -func hashingSearch(map: [Int: Int], target: Int) -> Int { +func hashingSearchArray(map: [Int: Int], target: Int) -> Int { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 return map[target, default: -1] } /* 哈希查找(链表) */ -func hashingSearch1(map: [Int: ListNode], target: Int) -> ListNode? { +func hashingSearchLinkedList(map: [Int: ListNode], target: Int) -> ListNode? { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null return map[target] @@ -33,7 +33,7 @@ enum HashingSearch { for i in nums.indices { map[nums[i]] = i // key: 元素,value: 索引 } - let index = hashingSearch(map: map, target: target) + let index = hashingSearchArray(map: map, target: target) print("目标元素 3 的索引 = \(index)") /* 哈希查找(链表) */ @@ -44,7 +44,7 @@ enum HashingSearch { map1[head!.val] = head! // key: 结点值,value: 结点 head = head?.next } - let node = hashingSearch1(map: map1, target: target) + let node = hashingSearchLinkedList(map: map1, target: target) print("目标结点值 3 的对应结点对象为 \(node!)") } } diff --git a/codes/swift/chapter_searching/linear_search.swift b/codes/swift/chapter_searching/linear_search.swift index 329c356d2..ef101cecb 100644 --- a/codes/swift/chapter_searching/linear_search.swift +++ b/codes/swift/chapter_searching/linear_search.swift @@ -7,7 +7,7 @@ import utils /* 线性查找(数组) */ -func linearSearch(nums: [Int], target: Int) -> Int { +func linearSearchArray(nums: [Int], target: Int) -> Int { // 遍历数组 for i in nums.indices { // 找到目标元素,返回其索引 @@ -20,7 +20,7 @@ func linearSearch(nums: [Int], target: Int) -> Int { } /* 线性查找(链表) */ -func linearSearch(head: ListNode?, target: Int) -> ListNode? { +func linearSearchLinkedList(head: ListNode?, target: Int) -> ListNode? { var head = head // 遍历链表 while head != nil { @@ -42,12 +42,12 @@ enum LinearSearch { /* 在数组中执行线性查找 */ let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8] - let index = linearSearch(nums: nums, target: target) + let index = linearSearchArray(nums: nums, target: target) print("目标元素 3 的索引 = \(index)") /* 在链表中执行线性查找 */ let head = ListNode.arrToLinkedList(arr: nums) - let node = linearSearch(head: head, target: target) + let node = linearSearchLinkedList(head: head, target: target) print("目标结点值 3 的对应结点对象为 \(node!)") } } diff --git a/codes/typescript/chapter_searching/hashing_search.ts b/codes/typescript/chapter_searching/hashing_search.ts index c840bfa6f..572671831 100644 --- a/codes/typescript/chapter_searching/hashing_search.ts +++ b/codes/typescript/chapter_searching/hashing_search.ts @@ -9,14 +9,14 @@ import { ListNode, arrToLinkedList } from "../module/ListNode"; /* 哈希查找(数组) */ -function hashingSearch(map: Map, target: number): number { +function hashingSearchArray(map: Map, target: number): number { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 return map.has(target) ? map.get(target) as number : -1; } /* 哈希查找(链表) */ -function hashingSearch1(map: Map, target: number): ListNode | null { +function hashingSearchLinkedList(map: Map, target: number): ListNode | null { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null return map.has(target) ? map.get(target) as ListNode : null; @@ -33,7 +33,7 @@ const map = new Map(); for (let i = 0; i < nums.length; i++) { map.set(nums[i], i); // key: 元素,value: 索引 } -const index = hashingSearch(map, target); +const index = hashingSearchArray(map, target); console.log("目标元素 3 的索引 = " + index); /* 哈希查找(链表) */ @@ -44,7 +44,7 @@ while (head != null) { map1.set(head.val, head); // key: 结点值,value: 结点 head = head.next; } -const node = hashingSearch1(map1, target); +const node = hashingSearchLinkedList(map1, target); console.log("目标结点值 3 的对应结点对象为", node); export {}; diff --git a/codes/zig/chapter_searching/hashing_search.zig b/codes/zig/chapter_searching/hashing_search.zig index b6f1a5552..1b29276ad 100644 --- a/codes/zig/chapter_searching/hashing_search.zig +++ b/codes/zig/chapter_searching/hashing_search.zig @@ -6,7 +6,7 @@ const std = @import("std"); const inc = @import("include"); // 哈希查找(数组) -fn hashingSearch(comptime T: type, map: std.AutoHashMap(T, T), target: T) T { +fn hashingSearchArray(comptime T: type, map: std.AutoHashMap(T, T), target: T) T { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 if (map.getKey(target) == null) return -1; @@ -14,7 +14,7 @@ fn hashingSearch(comptime T: type, map: std.AutoHashMap(T, T), target: T) T { } // 哈希查找(数组) -fn hashingSearch1(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) { +fn hashingSearchLinkedList(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null if (map.getKey(target) == null) return null; @@ -33,7 +33,7 @@ pub fn main() !void { for (nums) |num, i| { try map.put(num, @intCast(i32, i)); // key: 元素,value: 索引 } - var index = hashingSearch(i32, map, target); + var index = hashingSearchArray(i32, map, target); std.debug.print("目标元素 3 的索引 = {}\n", .{index}); // 哈希查找(链表) @@ -48,7 +48,7 @@ pub fn main() !void { try map1.put(head.?.val, head.?); head = head.?.next; } - var node = hashingSearch1(i32, map1, target); + var node = hashingSearchLinkedList(i32, map1, target); std.debug.print("目标结点值 3 的对应结点对象为 ", .{}); try inc.PrintUtil.printLinkedList(i32, node); diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index e7ec18658..b6b413aca 100644 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -920,7 +920,7 @@ comments: true self.__nums = [0] * self.__capacity # 数组(存储列表元素) self.__size = 0 # 列表长度(即当前元素数量) self.__extend_ratio = 2 # 每次列表扩容的倍数 - + """ 获取列表长度(即当前元素数量) """ def size(self): return self.__size @@ -934,12 +934,12 @@ comments: true # 索引如果越界则抛出异常,下同 assert index >= 0 and index < self.__size, "索引越界" return self.__nums[index] - + """ 更新元素 """ def set(self, num, index): assert index >= 0 and index < self.__size, "索引越界" self.__nums[index] = num - + """ 中间插入(尾部添加)元素 """ def add(self, num, index=-1): assert index >= 0 and index < self.__size, "索引越界" @@ -955,7 +955,7 @@ comments: true self.__nums[index] = num # 更新元素数量 self.__size += 1 - + """ 删除元素 """ def remove(self, index): assert index >= 0 and index < self.__size, "索引越界" @@ -967,7 +967,7 @@ comments: true self.__size -= 1 # 返回被删除元素 return num - + """ 列表扩容 """ def extend_capacity(self): # 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组 diff --git a/docs/chapter_searching/hashing_search.md b/docs/chapter_searching/hashing_search.md index 6604deaba..157e67a2e 100644 --- a/docs/chapter_searching/hashing_search.md +++ b/docs/chapter_searching/hashing_search.md @@ -20,7 +20,7 @@ comments: true ```java title="hashing_search.java" /* 哈希查找(数组) */ - int hashingSearch(Map map, int target) { + int hashingSearchArray(Map map, int target) { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 return map.getOrDefault(target, -1); @@ -31,7 +31,7 @@ comments: true ```cpp title="hashing_search.cpp" /* 哈希查找(数组) */ - int hashingSearch(unordered_map map, int target) { + int hashingSearchArray(unordered_map map, int target) { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 if (map.find(target) == map.end()) @@ -44,7 +44,7 @@ comments: true ```python title="hashing_search.py" """ 哈希查找(数组) """ - def hashing_search(mapp, target): + def hashing_search_array(mapp, target): # 哈希表的 key: 目标元素,value: 索引 # 若哈希表中无此 key ,返回 -1 return mapp.get(target, -1) @@ -54,7 +54,7 @@ comments: true ```go title="hashing_search.go" /* 哈希查找(数组) */ - func hashingSearch(m map[int]int, target int) int { + func hashingSearchArray(m map[int]int, target int) int { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 if index, ok := m[target]; ok { @@ -69,7 +69,7 @@ comments: true ```js title="hashing_search.js" /* 哈希查找(数组) */ - function hashingSearch(map, target) { + function hashingSearchArray(map, target) { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 return map.has(target) ? map.get(target) : -1; @@ -80,7 +80,7 @@ comments: true ```typescript title="hashing_search.ts" /* 哈希查找(数组) */ - function hashingSearch(map: Map, target: number): number { + function hashingSearchArray(map: Map, target: number): number { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 return map.has(target) ? map.get(target) as number : -1; @@ -97,7 +97,7 @@ comments: true ```csharp title="hashing_search.cs" /* 哈希查找(数组) */ - int hashingSearch(Dictionary map, int target) + int hashingSearchArray(Dictionary map, int target) { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 @@ -109,7 +109,7 @@ comments: true ```swift title="hashing_search.swift" /* 哈希查找(数组) */ - func hashingSearch(map: [Int: Int], target: Int) -> Int { + func hashingSearchArray(map: [Int: Int], target: Int) -> Int { // 哈希表的 key: 目标元素,value: 索引 // 若哈希表中无此 key ,返回 -1 return map[target, default: -1] @@ -130,7 +130,7 @@ comments: true ```java title="hashing_search.java" /* 哈希查找(链表) */ - ListNode hashingSearch1(Map map, int target) { + ListNode hashingSearchLinkedList(Map map, int target) { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null return map.getOrDefault(target, null); @@ -141,7 +141,7 @@ comments: true ```cpp title="hashing_search.cpp" /* 哈希查找(链表) */ - ListNode* hashingSearch1(unordered_map map, int target) { + ListNode* hashingSearchLinkedList(unordered_map map, int target) { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 nullptr if (map.find(target) == map.end()) @@ -154,7 +154,7 @@ comments: true ```python title="hashing_search.py" """ 哈希查找(链表) """ - def hashing_search1(mapp, target): + def hashing_search_linkedlist(mapp, target): # 哈希表的 key: 目标元素,value: 结点对象 # 若哈希表中无此 key ,返回 -1 return mapp.get(target, -1) @@ -164,7 +164,7 @@ comments: true ```go title="hashing_search.go" /* 哈希查找(链表) */ - func hashingSearch1(m map[int]*ListNode, target int) *ListNode { + func hashingSearchLinkedList(m map[int]*ListNode, target int) *ListNode { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 nil if node, ok := m[target]; ok { @@ -179,7 +179,7 @@ comments: true ```js title="hashing_search.js" /* 哈希查找(链表) */ - function hashingSearch1(map, target) { + function hashingSearchLinkedList(map, target) { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null return map.has(target) ? map.get(target) : null; @@ -190,7 +190,7 @@ comments: true ```typescript title="hashing_search.ts" /* 哈希查找(链表) */ - function hashingSearch1(map: Map, target: number): ListNode | null { + function hashingSearchLinkedList(map: Map, target: number): ListNode | null { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null return map.has(target) ? map.get(target) as ListNode : null; @@ -207,7 +207,7 @@ comments: true ```csharp title="hashing_search.cs" /* 哈希查找(链表) */ - ListNode? hashingSearch1(Dictionary map, int target) + ListNode? hashingSearchLinkedList(Dictionary map, int target) { // 哈希表的 key: 目标结点值,value: 结点对象 @@ -220,7 +220,7 @@ comments: true ```swift title="hashing_search.swift" /* 哈希查找(链表) */ - func hashingSearch1(map: [Int: ListNode], target: Int) -> ListNode? { + func hashingSearchLinkedList(map: [Int: ListNode], target: Int) -> ListNode? { // 哈希表的 key: 目标结点值,value: 结点对象 // 若哈希表中无此 key ,返回 null return map[target] diff --git a/docs/chapter_searching/linear_search.md b/docs/chapter_searching/linear_search.md index 73da9fe70..5a8bb05fa 100644 --- a/docs/chapter_searching/linear_search.md +++ b/docs/chapter_searching/linear_search.md @@ -16,7 +16,7 @@ comments: true ```java title="linear_search.java" /* 线性查找(数组) */ - int linearSearch(int[] nums, int target) { + int linearSearchArray(int[] nums, int target) { // 遍历数组 for (int i = 0; i < nums.length; i++) { // 找到目标元素,返回其索引 @@ -32,7 +32,7 @@ comments: true ```cpp title="linear_search.cpp" /* 线性查找(数组) */ - int linearSearch(vector& nums, int target) { + int linearSearchArray(vector& nums, int target) { // 遍历数组 for (int i = 0; i < nums.size(); i++) { // 找到目标元素,返回其索引 @@ -48,7 +48,7 @@ comments: true ```python title="linear_search.py" """ 线性查找(数组) """ - def linear_search(nums, target): + def linear_search_array(nums, target): # 遍历数组 for i in range(len(nums)): if nums[i] == target: # 找到目标元素,返回其索引 @@ -60,7 +60,7 @@ comments: true ```go title="linear_search.go" /* 线性查找(数组) */ - func linerSearchArray(nums []int, target int) int { + func linearSearchArray(nums []int, target int) int { // 遍历数组 for i := 0; i < len(nums); i++ { // 找到目标元素,返回其索引 @@ -118,7 +118,7 @@ comments: true ```csharp title="linear_search.cs" /* 线性查找(数组) */ - int linearSearch(int[] nums, int target) + int linearSearchArray(int[] nums, int target) { // 遍历数组 for (int i = 0; i < nums.Length; i++) @@ -137,7 +137,7 @@ comments: true ```swift title="linear_search.swift" /* 线性查找(数组) */ - func linearSearch(nums: [Int], target: Int) -> Int { + func linearSearchArray(nums: [Int], target: Int) -> Int { // 遍历数组 for i in nums.indices { // 找到目标元素,返回其索引 @@ -162,7 +162,7 @@ comments: true ```java title="linear_search.java" /* 线性查找(链表) */ - ListNode linearSearch(ListNode head, int target) { + ListNode linearSearchLinkedList(ListNode head, int target) { // 遍历链表 while (head != null) { // 找到目标结点,返回之 @@ -179,7 +179,7 @@ comments: true ```cpp title="linear_search.cpp" /* 线性查找(链表) */ - ListNode* linearSearch(ListNode* head, int target) { + ListNode* linearSearchLinkedList(ListNode* head, int target) { // 遍历链表 while (head != nullptr) { // 找到目标结点,返回之 @@ -196,7 +196,7 @@ comments: true ```python title="linear_search.py" """ 线性查找(链表) """ - def linear_search1(head, target): + def linear_search_linkedlist(head, target): # 遍历链表 while head: if head.val == target: # 找到目标结点,返回之 @@ -269,7 +269,7 @@ comments: true ```csharp title="linear_search.cs" /* 线性查找(链表) */ - ListNode? linearSearch(ListNode head, int target) + ListNode? linearSearchLinkedList(ListNode head, int target) { // 遍历链表 while (head != null) @@ -288,7 +288,7 @@ comments: true ```swift title="linear_search.swift" /* 线性查找(链表) */ - func linearSearch(head: ListNode?, target: Int) -> ListNode? { + func linearSearchLinkedList(head: ListNode?, target: Int) -> ListNode? { var head = head // 遍历链表 while head != nil { diff --git a/mkdocs.yml b/mkdocs.yml index 370745720..ae4987632 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -3,6 +3,7 @@ site_name: Hello 算法 site_url: https://www.hello-algo.com/ site_author: Krahets site_description: 一本动画图解、能运行、可提问的数据结构与算法入门书 +docs_dir: docs # Repository repo_name: krahets/hello-algo repo_url: https://github.com/krahets/hello-algo