Update linear_search and hashing_search.

This commit is contained in:
Yudong Jin
2023-02-04 23:49:37 +08:00
parent 62114ce79a
commit f14e3e4c57
22 changed files with 95 additions and 92 deletions

View File

@@ -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())
@@ -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())
@@ -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;

View File

@@ -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++) {
// 找到目标元素,返回其索引
@@ -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) {
// 找到目标结点,返回之
@@ -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;

View File

@@ -12,7 +12,7 @@ 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
@@ -20,7 +20,7 @@ namespace hello_algo.chapter_searching
}
/* 哈希查找(链表) */
static ListNode? hashingSearch1(Dictionary<int, ListNode> map, int target)
static ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> 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);
}
}

View File

@@ -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);
}
}

View File

@@ -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 {

View File

@@ -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)
}

View File

@@ -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 {
// 找到目标元素,返回其索引

View File

@@ -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)
}

View File

@@ -11,14 +11,14 @@ import java.util.*;
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);
@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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!)")
}
}

View File

@@ -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!)")
}
}

View File

@@ -9,14 +9,14 @@ import { ListNode, arrToLinkedList } from "../module/ListNode";
/* 哈希查找(数组) */
function hashingSearch(map: Map<number, number>, target: number): number {
function hashingSearchArray(map: Map<number, number>, target: number): number {
// 哈希表的 key: 目标元素value: 索引
// 若哈希表中无此 key ,返回 -1
return map.has(target) ? map.get(target) as number : -1;
}
/* 哈希查找(链表) */
function hashingSearch1(map: Map<number, ListNode>, target: number): ListNode | null {
function hashingSearchLinkedList(map: Map<number, ListNode>, 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 {};

View File

@@ -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);