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;
    }
};

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

时间: 2024-11-05 19:40:02

722. Remove Comments的相关文章

[LeetCode] Remove Comments

Given a C++ program, remove comments from it. The program source is an array where source[i] is the i-th line of the source code. This represents the result of splitting the original source code string by the newline character \n. In C++, there are t

[LeetCode] Remove Comments 移除评论

Given a C++ program, remove comments from it. The program source is an array where source[i] is the i-th line of the source code. This represents the result of splitting the original source code string by the newline character \n. In C++, there are t

leetcode722 - Remove Comments - medium

Given a C++ program, remove comments from it. The program source is an array where source[i] is the i-th line of the source code. This represents the result of splitting the original source code string by the newline character \n.In C++, there are tw

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

require js

Require原理 在require中,根据AMD(Asynchronous Module Definition)的思想,即异步模块加载机制,其思想就是把代码分为一个一个的模块来分块加载,这样无疑可以提高代码的重用. 在整个require中,主要的方法就两个:require和define,我们先来聊聊require require作为主函数来引入我们的"模块",require会从自身的存储中去查找对应的defined模块,如果没有找到,则这时这个模块有可以存在三种状态:loading,

PHP扩展开发相关总结

1.线程安全宏定义 在TSRM/TSRM.h文件中有如下定义 #define TSRMLS_FETCH() void ***tsrm_ls = (void ***) ts_resource_ex(0, NULL) #define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = (void ***) ctx #define TSRMLS_SET_CTX(ctx) ctx = (void ***) tsrm_ls #define TSRMG(id, type

PHP扩展代码结构详解

PHP扩展代码结构详解: 这个是继:使用ext_skel和phpize构建php5扩展  内容 (拆分出来) Zend_API:深入_PHP_内核:http://cn2.php.net/manual/zh/internals2.ze1.php 我们使用ext_skel创建扩展 hello_module,该模块包含一个方法:hello_world. 使用ext_skel 生成的代码都是PHP_开头的宏, 而不是ZEND_开头. 实际上这两者是一样的. 在源代码src/main/PHP.h 中发现:

[转]雅虎加速网站最佳实践

转自:Best Practices for Speeding Up Your Web Site Best Practices for Speeding Up Your Web Site The Exceptional Performance team has identified a number of best practices for making web pages fast. The list includes 35 best practices divided into 7 cate