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

运行结果


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

相关推荐
张李浩7 小时前
Leetcode 054螺旋矩阵 采用方向数组解决
算法·leetcode·矩阵
big_rabbit05027 小时前
[算法][力扣101]对称二叉树
数据结构·算法·leetcode
美好的事情能不能发生在我身上7 小时前
Hot100中的:贪心专题
java·数据结构·算法
2301_821700537 小时前
C++编译期多态实现
开发语言·c++·算法
xixihaha13248 小时前
C++与FPGA协同设计
开发语言·c++·算法
小小怪7508 小时前
C++中的函数式编程
开发语言·c++·算法
xixixiLucky8 小时前
编程入门算法题---小明爬楼梯求爬n层台阶一共多少种方法
算法
剑锋所指,所向披靡!9 小时前
数据结构之线性表
数据结构·算法
Yupureki9 小时前
《MySQL数据库基础》1. 数据库基础
c语言·开发语言·数据库·c++·mysql·oracle·github
m0_706653239 小时前
使用C-Free进行浮点变量的四则运算指南
c语言·开发语言