数据结构与算法之栈: LeetCode 03.05. 栈排序 (Ts版)

03.05. 栈排序

  • https://leetcode.cn/problems/sort-of-stacks-lcci/description/

描述

  • 栈排序。 编写程序,对栈进行排序使最小元素位于栈顶。最多只能使用一个其他的临时栈存放数据,但不得将元素复制到别的数据结构(如数组)中。该栈支持如下操作:push、pop、peek 和 isEmpty。当栈为空时,peek 返回 -1。

示例 1

 输入:
["SortedStack", "push", "push", "peek", "pop", "peek"]
[[], [1], [2], [], [], []]
 输出:
[null,null,null,1,null,2]

示例 2

 输入: 
["SortedStack", "pop", "pop", "push", "pop", "isEmpty"]
[[], [], [], [1], [], []]
 输出:
[null,null,null,null,null,true]

提示

  • 栈中的元素数目在[0, 5000]范围内

Typescript 版算法实现


1 ) 方案1:栈和辅助栈

class SortedStack {
    private stk1: number[] = [];
    private stk2: number[] = [];

    constructor() {}

    // 插入元素时保持栈的排序
    push(val: number): void {
        // 将所有比 val 大的元素从 stk1 移动到 stk2
        while (this.stk1.length > 0 && this.stk1[this.stk1.length - 1] < val) {
            this.stk2.push(this.stk1.pop()!);
        }
        // 将新元素插入 stk1
        this.stk1.push(val);
        // 将 stk2 中的元素移回 stk1
        while (this.stk2.length > 0) {
            this.stk1.push(this.stk2.pop()!);
        }
    }

    // 弹出栈顶元素
    pop(): void {
        if (!this.isEmpty()) {
            this.stk1.pop();
        }
    }

    // 获取栈顶元素
    peek(): number {
        if (this.isEmpty()) {
            return -1;
        } else {
            return this.stk1[this.stk1.length - 1];
        }
    }

    // 检查栈是否为空
    isEmpty(): boolean {
        return this.stk1.length === 0;
    }
}

/**
 * Your SortedStack object will be instantiated and called as such:
 * var obj = new SortedStack()
 * obj.push(val)
 * obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.isEmpty()
 */

2 )方案2:基于 栈排序

class SortedStack {
    private stk: number[] = [];
    
    // 插入元素时保持栈的排序
    push(val: number): void {
        let tempStack: number[] = [];
        
        // 将所有比 val 大的元素从 stk 移动到 tempStack
        while (this.stk.length > 0 && this.stk[this.stk.length - 1] < val) {
            tempStack.push(this.stk.pop()!);
        }
        
        // 将新元素插入 stk
        this.stk.push(val);
        
        // 将 tempStack 中的元素移回 stk
        while (tempStack.length > 0) {
            this.stk.push(tempStack.pop()!);
        }
    }

    // 弹出栈顶元素
    pop(): void {
        if (!this.isEmpty()) {
            this.stk.pop();
        }
    }

    // 获取栈顶元素
    peek(): number {
        if (this.isEmpty()) {
            return -1;
        } else {
            return this.stk[this.stk.length - 1];
        }
    }

    // 检查栈是否为空
    isEmpty(): boolean {
        return this.stk.length === 0;
    }

    // 返回排序后的栈
    sort(): number[] {
        const result: number[] = [];
        while (this.stk.length > 0) {
            result.push(this.stk.pop()!);
        }
        return result.reverse(); // 反转结果以保持升序
    }
}


/**
 * Your SortedStack object will be instantiated and called as such:
 * var obj = new SortedStack()
 * obj.push(val)
 * obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.isEmpty()
 */

你可能感兴趣的:(Data,Structure,and,Algorithms,leetcode,算法)