力扣 704 二分法查找 (C语言)

该代码片段是关于一个改进的二分查找函数,用于在数组中寻找目标值。循环条件调整了右边界,确保在遍历过程中正确处理数组末尾的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

在这里插入图片描述

题解

循环中的条件是left < = right,此时条件为左闭右闭区间,right的值会被取到right应赋值为numsSize - 1,已匹配数组中最后一位元素的下标。

int search(int* nums, int numsSize, int target) {
    // 'nums' 是指向数组第一个元素的指针。
    // 'numsSize' 是数组中元素的数量。
    // 'target' 是需要在数组中查找的目标值。

    int left = 0;                 // 定义左边界为数组的起始位置。
    int right = numsSize - 1;     // 定义右边界为数组的结束位置。

    // 当左边界不大于右边界时,执行循环。
    while (left <= right) {
        int mid = left + (right - left) / 2;  // 计算中间位置的索引。
        // 避免(left + right)可能导致的整数溢出。

        // 如果中间位置的元素等于目标值,则返回当前的中间位置索引。
        if (nums[mid] == target) {
            return mid;
        } else if (nums[mid] < target) {
            // 如果中间位置的元素小于目标值,说明目标值在数组中间位置的右侧。
            // 因此,将左边界移动到中间位置的右边一个位置。
            left = mid + 1;
        } else {
            // 如果中间位置的元素大于目标值,说明目标值在数组中间位置的左侧。
            // 因此,将右边界移动到中间位置的左边一个位置。
            right = mid - 1;
        }
    }

    // 如果循环结束后没有找到目标值,返回 -1。
    return -1;
}

循环中的条件是left <right。

int search(int* nums, int numsSize, int target) {
    int left = 0;
    int right = numsSize; // 注意这里不再是 numsSize - 1

    while (left < right) { // 循环条件现在是 left < right
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) {
            return mid; // 找到目标直接返回索引
        } else if (nums[mid] < target) {
            left = mid + 1; // 目标在中点右侧
        } else {
            right = mid; // 目标在中点左侧或者是中点,注意这里不再是 mid - 1
        }
    }

    // 循环结束后,如果目标元素是数组中最后一个元素,那么 left == right == numsSize - 1
    // 但此时还没有检查 nums[left] 是否等于 target
    // 所以需要额外检查一次
    if (left < numsSize && nums[left] == target) {
        return left;
    }

    return -1; // 如果没有找到目标,返回 -1
}

### 力扣LeetCode)第49题C语言解决方案 对于力扣LeetCode)上的第49题,即《字母异位词分组》,此题目要求编写一种方法来解决给定字符串列表按字母异位词组合的问题。下面提供了一种基于哈希表的方法实现这一功能,在C语言中的具体实现如下: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 10007 /* 哈希表大小 */ typedef struct Node { char *key; int val[10]; int cnt; // 数组val中有效元素数量 struct Node *next; } HashItem; /* 创建新的节点 */ HashItem *hashCreateNode(char *key, int idx) { HashItem *obj = (HashItem *)malloc(sizeof(HashItem)); obj->key = key; obj->cnt = 1; obj->val[obj->cnt - 1] = idx; obj->next = NULL; return obj; } unsigned long hashFunc(char *str) { unsigned long h = 0; while (*str != '\0') { h = h * 31 + *str++; } return h % MAX_SIZE; } void insertIntoBucket(HashItem **bucket, char *s, int sSize, int pos) { char temp[sSize + 1]; strcpy(temp,s); qsort(temp,strlen(temp),sizeof(char),strcmp); HashItem *item = NULL; unsigned long index = hashFunc(temp); item = bucket[index]; if (!item){ char* newKey=(char*)calloc(sSize+1,sizeof(char)); strcpy(newKey,temp); bucket[index]=hashCreateNode(newKey,pos); return ; } while(item){ if(strcmp(item->key,temp)==0){ item->val[item->cnt++]=pos; free(temp); return ; }else{ if(!item->next){ char* newKey=(char*)calloc(strlen(temp)+1,sizeof(char)); strcpy(newKey,temp); item->next=hashCreateNode(newKey,pos); return ; } item=item->next; } } } /** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). */ int** groupAnagrams(char ** strs, int strsSize, int* retSize, int** colSize){ HashItem *hashtable[MAX_SIZE] = {NULL}; memset(hashtable, 0 , sizeof hashtable); for(int i=0;i<strsSize;++i){ insertIntoBucket(hashtable,strs[i],strlen(strs[i]),i); } int count = 0; for(int i=0;i<MAX_SIZE;++i){ HashItem *pEntry = hashtable[i]; while(pEntry!=NULL){ ++count; pEntry=pEntry->next; } } int **retArr =(int**)malloc(count*sizeof(int *)); *colSize = (int*)malloc(count * sizeof(int)); int k = 0; for(int j=0;j<MAX_SIZE;++j){ HashItem *entry = hashtable[j]; while(entry!=NULL){ retArr[k]=(int*)malloc(entry->cnt * sizeof(int)); memcpy(retArr[k++], entry->val, entry->cnt * sizeof(int)); entry=entry->next; } } *retSize=count; return retArr; } ``` 上述代码实现了对输入字符串数组按照字母异位词进行分类的功能[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

琪琪的小白先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值