当前位置: 首页 > news >正文

【LeetCode-中等】722. 删除注释

题目链接

722. 删除注释

标签

字符串

步骤

Step1. 先将source合并为一个字符串进行处理,中间补上’\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 即可。

vector<string> ans;
int pos = tmpAns.find("\n", 0);
while (pos != string::npos) {if (pos != 0) {ans.push_back(tmpAns.substr(0, pos));}tmpAns.erase(0, pos+1);pos = tmpAns.find("\n", 0);
}

实现代码(C++)

class Solution {
public:vector<string> removeComments(vector<string>& source) {int state = 0;// 合并为一个字符串string combined;for (auto str : source) {combined += str + "\n";}vector<pair<int,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分割vector<string> ans;int pos = tmpAns.find("\n", 0);while (pos != string::npos) {if (pos != 0) {ans.push_back(tmpAns.substr(0, pos));}tmpAns.erase(0, pos+1);pos = tmpAns.find("\n", 0);}return ans;}
};
http://www.lryc.cn/news/107843.html

相关文章:

  • rust里如何判断字符串是否相等呢?
  • python基本知识学习
  • vue3和typescript_组件
  • Qt+联想电脑管家
  • 论文阅读-BotPercent: Estimating Twitter Bot Populations from Groups to Crowds
  • 用于永磁同步电机驱动器的自适应SDRE非线性无传感器速度控制(MatlabSimulink实现)
  • Spring Cloud+Spring Boot+Mybatis+uniapp+前后端分离实现知识付费平台免费搭建 qt
  • 删除注释(力扣)
  • 阿里云AK创建
  • OC与Swift的相互调用
  • 某银行软件测试笔试题
  • SpringMVC概述、SpringMVC的工作流程、创建SpringMVC的项目
  • 一文说清楚支付架构
  • 【Golang 接口自动化00】为什么要用Golang做自动化?
  • Android 架构模式如何选择
  • 深入了解 LoRaWAN® B 类设备
  • KK集团再闯港交所:引领潮流零售市场,2023年一季度业绩增势显著
  • Vue中的组件渲染
  • docker 保存和载入镜像
  • Java框架(九)--Spring Boot入门(1)
  • 2023年第四届“华数杯”数学建模思路 - 案例:随机森林
  • Redis中缓存穿透、击穿、雪崩以及解决方案
  • 系统架构设计师-软件架构设计(6)
  • Knife4j系列--解决不显示文件上传的问题
  • 深入学习Mysql引擎InnoDB、MylSAM
  • 第七章:SpringMVC中
  • MySQL数据库——DQL操作——基本查询
  • Electron 开发,报handshake failed; returned -1, SSL error code 1,错误
  • 知识区博主转型——兼做知识区和改造区博主!!!!!
  • Resnet与Pytorch花图像分类