|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 798236606 于 2020-3-5 14:49 编辑
传送门:https://leetcode-cn.com/problems ... rom-the-filesystem/
- class Solution {
- public:
- vector<string> removeSubfolders(vector<string>& folder) {
- sort(folder.begin(), folder.end());//按字典序排列,使同一目录下文件夹聚集,效果如["/a","/a/b", "/a/b/c", "/a/b/d", "/a/e"]
- vector<string> ans;
- string father = "#";//父文件夹名称,开始时没有父文件夹,选一个题目不会出现的字符串
- for (string s: folder)
- {
- if (s.find(father) == s.npos)//如果路径中找不到父文件夹,说明s不是子文件夹
- {
- ans.push_back(s);
- father = s + "/";//加"/"才能表示包含关系,如"/a"不是"/a/"的子文件夹
- }
- }
- return ans;
- }
- };
复制代码 |
|