C++ easy solution


  • 0
    N
    class Solution {
    public:
        vector<string> removeComments(vector<string>& source) {
            vector<string> ans;
            bool status = 0; // false:nothing, true:under block comment
            for(string line : source){
                if(status == 0 && (ans.empty() || ans.back()!=""))
                    ans.push_back("");
                int i=0;
                while(i<line.size()){
                    if(line.substr(i,2) == "//"){
                        if(status == 0)
                            break; //break  while(i<line.size()){...}   loop
                    }
                    else if(line.substr(i,2) == "/*"){
                        if(status == 0){
                            status = 1;
                            i+=2;
                            continue;
                        }
                    }
                    else if(line.substr(i,2) == "*/"){
                        if(status == 1){
                            status = 0;
                            i+=2;
                            continue;
                        }
                    }
                    if(status == 0)
                        ans.back().push_back(line[i]);
                    i++;
                }
            }
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.