Remove Sub-Folders from the Filesystem(C++删除子文件夹)

解题思路:

(1)先排序,从循环中断处开始下一次的比较,不然会超时

class Solution {
public:
    vector<string> removeSubfolders(vector<string>& f) {
        sort(f.begin(),f.end());
        vector<string> s;
        for(int i=0;i<f.size();) {
            int j=i+1;
            while(j<f.size() && f[i]==f[j].substr(0,f[i].length()) && f[j][f[i].length()]=='/') {
                j++;
            }
            s.push_back(f[i]);
            i=j;
        }
        return s;
    }
};

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/114965629