leetcode722 - Remove Comments - medium

Given a C++ program, remove comments from it. The program source is an array where source[i] is the i-th line of the source code. This represents the result of splitting the original source code string by the newline character \n.
In C++, there are two types of comments, line comments, and block comments.
The string // denotes a line comment, which represents that it and rest of the characters to the right of it in the same line should be ignored.
The string /* denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of */should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string /*/ does not yet end the block comment, as the ending would be overlapping the beginning.
The first effective comment takes precedence over others: if the string // occurs in a block comment, it is ignored. Similarly, if the string /* occurs in a line or block comment, it is also ignored.
If a certain line of code is empty after removing comments, you must not output that line: each string in the answer list will be non-empty.
There will be no control characters, single quote, or double quote characters. For example, source = "string s = "/* Not a comment. */";" will not be a test case. (Also, nothing else such as defines or macros will interfere with the comments.)
It is guaranteed that every open block comment will eventually be closed, so /* outside of a line or block comment always starts a new comment.
Finally, implicit newline characters can be deleted by block comments. Please see the examples below for details.
After removing the comments from the source code, return the source code in the same format.
Example 1:
Input:
source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"]
The line by line code is visualized as below:
/*Test program */
int main()
{
// variable declaration
int a, b, c;
/* This is a test
multiline
comment for
testing */
a = b + c;
}
Output: ["int main()","{ "," ","int a, b, c;","a = b + c;","}"]
The line by line code is visualized as below:
int main()
{
int a, b, c;
a = b + c;
}
Explanation:
The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
Example 2:
Input:
source = ["a/*comment", "line", "more_comment*/b"]
Output: ["ab"]
Explanation: The original source string is "a/*comment\nline\nmore_comment*/b", where we have bolded the newline characters. After deletion, the implicit newline characters are deleted, leaving the string "ab", which when delimited by newline characters becomes ["ab"].
Note:
* The length of source is in the range [1, 100].
* The length of source[i] is in the range [0, 80].
* Every open block comment is eventually closed.
* There are no single-quote, double-quote, or control characters in the source code.

实现题。
每一行的内容都有剩出一部分有意义code,作为newLine加入到结果的潜力。扫描每行,分别检索comment标志符”/*”, “*/“, “//“来进行相关逻辑处理。同时用一个全局变量isInBlock记忆当前的状态,是否在/**/里,来帮助逻辑的处理。
1.如果现在isInBlock,那就只想着找到”*/“。
2.如果现在!isInBlock,如果找到”/*”,即反转block状态了;如果找到”//“,那这行剩下都没用了;如果不是特殊标志符,那把这个有意义的char for code加到当前行的结果上。
一行扫完后,如果发现这行有加有意义内容,就加入最终结果里。

细节:
1.为了题目要求的a/*xxx, xxx, xxx*/c最后要拿到ac而不是a,c这种要求,要把newLine拿出去做全局变量,使得不同行可以放到结果的一行里。创造新行和添加结果的时候都加点小花边。
2.判断两个字符的时候注意一下边界检查。

实现:

class Solution {
    public List<String> removeComments(String[] source) {
        List<String> ans = new ArrayList<>();
        boolean isInBlock = false;
        String newLine = "";
        for (String line : source) {
            if (!isInBlock) newLine = "";
            for (int i = 0; i < line.length(); i++) {
                if (!isInBlock && i + 1 < line.length() && line.substring(i, i + 2).equals("/*")) {
                    isInBlock = true;
                    i++;
                } else if (isInBlock && i + 1 < line.length() && line.substring(i, i + 2).equals("*/")) {
                    isInBlock = false;
                    i++;
                } else if (!isInBlock && i + 1 < line.length() && line.substring(i, i + 2).equals("//")) {
                    break;
                } else if (!isInBlock){
                    newLine += line.charAt(i);
                }
            }
            if (!isInBlock && newLine.length() > 0) {
                ans.add(newLine);
            }
        }
        return ans;
    }
}

原文地址:https://www.cnblogs.com/jasminemzy/p/9739410.html

时间: 2024-11-05 17:31:03

leetcode722 - Remove Comments - medium的相关文章

[LeetCode] Remove Comments

Given a C++ program, remove comments from it. The program source is an array where source[i] is the i-th line of the source code. This represents the result of splitting the original source code string by the newline character \n. In C++, there are t

[LeetCode] Remove Comments 移除评论

Given a C++ program, remove comments from it. The program source is an array where source[i] is the i-th line of the source code. This represents the result of splitting the original source code string by the newline character \n. In C++, there are t

722. Remove Comments

class Solution { public: vector<string> removeComments(vector<string>& source) { vector<string> res; string ln; int state = 0; for (const auto & line : source) { for (int i = 0, ll = line.length(); i < ll; i++) { if (state ==

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

require js

Require原理 在require中,根据AMD(Asynchronous Module Definition)的思想,即异步模块加载机制,其思想就是把代码分为一个一个的模块来分块加载,这样无疑可以提高代码的重用. 在整个require中,主要的方法就两个:require和define,我们先来聊聊require require作为主函数来引入我们的"模块",require会从自身的存储中去查找对应的defined模块,如果没有找到,则这时这个模块有可以存在三种状态:loading,

PHP扩展开发相关总结

1.线程安全宏定义 在TSRM/TSRM.h文件中有如下定义 #define TSRMLS_FETCH() void ***tsrm_ls = (void ***) ts_resource_ex(0, NULL) #define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = (void ***) ctx #define TSRMLS_SET_CTX(ctx) ctx = (void ***) tsrm_ls #define TSRMG(id, type

PHP扩展代码结构详解

PHP扩展代码结构详解: 这个是继:使用ext_skel和phpize构建php5扩展  内容 (拆分出来) Zend_API:深入_PHP_内核:http://cn2.php.net/manual/zh/internals2.ze1.php 我们使用ext_skel创建扩展 hello_module,该模块包含一个方法:hello_world. 使用ext_skel 生成的代码都是PHP_开头的宏, 而不是ZEND_开头. 实际上这两者是一样的. 在源代码src/main/PHP.h 中发现:

[转]雅虎加速网站最佳实践

转自:Best Practices for Speeding Up Your Web Site Best Practices for Speeding Up Your Web Site The Exceptional Performance team has identified a number of best practices for making web pages fast. The list includes 35 best practices divided into 7 cate

requirejs源码

require.js /** vim: et:ts=4:sw=4:sts=4 * @license RequireJS 2.1.11 Copyright (c) 2010-2014, The Dojo Foundation All Rights Reserved. * Available via the MIT or new BSD license. * see: http://github.com/jrburke/requirejs for details */ //Not using str