Skip to content

Commit 9029bb8

Browse files
committed
1487. 保证文件名唯一
1 parent 5e06395 commit 9029bb8

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
|1233|[删除子文件夹](https://leetcode.cn/problems/remove-sub-folders-from-the-filesystem/)|[JavaScript](./algorithms/remove-sub-folders-from-the-filesystem.js)|Medium|
215215
|1305|[两棵二叉搜索树中的所有元素](https://leetcode.cn/problems/all-elements-in-two-binary-search-trees/)|[JavaScript](./algorithms/all-elements-in-two-binary-search-trees.js)|Medium|
216216
|1413|[逐步求和得到正数的最小值](https://leetcode.cn/problems/minimum-value-to-get-positive-step-by-step-sum/)|[JavaScript](./algorithms/minimum-value-to-get-positive-step-by-step-sum.js)|Easy|
217+
|1487|[保证文件名唯一](https://leetcode.cn/problems/making-file-names-unique/)|[JavaScript](./algorithms/making-file-names-unique.js)|Medium|
217218
|1604|[警告一小时内使用相同员工卡大于等于三次的人](https://leetcode.cn/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/)|[JavaScript](./algorithms/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period.js)|Medium|
218219
|1664|[生成平衡数组的方案数](https://leetcode.cn/problems/ways-to-make-a-fair-array/)|[JavaScript](./algorithms/ways-to-make-a-fair-array.js)|Medium|
219220
|1669|[合并两个链表](https://leetcode.cn/problems/merge-in-between-linked-lists/)|[JavaScript](./algorithms/merge-in-between-linked-lists.js)|Medium|
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 1487. 保证文件名唯一
3+
* @param {string[]} names
4+
* @return {string[]}
5+
*/
6+
var getFolderNames = function (names) {
7+
let map = new Map();
8+
9+
for (let i = 0; i < names.length; i++) {
10+
const name = names[i];
11+
if (map.has(name)) {
12+
let cnt = map.get(name);
13+
while (map.has(names[i])) {
14+
names[i] = `${name}(${cnt++})`;
15+
}
16+
map.set(names[i], 1); // new name
17+
map.set(name, cnt);
18+
} else {
19+
map.set(name, 1);
20+
}
21+
}
22+
23+
return names;
24+
};

0 commit comments

Comments
 (0)