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

运行结果


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

相关推荐
__AtYou__3 分钟前
Golang | Leetcode Golang题解之第417题太平洋大西洋水流问题
leetcode·golang·题解
yanyanwenmeng4 分钟前
matlab基础
开发语言·算法·matlab
##晴天小猪6 分钟前
ByteTrack多目标跟踪流程图
人工智能·算法·目标检测·机器学习·目标跟踪
Ddddddd_15817 分钟前
C++ | Leetcode C++题解之第421题数组中两个数的最大异或值
c++·leetcode·题解
ly-how21 分钟前
leetcode练习 二叉树的层序遍历
算法·leetcode
疑惑的杰瑞32 分钟前
[数据结构]算法复杂度详解
c语言·数据结构·算法
大油头儿35 分钟前
排序算法-选择排序
数据结构·算法·排序算法
搞点夜点心42 分钟前
算法课习题汇总(2)
算法
大二转专业1 小时前
408算法题leetcode--第10天
考研·算法·leetcode
.别止步春天.1 小时前
Python中lambda表达式的使用——完整通透版
数据结构·python·算法