当前位置: 首页 > article >正文

leetcode刷题第十天——栈与队列Ⅱ

本次刷题顺序是按照卡尔的代码随想录中给出的顺序

1047. 删除字符串中的所有相邻重复项

char* removeDuplicates(char* s) {
    int len = strlen(s);
    char* tmp = malloc(sizeof(char) * (len + 1));
    int top = -1, idx = 0;
    while(idx < len) {
        if(top == -1) tmp[++top] = s[idx];
        else {
            if(tmp[top] == s[idx]) top--;
            else tmp[++top] = s[idx];
        }
        idx++;
    }
    tmp[++top] = '\0';
    return tmp;
}

150. 逆波兰表达式求值

bool isNumber(char* ch) {
    if(strlen(ch) > 1 || '0' <= ch[0] && ch[0] <= '9') return true;
    else return false;
}

int evalRPN(char** tokens, int tokensSize) {
    int* st = malloc(sizeof(int)* tokensSize);
    int idx = 0, top = -1;
    while(idx < tokensSize) {
        char* ch = tokens[idx];
        if(!isNumber(ch)){
            switch(ch[0]) {
                case '+': st[top - 1] += st[top]; top--; break;
                case '-': st[top - 1] -= st[top]; top--; break;
                case '*': st[top - 1] *= st[top]; top--; break;
                case '/': st[top - 1] /= st[top]; top--; break;
            }
        }else {
            st[++top] = atoi(ch);
        }
        idx++;
    }
    return st[top];
}

347. 前 K 个高频元素

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

typedef struct {
    int key;
    int value;
    UT_hash_handle hh;
}hash;

typedef struct {
    int H_key;
    int H_value;
}Heap;

Heap* obj;
int Heap_size;

bool cmp(Heap obj1, Heap obj2) {
    return obj1.H_value < obj2.H_value;
}

void swap(Heap* obj1, Heap* obj2) {
    Heap tmp = *obj1;
    *obj1 = *obj2;
    *obj2 = tmp;
}

Heap H_Top() {
    return obj[1];
}

void H_Pop() {
    obj[1] = obj[Heap_size--];
    int idx = 1, t_idx;
    while((idx << 1) <= Heap_size) {//当前结点有子结点时,继续循环
        t_idx = idx << 1;
        if(t_idx < Heap_size && cmp(obj[t_idx + 1], obj[t_idx])) t_idx++;
        if(cmp(obj[idx], obj[t_idx])) return;
        swap(&obj[t_idx], &obj[idx]);
        idx = t_idx;
    }
}

void H_Push(hash x) {
    obj[++Heap_size].H_key = x.key;
    obj[Heap_size].H_value = x.value;
    int idx = Heap_size, t_idx;
    while(idx > 1) {
        t_idx = idx >> 1;
        if(cmp(obj[t_idx], obj[idx])) return;
        swap(&obj[t_idx], &obj[idx]);
        idx = t_idx;
    }
}

int* topKFrequent(int* nums, int numsSize, int k, int* returnSize) {
    hash* ha = NULL, * tmp1, * tmp2;
    int idx = 0;
    while(idx < numsSize) {//将数据信息存入哈希表
        HASH_FIND_INT(ha, &nums[idx], tmp1);
        if(tmp1 == NULL) {
            tmp1 = malloc(sizeof(hash));
            tmp1->key = nums[idx];
            tmp1->value = 1;
            HASH_ADD_INT(ha, key, tmp1);
        }else {
            tmp1->value++;
        }
        idx++;
    }

    obj = malloc(sizeof(Heap) * (k + 1));
    Heap_size = 0;
    Heap mid;

    HASH_ITER(hh, ha, tmp1, tmp2) {
        if (Heap_size == k) {
            mid = H_Top();
            if (mid.H_value < tmp1->value) {
                H_Pop();
                H_Push(*tmp1);
            }
        } else {
            H_Push(*tmp1);
        }
    }

    // 返回结果
    *returnSize = k;
    int* ret = malloc(sizeof(int) * k);
    for (int i = 0; i < k; i++) {
        mid = H_Top();
        H_Pop();
        ret[i] = mid.H_key;
    }
    return ret;
}

栈比较适合求解括号匹配、字符去重、逆波兰表达式

今天刷题,顺带复习了哈希表和小根堆的内容。小根堆和大根堆的思维框架是完全二叉树,所以使用数组实现小根堆时,有骚操作,在调整过程中,很妙,多看几遍H_Pop和H_Push的代码,好好体会

写这个系列的博客主要是给自己一个坚持刷题的理由,今天是第十天,感觉敲代码的流畅度有所提高,虽然思维上没有质变,但是相信坚持下去一定会有更大的收获,奥里给!


http://www.kler.cn/a/546056.html

相关文章:

  • DeepSeek、Kimi、文心一言、通义千问:AI 大语言模型的对比分析
  • Django REST framework 源码剖析-渲染器图解(Renderers)
  • C++中接口与继承的区别(自我学习用)
  • 【鸿蒙开发】第三十四章 DevEco Studio - 故障分析汇总
  • c语言中和||
  • Docker容器基本操作
  • 【前端】【vue】vue2/3,nuxt的插槽使用详解
  • flutter hive使用(复杂类)
  • [qt5学习笔记]用vs2022(msvc2017)+copilot进行QtWidgetsApplication源码解析
  • ECCV2022 | LGV | LGV:利用大几何邻域提升对抗样本的可迁移性
  • MySQL中rank()、row_number()、dense_rank()排序
  • 【Elasticsearch】运行时字段(Runtime Fields)索引时定义运行时字段
  • CES Asia 2025:构建长效价值运营体系,赋能科技产业新发展
  • 游戏引擎学习第103天
  • 利用prompt技术结合大模型对目标B/S架构软件系统进行测试
  • docker 的使用
  • 第6章 6.4 ASP.NET Core Web API各种技术及选择
  • 常用共轭先验分布
  • DeepSeek 概述与本地化部署【详细流程】
  • Spring Cache 详细讲解