单调栈和哈希表c语言题目:力扣496. 下一个更大元素 I(参考官解)

 1、假设有一组数据 5、7、3、4、6、2我们的目的是存储每一位元素的后一项比他大的第一个数,如若没有,则用-1代替。这里用用a[6]数组存储。

2、由于c语言具体实现栈操作较复杂,这里用stake[6]代替栈。

3、具体实现,我们从这一组数据中最后一个元素开始1思考,由于最后一位元素没有后继,故其后面没有比他大的,则初始a[5]=-1,并将该值放入栈中。我们接着往前遍历,6比2要大,这时2出栈(为什么出栈?因为6比2大了,假如前面比2小,肯定首先会比6小,所以2没有存在意义了),由于发现2比6小,所以令a[4]=-1。继续往前,这时发现,4比6小,所以a[3]=6,将4入栈,(6继续保留在栈中,防止出现比4大比六小的,这样只要出栈4,就可以和6比较了)。3比4小,a[2]=4,3入栈,7比3大,出栈3,7比4大,出栈4,7比6大,出栈6,此时栈空,故没有比7大的数,a[1]=-1,将7入栈。最后5小于7,a[0]=7,所有的元素都找到了他对应的最大值。

4、综上

①初始条件,a[numsSize-1]=-1,stake[0]=nums[numsSize-1]。

②入栈条件:当遍历值小于栈顶,先储存栈顶值(为遍历点的目标值),再入栈。

③出栈条件:当遍历值大于栈顶元素,一直出栈,直到找到大于它的值或者栈空,若栈空,该点目标值为-1,再将该点入栈。

5,力扣这题,有了单调栈存储的前提,可直接用数组下标直接找出给定元素对应的最大值。


int* nextGreaterElement(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
int *ret=malloc(sizeof(int)*nums1Size);
int a[10001],i,stake[nums2Size],top=-1;//a[]数组为索引数组,stake[]为栈
*returnSize=0;
stake[++top]=nums2[nums2Size-1];
a[nums2[nums2Size-1]]=-1;//num2数组最后一个元素后面没有比他大的
for(i=nums2Size-1;i>0;i--)
{
    while(top!=-1&&stake[top]<nums2[i-1])//从栈中寻找比当前num2[i-1]小的值。
        top--;//未找到一直出栈栈里的元素
    if(top==-1)//未找到
    {
        a[nums2[i-1]]=-1;
    }
    else
    {
        a[nums2[i-1]]=stake[top]; 
    }
 stake[++top]=nums2[i-1];//不管找没找到,都要存的当前值
}
for(i=0;i<nums1Size;i++)
{
    ret[(*returnSize)++]=a[nums1[i]];//利用索引数组实现O(1)时间复杂度
}
return ret;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的哈希表C语言实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define TABLE_SIZE 100 typedef struct { char* key; char* value; } HashNode; typedef struct { HashNode** nodes; int size; } HashTable; unsigned int hash(const char* key) { unsigned int hash = 0; unsigned int i; for (i = 0; i < strlen(key); ++i) { hash += key[i]; hash += (hash << 10); hash ^= (hash >> 6); } hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15); return hash; } HashTable* create_table(int size) { HashTable* table = (HashTable*)malloc(sizeof(HashTable)); table->nodes = (HashNode**)calloc(size, sizeof(HashNode*)); table->size = size; return table; } void free_table(HashTable* table) { int i; for (i = 0; i < table->size; ++i) { HashNode* node = table->nodes[i]; if (node != NULL) { free(node->key); free(node->value); free(node); } } free(table->nodes); free(table); } void set(HashTable* table, const char* key, const char* value) { unsigned int index = hash(key) % table->size; HashNode* node = table->nodes[index]; while (node != NULL) { if (strcmp(node->key, key) == 0) { free(node->value); node->value = strdup(value); return; } node = node->next; } HashNode* new_node = (HashNode*)malloc(sizeof(HashNode)); new_node->key = strdup(key); new_node->value = strdup(value); new_node->next = table->nodes[index]; table->nodes[index] = new_node; } char* get(HashTable* table, const char* key) { unsigned int index = hash(key) % table->size; HashNode* node = table->nodes[index]; while (node != NULL) { if (strcmp(node->key, key) == 0) { return node->value; } node = node->next; } return NULL; } int main() { HashTable* table = create_table(TABLE_SIZE); set(table, "key1", "value1"); set(table, "key2", "value2"); printf("%s\n", get(table, "key1")); printf("%s\n", get(table, "key2")); free_table(table); return 0; } ``` 这个哈希表使用了链表法解决冲突,具体实现细节可以参考代码注释。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值