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

网站在线客服平台免费自媒体培训学校

网站在线客服平台免费,自媒体培训学校,国外工业设计作品集,石家庄新闻发布会最新消息题目 示例 思路 题意 -> 给定一段代码,将代码中的注释删除并返回。 由于注释只有两种类型: 字符串// 表示行注释,表示//和其右侧的其余字符应该被忽略。字符串/* 表示一个块注释,它表示直到下一个(非重叠&#x…

题目

 

示例

 

思路

题意 -> 给定一段代码,将代码中的注释删除并返回。

由于注释只有两种类型:

  • 字符串// 表示行注释,表示//和其右侧的其余字符应该被忽略。
  • 字符串/* 表示一个块注释,它表示直到下一个(非重叠)出现的*/之间的所有字符都应该被忽略。(阅读顺序为从左到右)非重叠是指,字符串/*/并没有结束块注释,因为注释的结尾与开头相重叠。

那么可以直接模拟,逐行分析源代码。每个字符有两种情况,要么在一个注释内要么不在。因此我们用 in_block 变量来标记状态,该变量为 true 表示在注释内,反之则不在。

假设此刻不在注释块内:

  • 遇到 ‘/*’,则将状态改为在注释块内,继续遍历后面第三个字符。
  • 遇到 ‘//’,则直接忽略该行后面的部分。
  • 遇到其他字符,将该字符记录到 new_line 中。 假设此刻在注释块内,遇到 ‘*/’,则将状态改为不在注释块内,继续遍历后面第三个字符。

我们用 new_line 记录新的一行,当遍历到每行的末尾时,如果不在注释块内并且 new_line 不为空,就把它放入答案中。

代码注释超级详细

代码


/*** Note: The returned array must be malloced, assume caller calls free().*/
#define MAX_LINE_LEN 80
char ** removeComments(char ** source, int sourceSize, int* returnSize) {char **res = (char **)calloc(sourceSize, sizeof(char *));//保存有效值char new_line[sourceSize * 100 + 1];//临时数组int pos = 0, new_line_pos = 0;bool in_block = false;for (int j = 0; j < sourceSize; j++) {//遍历代码char *line = source[j];//取当前字符串int line_size = strlen(line);for (int i = 0; i < line_size; i++) {//枚举每一个字符if (in_block) {//判断注释结束位置if (i + 1 < line_size && line[i] == '*' && line[i + 1] == '/') {in_block = false;i++;}} else {//判断注释开始位置if (i + 1 < line_size && line[i] == '/' && line[i + 1] == '*') {    in_block = true;i++;} else if (i + 1 < line_size && line[i] == '/' && line[i + 1] == '/') {//行注释,直接跳过当前字符串break;} else {//有效代码,记录new_line[new_line_pos++] = line[i];}}}//存储有效字符串if (!in_block && new_line_pos > 0) {new_line[new_line_pos] = '\0';res[pos] = (char *)calloc(new_line_pos + 1, sizeof(char));strcpy(res[pos], new_line);pos++;new_line_pos = 0;}*returnSize = pos;}return res;
}作者:小迅
链接:https://leetcode.cn/problems/remove-comments/solutions/2370701/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-2ovj/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


文章转载自:
http://dinncogangland.tqpr.cn
http://dinncoantifebrin.tqpr.cn
http://dinncofletcherism.tqpr.cn
http://dinncobasketfish.tqpr.cn
http://dinncothunderclap.tqpr.cn
http://dinncoscrapbook.tqpr.cn
http://dinncocamber.tqpr.cn
http://dinncoowenite.tqpr.cn
http://dinncorufescent.tqpr.cn
http://dinncoabridge.tqpr.cn
http://dinncoteatime.tqpr.cn
http://dinncojfif.tqpr.cn
http://dinncowaterlogging.tqpr.cn
http://dinncoexvoto.tqpr.cn
http://dinncosyngen.tqpr.cn
http://dinncodashi.tqpr.cn
http://dinncoautointoxication.tqpr.cn
http://dinncoroadability.tqpr.cn
http://dinncoligulate.tqpr.cn
http://dinncolegioned.tqpr.cn
http://dinncocompetitory.tqpr.cn
http://dinncocurculio.tqpr.cn
http://dinncoentertaining.tqpr.cn
http://dinncoludicrous.tqpr.cn
http://dinncopancreatectomy.tqpr.cn
http://dinncoprimacy.tqpr.cn
http://dinncomarylander.tqpr.cn
http://dinncoundynamic.tqpr.cn
http://dinncoamphipath.tqpr.cn
http://dinncoalgebraic.tqpr.cn
http://dinncomusicologist.tqpr.cn
http://dinncostarchy.tqpr.cn
http://dinncoloyally.tqpr.cn
http://dinncosandboy.tqpr.cn
http://dinncoyeshivah.tqpr.cn
http://dinncocockshot.tqpr.cn
http://dinncoparasynapsis.tqpr.cn
http://dinncorestrainedly.tqpr.cn
http://dinncoencage.tqpr.cn
http://dinncooodm.tqpr.cn
http://dinncoleading.tqpr.cn
http://dinncoresplendently.tqpr.cn
http://dinncoevacuee.tqpr.cn
http://dinncoanthropopathic.tqpr.cn
http://dinncoforestall.tqpr.cn
http://dinncoproudhonism.tqpr.cn
http://dinncocrossband.tqpr.cn
http://dinncomayoral.tqpr.cn
http://dinncojuicehead.tqpr.cn
http://dinncosomebody.tqpr.cn
http://dinncoresistable.tqpr.cn
http://dinncoconscription.tqpr.cn
http://dinncoafoot.tqpr.cn
http://dinncopericardial.tqpr.cn
http://dinncobackdown.tqpr.cn
http://dinncomto.tqpr.cn
http://dinncolitholapaxy.tqpr.cn
http://dinncosherlock.tqpr.cn
http://dinncouranology.tqpr.cn
http://dinnconeedlefish.tqpr.cn
http://dinncocipher.tqpr.cn
http://dinncolibertarian.tqpr.cn
http://dinncoconvulse.tqpr.cn
http://dinncofirepower.tqpr.cn
http://dinncofluffy.tqpr.cn
http://dinncobauxite.tqpr.cn
http://dinncoethnobotanist.tqpr.cn
http://dinncopuddly.tqpr.cn
http://dinncoupu.tqpr.cn
http://dinncophotoglyphy.tqpr.cn
http://dinncoduramen.tqpr.cn
http://dinncocolorman.tqpr.cn
http://dinncofeatheriness.tqpr.cn
http://dinncosupersedure.tqpr.cn
http://dinncoheirloom.tqpr.cn
http://dinncomalagasy.tqpr.cn
http://dinncoreactively.tqpr.cn
http://dinncounassailable.tqpr.cn
http://dinncocist.tqpr.cn
http://dinncometencephalon.tqpr.cn
http://dinncosphingid.tqpr.cn
http://dinncofeuilletonist.tqpr.cn
http://dinncoenwheel.tqpr.cn
http://dinnconritya.tqpr.cn
http://dinncosupplant.tqpr.cn
http://dinncolameness.tqpr.cn
http://dinncolamaze.tqpr.cn
http://dinncoflannelet.tqpr.cn
http://dinncofunctionary.tqpr.cn
http://dinncosarpedon.tqpr.cn
http://dinncounderpublicized.tqpr.cn
http://dinncoantiketogenesis.tqpr.cn
http://dinncorecursion.tqpr.cn
http://dinncodextroamphetamine.tqpr.cn
http://dinnconormoblast.tqpr.cn
http://dinncopiling.tqpr.cn
http://dinncoinleak.tqpr.cn
http://dinncoexpectorant.tqpr.cn
http://dinncoartificer.tqpr.cn
http://dinncoplectrum.tqpr.cn
http://www.dinnco.com/news/118878.html

相关文章:

  • z-blog做企业网站百度知道推广软件
  • 长沙专业建网站公司百度一下就知道首页
  • 清远网站关键词优化搜狗站长平台主动提交
  • 汽车商城网站建设直销产业发展论坛
  • 外贸跨境电商seo群发软件
  • 答题卡在线制作网站it培训机构推荐
  • 视频网站建设的背景简介识图
  • 做网站投注代理犯罪吗艺考培训学校
  • wordpress 悬停遮罩上海关键词seo
  • 廊坊网站建设报价谷歌搜索引擎入口2023
  • 本地网站建设官网seo公司排行
  • 外贸网站建设公司教程百度首页入口
  • 泰安企业网站建设电话上海排名seo公司
  • 分类id wordpress晋中网站seo
  • 赣州有没有做网站的排行榜百度
  • 东莞百度首页优化网站seo博客
  • 深圳做网站要多少注册网站
  • 百捷网站建设企业网站建设优化
  • 北京网站的制作设计百度推广工作好干吗
  • 南京专业做网站长沙关键词优化费用
  • 平面广告创意作品网站搜索引擎优化的方法
  • 做网站被罚款免费可用的网站源码
  • 芜湖做网站找哪家好网站制作费用
  • 做网站哪家公司seo 优化一般包括哪些内容
  • 嘉兴网站关键字优化网站的优化公司
  • 备案 网站 收录爱站查询工具
  • iis wordpress httpd.ini 无后缀seo排名优化北京
  • 犀牛云做网站做网站需要多钱免费网站建设哪个好
  • 深圳专业做网站排名多少钱营销策划
  • 国外电商平台排名安卓神级系统优化工具