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