漂亮企业网站源码,中国建筑装饰装修,软件推广赚钱一个10元,英文网站模板源代码题目链接
722. 删除注释
标签
字符串
步骤
Step1. 先将source合并为一个字符串进行处理#xff0c;中间补上’\n’#xff0c;方便后续确定注释开始、结束位置。
string combined;
for (auto str : source) {combined str \n;
}Step2. 定义数组 toDel中间补上’\n’方便后续确定注释开始、结束位置。
string combined;
for (auto str : source) {combined str \n;
}Step2. 定义数组 toDel记录每一个注释开始、结束的位置进行状态转移。对于 /**/ 类型的注释的结束符 */如果匹配失败则需要回退一位。
状态转移表示如下
s0: if /: goto s1 # match /, change to s1else: goto s0
s1: if /: cut down this line, goto s0 # match //elif *: goto s2 # match /*else: goto s0 # match failed, return to s1
s2: if *: goto s3 # match * in comment; s2 means that the current state is InCommentelse: goto s2
s3: if /: end this comment, goto s0 # match */else: goto s3 # match failed, return to s2具体的代码部分如下
for (int i 0; i len; i) {char ch combined[i];switch (state) {case 0:if (ch /) {state 1;} else {state 0;}break;case 1:if (ch /) { // 出现//删去此行之后的内容// 找到从当前下标开始的第一个\n下一次遍历从其之后开始int end combined.find(\n, i);cmtBegin i - 1;cmtEnd end - 1;toDel.push_back({cmtBegin, cmtEnd});state 0;i end;} else if (ch *) { // 出现/*state 2;cmtBegin i - 1;} else {state 0;}break;case 2:if (ch *) {state 3;} else {state 2;}break;case 3:if (ch /) { // cmtEndstate 0;cmtEnd i;toDel.push_back({cmtBegin, cmtEnd});} else { // 匹配*/失败回退一位state 2;i--;}break;}
}Step3. 遍历 toDel得到不含注释的中间结果 tmpAns。
// 遍历toDel得到删除注释的中间结果
string tmpAns;
int last 0;
for (auto p : toDel) {// 下标a和b-1之间的长度b-atmpAns combined.substr(last, p.first - last);last p.second 1;
}
// 分为toDel.size()1段的最后一段
tmpAns combined.substr(last, combined.length() - last);Step4. 根据 \n 来分割 tmpAns 即可。
vectorstring ans;
int pos tmpAns.find(\n, 0);
while (pos ! string::npos) {if (pos ! 0) {ans.push_back(tmpAns.substr(0, pos));}tmpAns.erase(0, pos1);pos tmpAns.find(\n, 0);
}实现代码C
class Solution {
public:vectorstring removeComments(vectorstring source) {int state 0;// 合并为一个字符串string combined;for (auto str : source) {combined str \n;}vectorpairint,int toDel;int len combined.length();int cmtBegin -1, cmtEnd -1;for (int i 0; i len; i) {char ch combined[i];switch (state) {case 0:if (ch /) {state 1;} else {state 0;}break;case 1:if (ch /) { // 出现//删去此行之后的内容// 找到从当前下标开始的第一个\n下一次遍历从其之后开始int end combined.find(\n, i);cmtBegin i - 1;cmtEnd end - 1;toDel.push_back({cmtBegin, cmtEnd});state 0;i end;} else if (ch *) { // 出现/*state 2;cmtBegin i - 1;} else {state 0;}break;case 2:if (ch *) {state 3;} else {state 2;}break;case 3:if (ch /) { // cmtEndstate 0;cmtEnd i;toDel.push_back({cmtBegin, cmtEnd});} else { // 匹配*/失败回退一位state 2;i--;}break;} }// 遍历toDel得到删除注释的中间结果string tmpAns;int last 0;for (auto p : toDel) {tmpAns combined.substr(last, p.first - last);last p.second 1;}tmpAns combined.substr(last, combined.length() - last);// 根据\n分割vectorstring ans;int pos tmpAns.find(\n, 0);while (pos ! string::npos) {if (pos ! 0) {ans.push_back(tmpAns.substr(0, pos));}tmpAns.erase(0, pos1);pos tmpAns.find(\n, 0);}return ans;}
};