// I will use map to filter the dupicate chars
const deleteDupicate=(str)=>{
let map = new Map();
for(let char of str) {
if(!map.has(char)) {
map.set(char,1);
}
else map.set(char,map.get(char)+1);
}
for(let i=0;i<str.length;i++) {
if(map.get(str[i])===1) return i;
}
return -1;
}
const Input = "loveleetcode"
// console.log(deleteDupicate(Input))
const reverseStr=(str)=>{
let right = str.length-1;
let left = 0;
let ans = "";
while(right>=0) {
while(right>=0&&str[right]===" "){
right--;
}
left = right-1;
while(left>0&&str[left]!==" ") left--;
ans+=str.slice(left+1,right+1)+" ";
right = left-1;
}
return ans.slice(0,ans.length-1); // trim()函数可去掉开头和结尾的空格
}
const str = " the sky is blue "
// console.log(reverseStr(str));
const flatten=(arr)=>{
let result = [];
for(let item of arr) {
Array.isArray(item)
?result.push(...flatten(item))
:result.push(item);
}
return result;
}
// console.log(flatten([1, [2, [3, 4], 5], 6]));
const frequency=(str)=>{
let map = new Map();
let ans = "";
for(let char of str) {
map.has(char)
?map.set(char,map.get(char)+1)
:map.set(char,1)
}
let arr = Array.from(map).sort((a,b)=>{ // 转换为数组后降序排序
return b[1]-a[1];
})
for(let [key,value] of arr) {
ans+=key.repeat(value);
}
return ans;
}
const str2="Aabbdddcc"
// console.log(frequency(str2));
const intersection=(num1,num2)=>{
let set1 = new Set(num1);
let set2 = new Set();
for(let n of num2) {
if(set1.has(n)) set2.add(n);
}
return Array.from(set2);
}
// console.log(intersection([4, 9, 5], [9, 4, 9, 8, 4])); // [9, 4] or [4, 9]
class LRUCache {
constructor(capacity) {
this.capacity = capacity;
this.cache = new Map();
}
get(key) {
if(!this.cache.has(key)) return -1;
const value = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key,value);
return value;
}
put(key,value) {
if(this.cache.has(key)) this.cache.delete(key);
if(this.cache.size>=this.capacity) {
this.cache.delete(this.cache.keys().next().value)
}
this.cache.set(key,value);
}
}
// const cache = new LRUCache(2);
// cache.put(1, 1);
// cache.put(2, 2);
// console.log(cache.get(1)); // 1
// cache.put(3, 3); // 移除 key=2
// console.log(cache.get(2)); // -1
// cache.put(4, 4); // 移除 key=1
// console.log(cache.get(1)); // -1
// console.log(cache.get(3)); // 3
// console.log(cache.get(4)); // 4
const deepClone=(obj,cache=new Map())=>{
// 初始类型直接拷贝
if(obj===null||typeof obj!=="object") return obj;
// 防止循环引用,有了obj则返回
if(cache.has(obj)) return cache.get(obj);
const clone = obj.isArray?[]:{};
// 将这一层的空对象或数组存入,防止下一层循环引用
cache.set(obj,clone);
for(let key in obj) {
clone[key] = deepClone(obj[key],cache)
}
return clone;
}
const obj = {
name: 'Alice',
info: {
age: 25,
favorites: ['music', 'chess'],
},
};
// 创建循环引用
obj.self = obj;
// const copy = deepClone(obj);
// console.log(copy.name); // Alice
// console.log(copy.info.favorites[0]); // music
// console.log(copy.self === copy); // true ✅ 不死循环
// console.log(copy === obj); // false ✅ 是不同对象
const myPromiseRace=(promises)=>{
return new Promise((resolve,reject)=>{
for(let p of promises) {
Promise.resolve(p)
.then(resolve)
.catch(reject)
}
})
}
// const p1 = new Promise(resolve => setTimeout(() => resolve("A"), 100));
// const p2 = new Promise(resolve => setTimeout(() => resolve("B"), 50));
// myPromiseRace([p1, p2]).then(console.log); // 输出:B
const myPromiseAny=(promises)=>{
return new Promise((resolve,reject)=>{
let count = 0;
let rejectArr = [];
if(promises.length===0) {
return reject(new AggregateError([],"All promises are error"))
}
for(let p of promises) {
Promise.resolve(p)
.then(resolve)
.catch(err=>{
rejectArr.push(err);
count++;
if(count===promises.length) {
reject(new AggregateError(rejectArr,"All promises were rejected"))
}
})
}
})
}
const p1 = Promise.reject("fail A");
const p2 = Promise.reject("fail B");
const p3 = Promise.resolve("success C");
myPromiseAny([p1, p2, p3])
.then(console.log) // 输出:success C
.catch(err => console.error(err)); // 不执行
myPromiseAny([Promise.reject("A"), Promise.reject("B")])
.then(console.log)
.catch(err => console.error(err)); // 输出 AggregateError
console