队列
匹配括号
// 队列 + map 查看上一个值and下一个值 1.可以闭合 弹出 2.同样左开进
function isValid(s) {
const stack = [];
const leftBrackets = ['(', '{', '['];
const rightBrackets = [')', '}', ']'];
for (let i = 0; i < s.length; i++) {
const char = s[i];
if (leftBrackets.includes(char)) {
stack.push(char);
} else if (rightBrackets.includes(char)) {
if (stack.length === 0 || leftBrackets[rightBrackets.indexOf(char)]!== stack.pop()) {
return false;
}
}
}
return stack.length === 0;
}
// 测试用例
console.log(isValid('()'));
console.log(isValid('()[]{}'));
console.log(isValid('(]'));
console.log(isValid('([)]'));
console.log(isValid('{[]}'));
合并乱序区间
// 实现一个合并乱序区间的方法
/**
输入 [[1,4], [3,8], [10,15], [11,18], [20,27], [14,15],[19,28]]
期望输出 [[1, 8], [10,18],[19,28]]
*/
function f(nums) {
nums.sort((a,b)=> a[0]-b[0]);
const ans = [];
for(let p of nums) {
const m = ans.length;
if(m && p[0] <= ans[m-1][1]){
ans[m-1][1] = Math.max(ans[m-1][1],p[1]);
}else {
ans.push(p);
}
}
return ans;
}
const nums = [[1,4], [3,8], [10,15], [11,18], [20,27], [14,15],[19,28]] ;
console.log(f(nums))
已知有两个栈,有 pop,push,getSize 接口,请用这两个栈实现 1 个队列,包含 dequeue 和 enqueue 接口
class Queue {
constructor() {
this.inStack = [];
this.outStack = [];
}
// 入队操作
enqueue(value) {
this.inStack.push(value);
}
// 出队操作
dequeue() {
if (this.outStack.length === 0) {
// 如果输出栈为空,则将输入栈的所有元素转移到输出栈
while (this.inStack.length > 0) {
this.outStack.push(this.inStack.pop());
}
}
// 如果输出栈不为空,则直接弹出栈顶元素
if (this.outStack.length > 0) {
return this.outStack.pop();
}
// 如果两个栈都为空,则队列为空
return null;
}
// 获取队列的大小
getSize() {
return this.inStack.length + this.outStack.length;
}
}
// 使用示例
const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
console.log(queue.dequeue()); // 输出: 1
queue.enqueue(3);
console.log(queue.dequeue()); // 输出: 2
console.log(queue.dequeue()); // 输出: 3
console.log(queue.getSize()); // 输出: 0
场景题:队列,事件循环,链式调用
// 实现一个LazyMan,可以按照以下方式调用。
// LazyMan(“Hank”)输出:
// Hi! This is Hank!
// LazyMan(“Hank”).sleep(10).eat(“dinner”)输出
// Hi! This is Hank!
// //等待10秒..
// Wake up after 10
// Eat dinner~
// LazyMan(“Hank”).eat(“dinner”).eat(“supper”)输出
// Hi This is Hank!
// Eat dinner~
// Eat supper~
// LazyMan(“Hank”).sleepFirst(5).eat(“supper”)输出
// //等待5秒
// Wake up after 5
// Hi This is Hank!
// Eat supper
//
class LazyMan {
constructor(name) {
this.name = name;
this.queue = [];
setTimeout(() => {
if(this.status !==1 ){
this.sayHi();
}
this.runQueue();
}, 0);
}
sayHi() {
console.log(`Hi! This is ${this.name}!`);
}
sleep(time) {
this.queue.push(() => {
setTimeout(() => {
console.log(`Wake up after ${time}`);
this.runQueue();
}, time * 1000);
});
return this;
}
sleepFirst(time) {
this.status = 1
this.queue.unshift(() => {
setTimeout(() => {
console.log(`Wake up after ${time}`);
this.sayHi();
this.runQueue();
}, time * 1000);
});
return this;
}
eat(food) {
this.queue.push(() => {
console.log(`Eat ${food}~`);
this.runQueue();
});
return this;
}
runQueue() {
const task = this.queue.shift();
if (task) {
task();
}
}
}
// 测试用例
new LazyMan('Hank').sleep(10).eat('dinner');
// new LazyMan('Hank').eat('dinner').eat('supper');
// new LazyMan('Hank').sleepFirst(5).eat('supper');