Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
remove-comments.cpp 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2017-11-05 23:36 +08:00 . Create remove-comments.cpp
// Time: O(n), n is the length of the source
// Space: O(k), k is the max length of a line
class Solution {
public:
vector<string> removeComments(vector<string>& source) {
bool in_block = false;
vector<string> result;
string newline;
for (const auto& line : source) {
for (int i = 0; i < line.length(); ++i) {
if (!in_block && i + 1 < line.length() && line.substr(i, 2) == "/*") {
in_block = true;
++i;
} else if (in_block && i + 1 < line.length() && line.substr(i, 2) == "*/") {
in_block = false;
++i;
} else if (!in_block && i + 1 < line.length() && line.substr(i, 2) == "//") {
break;
} else if (!in_block) {
newline.push_back(line[i]);
}
}
if (!in_block && !newline.empty()) {
result.emplace_back(move(newline));
}
}
return result;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助