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

运行结果


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

相关推荐
程序员阿鹏5 分钟前
为什么MySQL InnoDB选择B+树?
数据结构·数据库·b树·sql·mysql·算法·缓存
yutian060616 分钟前
C: Unix UTC 时间戳转为 UTC+8 北京时间
c语言·unix
AI 思录27 分钟前
Prompt 事故档案(八):日常表达被标为“待校准”,AI 的爹味语法从哪里来
大数据·人工智能·算法·prompt·用户体验·ai合规
月光船幽幽32 分钟前
跨范式映射的稳定接口设计
人工智能·python·算法
2601_965742222 小时前
全媒体运营与短视频代运营,两者有什么区别?
大数据·数据结构·人工智能·算法·ai·媒体
wordbaby3 小时前
混合检索:两全其美的艺术
人工智能·算法
彧azz3 小时前
数据结构:关于图的学习
c语言·数据结构·笔记·学习·算法
热心网友俣先生3 小时前
A题-药材的烘干问题-题意逐句翻译
算法·数学建模
2601_960356383 小时前
2026秋招运营分析师能力模型:SQL、指标体系、AIGC与业务分析
算法
hetao17338373 小时前
校内场-提高组 ZYZSC-S-Round 4
c++·算法