列表

详情


1410. HTML 实体解析器

「HTML 实体解析器」 是一种特殊的解析器,它将 HTML 代码作为输入,并用字符本身替换掉所有这些特殊的字符实体。

HTML 里这些特殊字符和它们对应的字符实体包括:

给你输入字符串 text ,请你实现一个 HTML 实体解析器,返回解析器解析后的结果。

 

示例 1:

输入:text = "& is an HTML entity but &ambassador; is not."
输出:"& is an HTML entity but &ambassador; is not."
解释:解析器把字符实体 & 用 & 替换

示例 2:

输入:text = "and I quote: "...""
输出:"and I quote: \"...\""

示例 3:

输入:text = "Stay home! Practice on Leetcode :)"
输出:"Stay home! Practice on Leetcode :)"

示例 4:

输入:text = "x > y && x < y is always false"
输出:"x > y && x < y is always false"

示例 5:

输入:text = "leetcode.com&frasl;problemset&frasl;all"
输出:"leetcode.com/problemset/all"

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: string entityParser(string text) { } };

golang 解法, 执行用时: 56 ms, 内存消耗: 7.6 MB, 提交时间: 2023-11-23 21:33:39

func entityParser(text string) string {
    entityMap := map[string]string{
        "&quot;": "\"",
        "&apos;": "'",
        "&gt;": ">",
        "&lt;": "<",
        "&frasl;": "/",
        "&amp;": "&",
    }

    i := 0
    n := len(text)
    res := make([]string, 0)
    for i < n {
        isEntity := false
        if text[i] == '&' {
            for k, v := range entityMap {
                if i + len(k) <= n && text[i : i + len(k)] == k {
                    res = append(res, v)
                    isEntity = true
                    i += len(k)
                    break
                }
            }
        }
        if !isEntity {
            res = append(res, text[i:i+1])
            i++
        }
    }
    return strings.Join(res, "")
}

javascript 解法, 执行用时: 244 ms, 内存消耗: 52.9 MB, 提交时间: 2023-11-23 21:33:14

/**
 * @param {string} text
 * @return {string}
 */
var entityParser = function(text) {
    const entityMap = {
        "&quot;": '"',
        "&apos;": "'",
        "&gt;": ">",
        "&lt;": "<",
        "&frasl;": "/",
        "&amp;": "&",
    };

    let i = 0;
    const n = text.length;
    const res = [];

    while (i < n) {
        let isEntity = false;
        if (text[i] === "&") {
            for (const [key, value] of Object.entries(entityMap)) {
                if (text.slice(i, i + key.length) === key) {
                    res.push(value);
                    isEntity = true;
                    i += key.length;
                    break;
                }
            }
        }
        if (!isEntity) {
            res.push(text[i]);
            i += 1;
        }
    }
    return res.join("");
};

javascript 解法, 执行用时: 128 ms, 内存消耗: 49.1 MB, 提交时间: 2023-06-01 10:05:21

/**
 * @param {string} text
 * @return {string}
 */
var entityParser = function (text) {
  let map = {
    "&quot;": '"',
    "&apos;": "'",
    "&amp;": "&",
    "&gt;": ">",
    "&lt;": "<",
    "&frasl;": "/",
  };
  let res = "";
  let i = 0,
    len = text.length;
  while (i < len) {
    let endIndex = text.indexOf(";", i);
    let cur = text.substring(i, endIndex + 1);
    if (text[i] == "&" && map[cur]) {
      res += map[cur];
      i = endIndex + 1;
    } else {
      res += text[i];
      i++;
    }
  }
  return res;
};

python3 解法, 执行用时: 72 ms, 内存消耗: 16.7 MB, 提交时间: 2023-06-01 10:04:53

class Solution:
    def entityParser(self, text: str) -> str:
        text=text.replace('&quot;','"\\')
        text=text.replace('&apos;',"'\\")
        text=text.replace('&amp;','&\\')
        text=text.replace('&gt;','>\\')
        text=text.replace('&lt;','<\\')
        text=text.replace('&frasl;','/\\')
        text=text.replace('\\','')
        return text

java 解法, 执行用时: 22 ms, 内存消耗: 43.2 MB, 提交时间: 2023-06-01 10:04:02

class Solution {
    public String entityParser(String text) {
       String reg1="&quot;";
       String reg2="&apos;";
       String reg3="&amp;";
       String reg4="&gt;";
       String reg5="&lt;";
       String reg6="&frasl;";
       return text.replace(reg1,"\"").replace(reg2,"'").replace(reg6,"/").replace(reg4,">").replace(reg5,"<").replace(reg3,"&");

    }
}

java 解法, 执行用时: 28 ms, 内存消耗: 43.2 MB, 提交时间: 2023-06-01 10:03:47

class Solution {
    public String entityParser(String text) {
        StringBuffer sb = new StringBuffer();
        int length = text.length();
        int entityStart = -1;
        for (int i = 0; i < length; i++) {
            char c = text.charAt(i);
            if (c == '&') {
                if (entityStart >= 0) {
                    sb.append(text.substring(entityStart, i));
                }
                entityStart = i;
            } else {
                if (entityStart >= 0) {
                    if (c == ';') {
                        String entity = text.substring(entityStart, i + 1);
                        String specialCharacter = getCharacter(entity);
                        sb.append(specialCharacter);
                        entityStart = -1;
                    }
                } else {
                    sb.append(c);
                }
            }
        }
        if (entityStart >= 0) {
            sb.append(text.substring(entityStart));
        }
        return sb.toString();
    }

    public String getCharacter(String entity) {
        switch (entity) {
        case "&quot;":
            return "\"";
        case "&apos;":
            return "\'";
        case "&amp;":
            return "&";
        case "&gt;":
            return ">";
        case "&lt;":
            return "<";
        case "&frasl;":
            return "/";
        default:
            return entity;
        }
    }
}

上一题