mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-16 05:30:59 +00:00
Refactor the articles related to searching algorithm. Add the chapter of binary search. Add the section of searching algorithm revisited. (#464)
This commit is contained in:
@@ -1,3 +1,2 @@
|
||||
add_executable(binary_search binary_search.c )
|
||||
add_executable(linear_search linear_search.c)
|
||||
add_executable(hashing_search hashing_search.c)
|
||||
add_executable(binary_search binary_search.c)
|
||||
add_executable(leetcode_two_sum leetcode_two_sum.c)
|
||||
@@ -1,59 +0,0 @@
|
||||
/**
|
||||
* File: binary_search.c
|
||||
* Created Time: 2023-03-18
|
||||
* Author: Guanngxu (446678850@qq.com)
|
||||
*/
|
||||
|
||||
#include "../include/include.h"
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
int binarySearch(int *nums, int len, int target) {
|
||||
// 初始化双闭区间 [0, n-1] ,即 left, right 分别指向数组首元素、尾元素
|
||||
int left = 0, right = len - 1;
|
||||
// 循环,当搜索区间为空时跳出(当 left > right 时为空)
|
||||
while (left <= right) {
|
||||
int mid = (left + right) / 2; // 计算中点索引 mid
|
||||
if (nums[mid] < target) // 此情况说明 target 在区间 [mid+1, right] 中
|
||||
left = mid + 1;
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid-1] 中
|
||||
right = mid - 1;
|
||||
else // 找到目标元素,返回其索引
|
||||
return mid;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
int binarySearch1(int *nums, int len, int target) {
|
||||
// 初始化左闭右开 [0, n) ,即 left, right 分别指向数组首元素、尾元素+1
|
||||
int left = 0, right = len;
|
||||
// 循环,当搜索区间为空时跳出(当 left = right 时为空)
|
||||
while (left < right) {
|
||||
int mid = (left + right) / 2; // 计算中点索引 mid
|
||||
if (nums[mid] < target) // 此情况说明 target 在区间 [mid+1, right) 中
|
||||
left = mid + 1;
|
||||
else if (nums[mid] > target) // 此情况说明 target 在区间 [left, mid) 中
|
||||
right = mid;
|
||||
else // 找到目标元素,返回其索引
|
||||
return mid;
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int target = 6;
|
||||
int nums[10] = { 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 };
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
int index = binarySearch(nums, 10, target);
|
||||
printf("目标元素 6 的索引 = %d\n", index);
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, 10, target);
|
||||
printf("目标元素 6 的索引 = %d\n", index);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* File: leetcode_two_sum.c
|
||||
* Created Time: 2023-01-19
|
||||
* Author: Reanon (793584285@qq.com)
|
||||
*/
|
||||
|
||||
#include "../include/include.h"
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {
|
||||
for (int i = 0; i < numsSize; ++i) {
|
||||
for (int j = i + 1; j < numsSize; ++j) {
|
||||
if (nums[i] + nums[j] == target) {
|
||||
int *ret = malloc(sizeof(int) * 2);
|
||||
ret[0] = i, ret[1] = j;
|
||||
*returnSize = 2;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
*returnSize = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* 哈希表 */
|
||||
struct hashTable {
|
||||
int key;
|
||||
int val;
|
||||
// 借助 LetCode 上常用的哈希表
|
||||
UT_hash_handle hh;
|
||||
};
|
||||
|
||||
typedef struct hashTable hashTable;
|
||||
|
||||
hashTable *find(hashTable *h, int key) {
|
||||
hashTable *tmp;
|
||||
HASH_FIND_INT(h, &key, tmp);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void insert(hashTable *h, int key, int val) {
|
||||
hashTable *t = find(h, key);
|
||||
if (t == NULL) {
|
||||
hashTable *tmp = malloc(sizeof(hashTable));
|
||||
tmp->key = key, tmp->val = val;
|
||||
HASH_ADD_INT(h, key, tmp);
|
||||
} else {
|
||||
t->val = val;
|
||||
}
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
|
||||
hashTable *hashtable = NULL;
|
||||
for (int i = 0; i < numsSize; i++) {
|
||||
hashTable *t = find(hashtable, target - nums[i]);
|
||||
if (t != NULL) {
|
||||
int *ret = malloc(sizeof(int) * 2);
|
||||
ret[0] = t->val, ret[1] = i;
|
||||
*returnSize = 2;
|
||||
return ret;
|
||||
}
|
||||
insert(hashtable, nums[i], i);
|
||||
}
|
||||
*returnSize = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// ======= Test Case =======
|
||||
int nums[] = {2, 7, 11, 15};
|
||||
int target = 9;
|
||||
// ====== Driver Code ======
|
||||
int returnSize;
|
||||
int *res = twoSumBruteForce(nums, sizeof(nums) / sizeof(int), target, &returnSize);
|
||||
// 方法一
|
||||
printf("方法一 res = ");
|
||||
printArray(res, returnSize);
|
||||
|
||||
// 方法二
|
||||
res = twoSumHashTable(nums, sizeof(nums) / sizeof(int), target, &returnSize);
|
||||
printf("方法二 res = ");
|
||||
printArray(res, returnSize);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user