Skip to content

Commit 98282a0

Browse files
committed
Create 1487. 保证文件名唯一.js
1 parent a690e20 commit 98282a0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

1487. 保证文件名唯一.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string[]} names
3+
* @return {string[]}
4+
*/
5+
var getFolderNames = function (names) {
6+
const result = [];
7+
const map = {};
8+
for (const name of names) {
9+
if (map[name]) {
10+
do {
11+
const renamed = name + `(${map[name]})`;
12+
if (!map[renamed]) {
13+
map[renamed] = 1;
14+
result.push(renamed);
15+
break;
16+
}
17+
map[name]++;
18+
} while (true);
19+
} else {
20+
map[name] = 1;
21+
result.push(name);
22+
}
23+
}
24+
return result;
25+
};

0 commit comments

Comments
 (0)