https://leetcode-cn.com/problems/implement-queue-using-stacks/
class MyQueue {
private inStack: number[];
private outStack: number[];
constructor() {
this.inStack = [];
this.outStack = [];
}
push(x: number): void {
this.inStack.push(x);
}
pop(): number {
if (this.outStack.length === 0) {
while (this.inStack.length) {
this.outStack.push(this.inStack.pop());
}
}
if (this.outStack.length === 0) {
return null;
}
return this.outStack.pop();
}
peek(): number {
if (this.outStack.length === 0) {
while (this.inStack.length) {
this.outStack.push(this.inStack.pop());
}
}
if (this.outStack.length === 0) {
return null;
}
return this.outStack[this.outStack.length - 1];
}
empty(): boolean {
return !this.inStack.length && !this.outStack.length;
}
}
https://bigfrontend.dev/zh/problem/the-angle-between-hour-hand-and-minute-hand-of-a-clock
/**
* @param {string} time
* @returns {number}
*/
function angle(time) {
let [hours, minutes] = time.split(':').map(Number);
// 12小时制
hours = hours % 12;
// 每分钟6度,一圈为60分钟
const mDPM = 360 / 60;
// 每小时30度,一圈12小时(整点的情况下)
const hDPM = 360 / 12;
// 超出部分,比如01:30,时针度数在1和2正中间。
// 一分钟,时针偏移0.5度,30分钟时针偏移15度
const extra = 360 / 12 / 60 * minutes;
// 时针度数
const hoursAngle = hDPM * hours + extra;
// 分针度数
const minutesAngle = mDPM * minutes;
// 计算角度
const diff = Math.abs(hoursAngle - minutesAngle);
// 四舍五入,是否超过180
return Math.round(Math.min(diff, 360 - diff));
}