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

行业网站产品选择小程序制作多少钱

行业网站产品选择,小程序制作多少钱,云服务器费用,小程序制作封面题目链接 722. 删除注释 标签 字符串 步骤 Step1. 先将source合并为一个字符串进行处理,中间补上’\n’,方便后续确定注释开始、结束位置。 string combined; for (auto str : source) {combined str "\n"; }Step2. 定义数组 toDel&am…

题目链接

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.yayakq.cn/news/254629/

相关文章:

  • 响应式网站模板 食品电子商务电商网站饿建设
  • 短视频素材免费下载网站wordpress关键词插件吗
  • 长沙网上商城网站如何提升seo排名
  • 奇艺广州网站建设熊掌号重庆网站建设选卓光
  • 电子商务网站建设与管理的有关论文微信营销策略
  • 昆明网站代理excel做注册网站
  • 公司高端网站建做点效果图赚钱的网站
  • 怎样做网站策划系统ui设计界面
  • 买网站的域名wordpress 两个网站
  • 做网站需要要多少钱网站开发设计需求文档
  • 衡水做企业网站wordpress 新建分类目录
  • linux建立网站从化低价网站建设
  • 营销型旅游网站建设热点链接到另一个网站怎么做
  • 品牌网站建设有哪些方面做网站代理怎么赚钱
  • vs进行网站建设南宁市建筑规划设计集团有限公司
  • 用dedecms做两个一样的网站wordpress上传上限
  • 遵义网上制作网站wordpress页脚二维码
  • 网站发布到ftp济南seo培训
  • 保定建网站wordpress更换主题方法
  • 佛山仿站定制模板建站网站开发那个好
  • dw建设网站的代码模板怎样做网站发帖
  • 兴县做网站公司网络营销推广方法和应用场景
  • 有好的学网站建设的书吗旅游网站制作建设
  • 房产网站模板程序帝国cms做门户网站
  • 怎么做网站论坛软件开发网站开发培训
  • 做照片书的模板下载网站学院网站建设意义
  • 摄影网站免费网站域名不要了怎么做
  • 企业3合1网站建设什么游戏不用登录直接玩
  • 企业网站的功能模块网站开发学习教程
  • 网站被挂广告怎么办广州珈瑶公司是哪一年注册的