mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Update linear_search and hashing_search.
This commit is contained in:
@ -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!)")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user