mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-12 18:10:42 +08:00
Update linear_search and hashing_search.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@ -11,3 +11,5 @@ site/
|
||||
.cache/
|
||||
scripts/
|
||||
docs/overrides/
|
||||
|
||||
docs/src
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
// 找到目标元素,返回其索引
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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!)")
|
||||
}
|
||||
}
|
||||
|
@ -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!)")
|
||||
}
|
||||
}
|
||||
|
@ -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 {};
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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 的数组,并将原数组拷贝到新数组
|
||||
|
@ -20,7 +20,7 @@ comments: true
|
||||
|
||||
```java title="hashing_search.java"
|
||||
/* 哈希查找(数组) */
|
||||
int hashingSearch(Map<Integer, Integer> map, int target) {
|
||||
int hashingSearchArray(Map<Integer, Integer> 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<int, int> map, int target) {
|
||||
int hashingSearchArray(unordered_map<int, int> 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<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;
|
||||
@ -97,7 +97,7 @@ comments: true
|
||||
|
||||
```csharp title="hashing_search.cs"
|
||||
/* 哈希查找(数组) */
|
||||
int hashingSearch(Dictionary<int, int> map, int target)
|
||||
int hashingSearchArray(Dictionary<int, int> 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<Integer, ListNode> map, int target) {
|
||||
ListNode hashingSearchLinkedList(Map<Integer, ListNode> 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<int, ListNode*> map, int target) {
|
||||
ListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> 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<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;
|
||||
@ -207,7 +207,7 @@ comments: true
|
||||
|
||||
```csharp title="hashing_search.cs"
|
||||
/* 哈希查找(链表) */
|
||||
ListNode? hashingSearch1(Dictionary<int, ListNode> map, int target)
|
||||
ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> 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]
|
||||
|
@ -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<int>& nums, int target) {
|
||||
int linearSearchArray(vector<int>& 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 {
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user