1487. 保证文件名唯一

map标记即可

注意对于某个文件名生成的结果文件名也需要标记一下

class Solution {
public:
    vector<string> ret;
    map<string, int> mmap;
    vector<string> getFolderNames(vector<string>& names) {
        int n = names.size();
        
        for(int i = 0; i < n; i++)
        {
            if(mmap[names[i]])
            {
                string tmp = names[i];

                while(mmap[tmp])
                {
                    string str;
                    stringstream ss;

                    ss << mmap[names[i]];
                    ss >> str;
                    tmp = names[i] + "(" + str + ")";
                    mmap[names[i]]++;
                }
                mmap[tmp]++;
                ret.push_back(tmp);
            }
            else
            {
                ret.push_back(names[i]);
                mmap[names[i]]++;
            }
        }
        return ret;




    }
};

 

posted @ 2021-11-14 22:52  WTSRUVF  阅读(34)  评论(0编辑  收藏  举报