---
# System prepended metadata

title: 0722. Remove Comments
tags: [Leetcode, Medium, Microsoft]

---

# 0722. Remove Comments
###### tags: `Leetcode` `Microsoft` `Medium`
Link: https://leetcode.com/problems/remove-comments/
## 思路
非常繁琐的一道题
不过顺着要求来就能做出来
原本想用stack
但是发现没有必要
因为input一定是legal的
## Code
```java=
class Solution {
    public List<String> removeComments(String[] source) {
        List<String> ans = new ArrayList<String>();
        boolean inBlock = false;
        StringBuffer newLine = new StringBuffer();
        for(String s:source){
            if(!inBlock) newLine = new StringBuffer();
            for(int i = 0;i < s.length();i++){
                if(!inBlock && i+1 < s.length() && s.charAt(i)=='/'&& s.charAt(i+1)=='/'){
                    i++;
                    break;
                }
                else if(!inBlock && i+1 < s.length() && s.charAt(i)=='/' && s.charAt(i+1)=='*'){
                    inBlock = true;
                    i++;
                }
                else if(inBlock&& i+1 < s.length()  && s.charAt(i)=='*'&& s.charAt(i+1)=='/'){
                    inBlock = false;
                    i++;
                }
                else if(!inBlock){
                    newLine.append(s.charAt(i));
                }
            }
            if(newLine.length()!=0&&!inBlock){
                ans.add(newLine.toString());
            }
        }
        return ans;
    }
}
```