首页 > 其他 > 详细

722. Remove Comments

时间:2018-11-20 00:39:23      阅读:294      评论:0      收藏:0      [点我收藏+]
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 == 0) {
                    if (i < ll-1) {
                        if (line[i] == / && line[i+1] == /)
                            break;  // // comment, skip line
                        else if (line[i] == / && line[i+1] == *) {
                            state = 1;
                            i += 1;
                            continue;
                        }
                    }
                    ln.push_back(line[i]);
                }
                else if (state == 1) {  // inside /*
                    if (i < ll-1 && line[i] == * && line[i+1] == /) {
                        state = 0;
                        i += 1;
                        continue;
                    }
                }
            }
            if (state == 0 && ln.length() > 0) {
                res.push_back(ln);
                ln = "";
            }
        }
        if (ln.length() > 0)
            res.push_back(ln);
        return res;
    }
};

 

722. Remove Comments

原文:https://www.cnblogs.com/JTechRoad/p/9986719.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!