版权声明:本文为博主原创文章,博客地址:https://blog.csdn.net/qq_41855420,未经博主允许不得转载。 https://blog.csdn.net/qq_41855420/article/details/89641913
给一个 C++ 程序,删除程序中的注释。这个程序source是一个数组,其中source[i]表示第i行源码。 这表示每行源码由\n分隔。
在 C++ 中有两种注释风格,行内注释和块注释。
字符串// 表示行注释,表示//和其右侧的其余字符应该被忽略。
字符串/* 表示一个块注释,它表示直到*/的下一个(非重叠)出现的所有字符都应该被忽略。(阅读顺序为从左到右)非重叠是指,字符串/*/并没有结束块注释,因为注释的结尾与开头相重叠。
第一个有效注释优先于其他注释:如果字符串//出现在块注释中会被忽略。 同样,如果字符串/*出现在行或块注释中也会被忽略。
如果一行在删除注释之后变为空字符串,那么不要输出该行。即,答案列表中的每个字符串都是非空的。
样例中没有控制字符,单引号或双引号字符。比如,source = “string s = “/* Not a comment. */”;” 不会出现在测试样例里。(此外,没有其他内容(如定义或宏)会干扰注释。)
我们保证每一个块注释最终都会被闭合, 所以在行或块注释之外的/*总是开始新的注释。
最后,隐式换行符可以通过块注释删除。 有关详细信息,请参阅下面的示例。
从源代码中删除注释后,需要以相同的格式返回源代码。
示例 1:
输入:
source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"]
示例代码可以编排成这样:
/*Test program */
int main()
{
// variable declaration
int a, b, c;
/* This is a test
multiline
comment for
testing */
a = b + c;
}
输出: ["int main()","{ "," ","int a, b, c;","a = b + c;","}"]
编排后:
int main()
{
int a, b, c;
a = b + c;
}
解释:
第 1 行和第 6-9 行的字符串 /* 表示块注释。第 4 行的字符串 // 表示行注释。
示例 2:
输入:
source = ["a/*comment", "line", "more_comment*/b"]
输出: ["ab"]
解释: 原始的 source 字符串是 "a/*comment\nline\nmore_comment*/b", 其中我们用粗体显示了换行符。删除注释后,隐含的换行符被删除,留下字符串 "ab" 用换行符分隔成数组时就是 ["ab"].
注意:
source的长度范围为[1, 100].
source[i]的长度范围为[0, 80].
每个块注释都会被闭合。
给定的源码中不会有单引号、双引号或其他控制字符。
思路分析: 这道题看起来挺容易的,其实也是比较容易,蛋式领扣的测试数据往往让你怀疑赢生啊!
这里我们需要考虑块注释、行注释,我首先给各个道友几个我踩过的坑!!!
1、如果出现某一行为" /*/ declare members;/**/",处理后应该为" ",注意/*/重叠,并没有结束块注释,因为注释的结尾与开头相重叠。
2、如["a/*comment", "line", "more_comment*/b"],注意中间出现块注释,"ab"是连起来的,即使它们出现不同的行。
3、如果是["void func(int k) {", "// this function does nothing /*", " k = k*2/4;", " k = k/2;*/", "}"],处理之后应该是["void func(int k) {"," k = k*2/4;"," k = k/2;*/","}"],因为第二句"// this function does nothing /*"首先出现行注释,所以最后的"/*"无效,行注释后所有的字符都忽视
4、如果["a//*b//*c","blank","d/*/e*//f"],处理后是["a","blank","d/f"]。第一句"a//*b//*c"首先的出现的行注释,"d/*/e*//f"首先出现的块注释
不难发现一些 特征,
1、注释处理“/*/”与“/**/”的不同
2、如果行注释、块注释同时出现在一句,先处理出现下标靠前的。
3、如果出现块注释,则块注释之前连接的字符串与块注释之后连接的字符串在同一行。比如示例二
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
vector<string> result;
int index = 0, sourceSize = source.size();
//扫描每一行代码
while (index < sourceSize) {
if (source[index].empty()) {//代码为空,直接跳过
++index;
continue;
}
int indexOne = source[index].find("//");//检测行注释标志
int indexTwo = source[index].find("/*");//检测块注释起始标志
//第一种情况:处理行注释,蛋式必须保证出现"//",并且下标出现的比块注释出现的考前(或者没有块注释)
if (indexOne != string::npos && (indexTwo == -1 || indexOne < indexTwo)) {
//取出行注释前的字符段放入result,蛋式需要注意可能行注释前没有字符段,比如"//this function does nothing"
if (indexOne != 0){
result.push_back(source[index].substr(0, indexOne));
}
index += 1;
continue;
}
//第二种情况:处理块注释
if (indexTwo != string::npos) {
string before = source[index].substr(0, indexTwo);//先获取块注释起始标志前的字符段,比如"a/*comment"中的"a"
int indexThree = source[index].find("*/", indexTwo + 2);//这里indexTwo + 2表示的是在块注释起始后查找"*/",注意"/*/"
//可能块注释出现在同一行,比如"a/*comment*/abc/*he*/123"
if (indexThree != string::npos) {
//块注释出现在同一行,我们先挖掉第一个块注释,得到"aabc/*he*/123"
if (indexThree + 2 < source[index].size()) {
before += source[index].substr(indexThree + 2);
}
source[index] = before;//然后将"aabc/*he*/123"替换处理之前,然后继续处理
}
else {
//如果块注释出现在多行,比如["a/*comment", "line", "more_comment*/b//variable declaration"]
++index;
while (true) {
//移动index指针,直到找到了块注释的结束标志"*/"
int indexThree = source[index].find("*/");
if (indexThree != string::npos) {
//挖掉横跨多行的块注释得到"ab//variable declaration"
if (indexThree + 2 < source[index].size()) {
before += source[index].substr(indexThree + 2);
}
source[index] = before;//将挖掉块注释的代码替换处理之前的代码
break;
}
++index;
}
}
continue;//继续处理
}
//第三种情况:没有注释,直接放入到结果中即可
result.push_back(source[index++]);
}
return result;
}
};