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;
}

运行结果


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

相关推荐
小小小新人1212338 分钟前
C语言 ATM (4)
c语言·开发语言·算法
你的冰西瓜1 小时前
C++排序算法全解析(加强版)
c++·算法·排序算法
এ᭄画画的北北1 小时前
力扣-31.下一个排列
算法·leetcode
绝无仅有2 小时前
企微审批对接错误与解决方案
后端·算法·架构
无限远的弧光灯3 小时前
c语言学习_函数递归
c语言·开发语言·学习
用户5040827858393 小时前
1. RAG 权威指南:从本地实现到生产级优化的全面实践
算法
小林C语言3 小时前
C语言 | 指针小结
c语言
Python×CATIA工业智造4 小时前
详细页智能解析算法:洞悉海量页面数据的核心技术
爬虫·算法·pycharm