Open Sunny-117 opened 2 years ago
// 算法题:字符串重命名。
// 举例 ,输入["ab","c","ab","c","a","d"],
// 输出["ab1","c1","ab2","c2","a","d"],
// 输出不可以改变原有顺序,不重复的字符串不动。
// 最简单的做法:直接暴力,双循环即可实现
// 也可以采用map存储计数,然后从尾部开始遍历,注意两种计数为1的情况需要区分即可
function rename(arr) {
let map = new Map();
for (let i = 0; i < arr.length; i++) {
let char = arr[i];
if (map.has(char)) map.set(char, map.get(char) + 1);
else map.set(char, 1);
}
console.log(map);
for (let i = arr.length - 1; i >= 0; i--) {
let char = arr[i];
let count = map.get(char);
if (count === 1) continue;
if (count === "mark") arr[i] += 1;
else {
arr[i] += count--;
map.set(char, count === 1 ? "mark" : count);
}
}
return arr;
}
console.log(rename(["ab", "ab", "c", "ab", "c", "a", "d"]));
function main(arr){
const res = [];
const map = new Map();
for (const item of arr) {
map.has(item) ? (map.set(item, map.get(item) + 1), res.push(item + map.get(item))) : (map.set(item, 1), res.push(item + 1));
}
return res;
}
const arr = ['ab', 'c', 'ab', 'd', 'c'];
let map = new Map();
for (let i = 0; i < arr.length; i++) {
map.set(arr[i], (map.get(arr[i]) || 0) + 1);
}
map = Array.from(map).filter((item) => item[1] !== 1);
map = new Map(map);
for (let i = arr.length - 1; i >= 0; i--) {
if (map.get(arr[i]) > 0) {
let tmp = arr[i];
let val = map.get(arr[i]);
arr[i] += map.get(arr[i]);
map.set(tmp, val - 1);
}
}
console.log(arr);
少一个for循环,空间换时间
function renameDuplicates(arr) {
let countMap = {};
arr.forEach((el, idx, origin) => {
if(!countMap[el]) {
countMap[el] = {}
countMap[el].num = 1
countMap[el].startIndex = idx
} else {
countMap[el].num += 1
}
if(countMap[el].num === 1) return
else {
if(countMap[el].num === 2) origin[countMap[el].startIndex] += '1'
origin[idx] = el + countMap[el].num
}
});
return arr
}
renameDuplicates(['a', 'c', 'c', 'c']) // ['a', 'c1', 'c2', 'c3']
const arr = ['ab', 'c', 'ab', 'd', 'c']
let map = {}
let list = arr.reduce((pre, item, index, arr) => {
if(map[item]){
map[item].num++
}else{
map[item] = {
index:index,
num:1
}
}
if(map[item].num === 1){
pre.push(item)
}else{
pre[map[item].index]+=1
pre.push(item + map[item].num)
}
return pre
}, [])
console.log(list)
function rename(arr) {
const map = {} //空间换时间
let res = [] //记录结果
for (let i = 0; i < arr.length; i++) {
let s = arr[i]
if (map[s]) {
if (map[s].num === 1) { //出现重复,修改第一个元素为元素1
res[map[s].index] += 1
}
map[s].num++
res.push(s + map[s].num)
} else {
map[s] = { //记录第一次遍历时的下标
num: 1,
index: i
}
res.push(s) //先默认没有重复,如果有,后续根据下标进行修改
}
}
return res
}
function renameStrings(arr) {
let result = [];
let map = new Map();
for (let i = 0; i < arr.length; i++) {
if (map.has(arr[i])) {
let index = result.indexOf(arr[i]);
result[index] = `${arr[i]}1`;
let num = map.get(arr[i]) + 1;
map.set(arr[i], num);
result.push(`${arr[i]}${num}`);
} else {
map.set(arr[i], 1);
result.push(`${arr[i]}`);
}
}
return result;
}
console.log(renameStrings(["ab", "c", "ab", "c", "a", "d"]));
const renameArr = function(arr) {
const map = new Map();
for(var i = 0; i < arr.length; i++) {
var temp = arr[i];
var c_index = Number((map.get(temp)||0) - 0 + 1);
arr[i] = arr[i] + c_index;
map.set(temp, c_index);
}
return arr;
}
console.log(renameArr(["ab", "ab", "c", "ab", "c", "a", "d"]));
算法题:字符串重命名。 举例 ,输入["ab","c","ab","c","a","d"], 输出["ab1","c1","ab2","c2","a","d"], 输出不可以改变原有顺序,不重复的字符串不动。