Leetcode—1410.HTML实体解析器【中等】

2023每日刷题(三十八)

Leetcode---1410.HTML实体解析器

算法思想

实现代码

python 复制代码
typedef struct entityChar {
    char* entity;
    char rechar;
}entity;

entity matches[] = {
    {""", '"'},
    {"'", '\''},
    {"&", '&'},
    {">", '>'},
    {"&lt;", '<'},
    {"&frasl;", '/'}
};

char* entityParser(char* text) {
    int n = strlen(text);
    char* ans = (char *)malloc(sizeof(char) * (n + 10));
    char* p = ans;
    int flag = 0;
    int i = 0;
    while(i < n) {
        flag = 0;
        if(text[i] == '&') {
            for(int j = 0; j < sizeof(matches) / sizeof(matches[0]); j++) {
                if(strncmp(text + i, matches[j].entity, strlen(matches[j].entity)) == 0) {
                    strcpy(p, &matches[j].rechar);
                    p += strlen(&matches[j].rechar);
                    i += strlen(matches[j].entity);
                    flag = 1;
                    break;
                }
            }
        }
        if(!flag) {
            *p = text[i];
            p++;
            i++;
        }
    }
    *p = '\0';
    return ans;
}

运行结果


之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
青少儿编程课堂2 分钟前
2026蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:堆排序与优先队列解析
c++·python·算法·bfs·信息学竞赛
SZYSK43 分钟前
OpenHarmony 上读卡、推理和双屏刷新抢 CPU:核验机与消费机的调度差
物联网·算法·harmonyos·1024程序员节
东鹏特饮1 小时前
本机状态监控页面html
运维·css·html·css3
FLJwu1 小时前
《运动相机硬件防护选型实战》专栏|第二篇(PPTC 自恢复保险丝)
android·经验分享·数码相机·1024程序员节·运动相机
软件技术NINI1 小时前
基于SpringBoot和Vue的摇滚乐鉴赏网站
vue.js·spring boot·后端·spring·html·毕业设计·毕设
Ivanqhz1 小时前
MoE + LLM 应用
数据库·人工智能·pytorch·python·深度学习·算法·1024程序员节
系统集成-鹰翔1 小时前
ERP对接升级提示数据库迁移失败:SQLite逐版本事务到底回滚到哪一步
javascript·数据库·经验分享·后端
lpfasd1231 小时前
推荐算法进化史
算法·机器学习·推荐算法
Pniubi1 小时前
力扣543二叉树的直径(深度优先搜索法(递归))
算法·leetcode·职场和发展
暴走约伯2 小时前
【VFX工具链源码拆解 01】总体架构:大型项目特效流程的程序化落地
开发语言·经验分享·3d·unity·架构·游戏引擎