Skip to content

Commit

Permalink
Update linear_search and hashing_search.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Feb 4, 2023
1 parent 62114ce commit f14e3e4
Show file tree
Hide file tree
Showing 22 changed files with 95 additions and 92 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ site/
.cache/
scripts/
docs/overrides/

docs/src
8 changes: 4 additions & 4 deletions codes/cpp/chapter_searching/hashing_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../include/include.hpp"

/* 哈希查找(数组) */
int hashingSearch(unordered_map<int, int> map, int target) {
int hashingSearchArray(unordered_map<int, int> map, int target) {
// 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1
if (map.find(target) == map.end())
Expand All @@ -16,7 +16,7 @@ int hashingSearch(unordered_map<int, int> map, int target) {
}

/* 哈希查找(链表) */
ListNode* hashingSearch1(unordered_map<int, ListNode*> map, int target) {
ListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> map, int target) {
// 哈希表的 key: 目标结点值,value: 结点对象
// 若哈希表中无此 key ,返回 nullptr
if (map.find(target) == map.end())
Expand All @@ -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;

/* 哈希查找(链表) */
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions codes/cpp/chapter_searching/linear_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../include/include.hpp"

/* 线性查找(数组) */
int linearSearch(vector<int>& nums, int target) {
int linearSearchArray(vector<int>& nums, int target) {
// 遍历数组
for (int i = 0; i < nums.size(); i++) {
// 找到目标元素,返回其索引
Expand All @@ -19,7 +19,7 @@ int linearSearch(vector<int>& nums, int target) {
}

/* 线性查找(链表) */
ListNode* linearSearch(ListNode* head, int target) {
ListNode* linearSearchLinkedList(ListNode* head, int target) {
// 遍历链表
while (head != nullptr) {
// 找到目标结点,返回之
Expand All @@ -38,12 +38,12 @@ int main() {

/* 在数组中执行线性查找 */
vector<int> 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;
Expand Down
8 changes: 4 additions & 4 deletions codes/csharp/chapter_searching/hashing_search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace hello_algo.chapter_searching
public class hashing_search
{
/* 哈希查找(数组) */
static int hashingSearch(Dictionary<int, int> map, int target)
static int hashingSearchArray(Dictionary<int, int> map, int target)
{
// 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1
return map.GetValueOrDefault(target, -1);
}

/* 哈希查找(链表) */
static ListNode? hashingSearch1(Dictionary<int, ListNode> map, int target)
static ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target)
{

// 哈希表的 key: 目标结点值,value: 结点对象
Expand All @@ -41,7 +41,7 @@ public void Test()
{
map[nums[i]] = i; // key: 元素,value: 索引
}
int index = hashingSearch(map, target);
int index = hashingSearchArray(map, target);
Console.WriteLine("目标元素 3 的索引 = " + index);

/* 哈希查找(链表) */
Expand All @@ -53,7 +53,7 @@ public void Test()
map1[head.val] = head; // key: 结点值,value: 结点
head = head.next;
}
ListNode? node = hashingSearch1(map1, target);
ListNode? node = hashingSearchLinkedList(map1, target);
Console.WriteLine("目标结点值 3 的对应结点对象为 " + node);
}
}
Expand Down
8 changes: 4 additions & 4 deletions codes/csharp/chapter_searching/linear_search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand All @@ -26,7 +26,7 @@ static int linearSearch(int[] nums, int target)
}

/* 线性查找(链表) */
static ListNode? linearSearch(ListNode head, int target)
static ListNode? linearSearchLinkedList(ListNode head, int target)
{
// 遍历链表
while (head != null)
Expand All @@ -47,12 +47,12 @@ public void Test()

/* 在数组中执行线性查找 */
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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions codes/go/chapter_searching/hashing_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions codes/go/chapter_searching/hashing_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

/* 哈希查找(链表) */
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions codes/go/chapter_searching/linear_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
// 找到目标元素,返回其索引
Expand All @@ -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 {
// 找到目标元素,返回其索引
Expand Down
4 changes: 2 additions & 2 deletions codes/go/chapter_searching/linear_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions codes/java/chapter_searching/hashing_search.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

public class hashing_search {
/* 哈希查找(数组) */
static int hashingSearch(Map<Integer, Integer> map, int target) {
static int hashingSearchArray(Map<Integer, Integer> map, int target) {
// 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1
return map.getOrDefault(target, -1);
}

/* 哈希查找(链表) */
static ListNode hashingSearch1(Map<Integer, ListNode> map, int target) {
static ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
// 哈希表的 key: 目标结点值,value: 结点对象
// 若哈希表中无此 key ,返回 null
return map.getOrDefault(target, null);
Expand All @@ -34,7 +34,7 @@ public static void main(String[] args) {
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);

/* 哈希查找(链表) */
Expand All @@ -45,7 +45,7 @@ public static void main(String[] args) {
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);
}
}
8 changes: 4 additions & 4 deletions codes/java/chapter_searching/linear_search.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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++) {
// 找到目标元素,返回其索引
Expand All @@ -22,7 +22,7 @@ static int linearSearch(int[] nums, int target) {
}

/* 线性查找(链表) */
static ListNode linearSearch(ListNode head, int target) {
static ListNode linearSearchLinkedList(ListNode head, int target) {
// 遍历链表
while (head != null) {
// 找到目标结点,返回之
Expand All @@ -39,12 +39,12 @@ public static void main(String[] args) {

/* 在数组中执行线性查找 */
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);
}
}
8 changes: 4 additions & 4 deletions codes/javascript/chapter_searching/hashing_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

/* 哈希查找(链表) */
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions codes/python/chapter_searching/hashing_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
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)
Expand All @@ -31,7 +31,7 @@ def hashing_search1(mapp, target):
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)

# 哈希查找(链表)
Expand All @@ -41,5 +41,5 @@ def hashing_search1(mapp, target):
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)
8 changes: 4 additions & 4 deletions codes/python/chapter_searching/linear_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
from include import *

""" 线性查找(数组) """
def linear_search(nums, target):
def linear_search_array(nums, target):
# 遍历数组
for i in range(len(nums)):
if nums[i] == target: # 找到目标元素,返回其索引
return i
return -1 # 未找到目标元素,返回 -1

""" 线性查找(链表) """
def linear_search1(head, target):
def linear_search_linkedlist(head, target):
# 遍历链表
while head:
if head.val == target: # 找到目标结点,返回之
Expand All @@ -32,10 +32,10 @@ def linear_search1(head, target):

# 在数组中执行线性查找
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)
Loading

0 comments on commit f14e3e4

Please sign in to comment.