-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1487.保证文件名唯一.js
More file actions
38 lines (37 loc) · 877 Bytes
/
1487.保证文件名唯一.js
File metadata and controls
38 lines (37 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* @lc app=leetcode.cn id=1487 lang=javascript
*
* [1487] 保证文件名唯一
*/
// @lc code=start
/**
* @param {string[]} names
* @return {string[]}
*/
var getFolderNames = function (names) {
const map = new Map();
const res = [];
for (let name of names) {
// map中存在
if (map.has(name)) {
let num = map.get(name); // 存在了几个
// (?) 一直+1 直到找到map中不存在为止
while (true) {
if (map.has(`${name}(${num})`)) num++;
else break;
}
res.push(`${name}(${num})`);
map.set(name, num);
map.set(`${name}(${num})`, 1);
}
// map中不存在 计数1 直接加入结果数组
else {
map.set(name, 1);
res.push(name);
}
}
return res;
};
const names = ["kaido", "kaido(1)", "kaido", "kaido(1)"];
console.log(getFolderNames(names));
// @lc code=end