1487. 保证文件名唯一

文章目录

题意

题目链接

思路

计算

代码

C++ 复制代码
class Solution {
public:
    vector<string> getFolderNames(vector<string>& names) {
        set<string> s;
        vector<string> ans;
        map<string, int> m;
        for (auto &it:names) {
            if (s.count(it) == 0) {
                m[it]++;
                s.insert(it);
                ans.push_back(it);
            } else {
                int i = m.count(it) ? m[it] : 1;
                while (s.count(it + string("(") + to_string(i) + string(")")))
                    i++;
                const string tmp = it + string("(") + to_string(i) + string(")");
                m[it] = i + 1;
                s.insert(tmp);
                ans.push_back(tmp);
            }
        }
        return ans;
    }
};```
相关推荐
BK贝壳13 小时前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
人间乄惊鸿客15 小时前
c++ 右值
c++·算法
fanstuck16 小时前
AI 客服答错了,三分钟分清是 Prompt 还是系统的锅
算法
田里的水稻17 小时前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
码完就睡17 小时前
数据结构——图的基础概念
数据结构
猿人谷19 小时前
从百万个 Pareto 解中选出 100 个:Riesz s-Energy 如何实现高效的多样性选择?
后端·算法·aigc
地平线开发者19 小时前
地平线 × 同济无人车队:PointPillars 从浮点模型到 BPU 部署
算法·自动驾驶