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;
    }
};```
相关推荐
DeeGLMath2 小时前
从基础算法到机器学习的研究轨迹
人工智能·算法·机器学习
Barkamin2 小时前
冒泡排序的简单实现
java·算法·排序算法
_dindong2 小时前
【单调栈/队列&并查集&字符串哈希&Tire树】习题集锦
数据结构·c++·算法·哈希算法
独自破碎E2 小时前
【手撕真题】合并区间
算法
big_rabbit05022 小时前
[算法][力扣110]平衡二叉树
数据结构·算法·leetcode
二年级程序员2 小时前
排序(五)“计数排序” 与 “各排序实际用时测量”
c语言·算法·排序算法
松☆2 小时前
C++ 程序设计基础:从 Hello World 到数据类型与 I/O 流的深度解析
c++·算法
Book思议-2 小时前
单链表应用:双指针【快慢指针】
数据结构
今儿敲了吗3 小时前
41| 快速乘
数据结构·c++·笔记·学习·算法