• 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;
        }
    };
  • 相关阅读:
    AGC005D ~K Perm Counting
    运行python脚本后台执行
    java枚举类型
    java可变参数长度
    java 泛型数组列表
    java抽象类
    java final使用
    java继承
    java 对象
    java 重载
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/9986719.html
Copyright © 2020-2023  润新知