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 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.

答案
A character level parser, I think this should make the coder look more clear

class Solution {
public List<String> removeComments(String[] source) {
        List<String> ret = new ArrayList<>();

        /* state 0: not in a comment, 1: block comment */
        int state = 0;
        int block_start = 0;
        for(int i = 0; i < source.length; i++) {
            String line = source[i];
            String newline = "";
            char last_c = 0;
            boolean concat = false;
            for(int j = 0; j < line.length(); j++) {
                char c = line.charAt(j);
                if(state == 0) {
                    // Should detect start of line/block comment here
                    if(c == '/' && last_c == '/') {
                        // Remove last char from newline
                        newline = newline.substring(0, newline.length() - 1);
                        break;
                    }
                    if(c == '*' && last_c == '/') {
                        state = 1;
                        // Remove last char from newline
                        newline = newline.substring(0, newline.length() - 1);
                        // Set last char to 0 to avoid overlapping block comment
                        last_c = 0;
                        block_start = i;
                        continue;
                    }
                    newline += c;
                }
                else {
                    // Should detect end of block comment here
                    if(c == '/' && last_c == '*') {
                        state = 0;
                        // Avoid overlapping
                        last_c = 0;
                        if(block_start != i) concat = true;
                        continue;
                    }
                }
                last_c = c;
            }
            if(concat) ret.set(ret.size() - 1, ret.get(ret.size() - 1) + newline);
            else if(!newline.equals(""))
                ret.add(newline);
        }
        return ret;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容