标签:index for pos any inf return 时间 res 遇到
时间复杂度O(n),空间复杂度不会。。
class Solution {
public:
string entityParser(string text) {
string result;
string shuangyinhao = """;
string danyinhao = "'";
string yufuhao = "&";
string dayuhao = ">";
string xiaoyuhao = "<";
string xiexianhao = "⁄";
for(int i = 0; i < text.length(); i++){
if(text[i] != ‘&‘){
result.append(1, text[i]);
continue;
}
else{
int record = 1;
int index = i + 1;
int flag = 1;
if(text[index] == ‘a‘){
record++;
if(index + 1 < text.length() && text[index+1] == ‘p‘){
for(int j = index + 1, k = 2; j < text.length(); j++, k++){
record++;
if(danyinhao[k] != text[j]){
flag = 0;
break;
}
if(k == danyinhao.length() - 1) break;
}
if(flag) result.push_back(39);
}
if(index + 1 < text.length() && text[index+1] == ‘m‘){
for(int j = index + 1, k = 2; j < text.length(); j++, k++){
record++;
if(yufuhao[k] != text[j]){
flag = 0;
break;
}
if(k == yufuhao.length() - 1) break;
}
if(flag) result.push_back(‘&‘);
}
}
if(text[index] == ‘q‘){
record++;
for(int j = index + 1, k = 2; j < text.length(); j++, k++){
record++;
if(shuangyinhao[k] != text[j]){
flag = 0;
break;
}
if(k == shuangyinhao.length() - 1) break;
}
if(flag) result.push_back(‘"‘);
}
if(text[index] == ‘g‘){
record++;
for(int j = index + 1, k = 2; j < text.length(); j++, k++){
record++;
if(dayuhao[k] != text[j]){
flag = 0;
break;
}
if(k == dayuhao.length() - 1) break;
}
if(flag) result.push_back(‘>‘);
}
if(text[index] == ‘l‘){
record++;
for(int j = index + 1, k = 2; j < text.length(); j++, k++){
record++;
if(xiaoyuhao[k] != text[j]){
flag = 0;
break;
}
if(k == xiaoyuhao.length() - 1) break;
}
if(flag) result.push_back(‘<‘);
}
if(text[index] == ‘f‘){
record++;
for(int j = index + 1, k = 2; j < text.length(); j++, k++){
record++;
if(xiexianhao[k] != text[j]){
flag = 0;
break;
}
if(k == xiexianhao.length() - 1) break;
}
if(flag) result.push_back(‘/‘);
}
if(!flag)
for(int j = i; j < i + record; j++) result.append(1, text[j]);
i += (record - 1);
}
}
return result;
}
};
标签:index for pos any inf return 时间 res 遇到
原文地址:https://www.cnblogs.com/wasi-991017/p/12684853.html