zoukankan      html  css  js  c++  java
  • 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 == 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;
        }
    };
  • 相关阅读:
    CodeForces
    网络流
    poj 2185
    树的分治学习
    数位DP
    URAL 1969. Hong Kong Tram
    hdu 4759 Poker Shuffle
    hdu3712 Detector Placement
    分块思想
    莫比乌斯反演
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/9986719.html
Copyright © 2011-2022 走看看