【力扣】722. 删除注释

以下为力扣官方题解,及本人代码

题目

题意

给一个 C + + C++ C++ 程序,删除程序中的注释。这个程序 s o u r c e source source 是一个数组,其中 s o u r c e [ i ] source[i] source[i] 表示第 i i i 行源码。 这表示每行源码由 \n 分隔。
C + + C++ C++ 中有两种注释风格,行内注释和块注释。

  • 字符串 / / // // 表示行注释,表示 / / // // 和其右侧的其余字符应该被忽略。
  • 字符串 / ∗ /* / 表示一个块注释,它表示直到下一个(非重叠)出现的 ∗ / */ / 之间的所有字符都应该被忽略。(阅读顺序为从左到右)非重叠是指,字符串 / ∗ / /*/ // 并没有结束块注释,因为注释的结尾与开头相重叠。
    第一个有效注释优先于其他注释。
  • 如果字符串 / / // // 出现在块注释中会被忽略。
  • 同样,如果字符串 / ∗ /* / 出现在行或块注释中也会被忽略。
    如果一行在删除注释之后变为空字符串,那么不要输出该行。即,答案列表中的每个字符串都是非空的。
    样例中没有控制字符,单引号或双引号字符。
  • 比如, s o u r c e = " s t r i n g s = " / ∗ N o t a c o m m e n t . ∗ / " ; " source = "string s = "/* Not a comment. */";" source="strings="/Notacomment./";" 不会出现在测试样例里。

此外,没有其他内容(如定义或宏)会干扰注释。
我们保证每一个块注释最终都会被闭合, 所以在行或块注释之外的 / ∗ /* / 总是开始新的注释。
最后,隐式换行符可以通过块注释删除。 有关详细信息,请参阅下面的示例。
从源代码中删除注释后,需要以相同的格式返回源代码。

示例 1

输入:source = [“/*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;”,”}”]
解释:示例代码可以编排成这样:
/*Test program */
int main(){
// variable declaration
int a, b, c;
/* This is a test
multiline
comment for
testing */
a = b + c;
}
1 1 1 行和第 6 − 9 6-9 69 行的字符串 / ∗ /* / 表示块注释。第 4 4 4 行的字符串 / / // // 表示行注释。
编排后:
int main()
{
.
int a, b, c;
a = b + c;
}

示例 2

输入: source = [“a/*comment”, “line”, “more_comment*/b”]
输出: [“ab”]
解释: 原始的 s o u r c e source source 字符串是 “a/*comment\nline\nmore_comment*/b”,其中 \n 是换行符。删除注释后,隐含的换行符被删除,留下字符串 " a b " "ab" "ab" 用换行符分隔成数组时就是 [ " a b " ] ["ab"] ["ab"]

提示

  • 1 < = s o u r c e . l e n g t h < = 100 1 <= source.length <= 100 1<=source.length<=100
  • 0 < = s o u r c e [ i ] . l e n g t h < = 80 0 <= source[i].length <= 80 0<=source[i].length<=80
  • s o u r c e [ i ] source[i] source[i] 由可打印的 A S C I I ASCII ASCII 字符组成。
  • 每个块注释都会被闭合。
  • 给定的源码中不会有单引号、双引号或其他控制字符。

官方题解

模拟

思路与算法

我们需要逐行分析源代码。每个字符有两种情况,要么在一个注释内要么不在。因此我们用 i n _ b l o c k in\_block in_block 变量来标记状态,该变量为 t r u e true true 表示在注释内,反之则不在。
假设此刻不在注释块内:

  • 遇到 ‘ / ∗ /* /’,则将状态改为在注释块内,继续遍历后面第三个字符。
  • 遇到 ‘ / / // //’,则直接忽略该行后面的部分。
  • 遇到其他字符,将该字符记录到 n e w l i n e new_line newline 中。
    假设此刻在注释块内,遇到 ‘ ∗ / */ /’,则将状态改为不在注释块内,继续遍历后面第三个字符。
    我们用 n e w _ l i n e new\_line new_line 记录新的一行,当遍历到每行的末尾时,如果不在注释块内并且 n e w _ l i n e new\_line new_line 不为空,就把它放入答案中。

复杂度

  • 时间复杂度: O ( n m ) O(nm) O(nm)。其中 n n n s o u r c e source source 的长度, m m m s o u r c e [ i ] source[i] source[i] 的最大长度。
  • 空间复杂度: O ( n m ) O(nm) O(nm)。在极端情况下,每一行的隐式换行符都被块注释删除, n e w l i n e new_line newline 的长度将会达到 O ( n m ) O(nm) O(nm)

本人代码

Java

class Solution {
    public List<String> removeComments(String[] source) {
        List<String> ans = new ArrayList<>();
        StringBuilder newStr = new StringBuilder();
        boolean inBlock = false;
        for (String line : source) {
            for (int i = 0; i < line.length(); i++) {
                if (inBlock) {
                    if (i + 1 < line.length() && line.charAt(i) == '*' && line.charAt(i + 1) == '/') {
                        inBlock = false;
                        //跳过注释符号
                        i++;
                    } 
                } else {
                    if (i + 1 < line.length() && line.charAt(i) == '/' && line.charAt(i + 1) == '*') {
                        //跳过注释符号
                        inBlock = true;
                        i++;
                    } else if (i + 1 < line.length() && line.charAt(i) == '/' && line.charAt(i + 1) == '/') {
                        //跳过单行注释的内容
                        break;
                    } else {
                        newStr.append(line.charAt(i));
                    }
                }
            }
            if (!inBlock && newStr.length() > 0) {
                ans.add(newStr.toString());
                //newStr 长度重置为 0,多次使用
                newStr.setLength(0);
            }
        }
        
        return ans;
    }
}

提交结果:通过

  • 执行用时: 0 m s 0ms 0ms
  • 内存消耗: 39.7 M B 39.7MB 39.7MB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨momo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值