题解 | #栈和排序#

栈和排序

https://www.nowcoder.com/practice/95cb356556cf430f912e7bdf1bc2ec8f

import java.util.*;

/**
 * NC115 栈和排序
 * @author d3y1
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 栈排序
     * @param a int整型一维数组 描述入栈顺序
     * @return int整型一维数组
     */
    public int[] solve (int[] a) {
        // return solution1(a);
        // return solution2(a);
        return solution3(a);
    }

    /**
     * 模拟法: Stack+TreeSet
     * @param a
     * @return
     */
    private int[] solution1(int[] a){
        int n = a.length;

        TreeSet<Integer> treeSet = new TreeSet<>((o1, o2) -> (o2-o1));
        for(int i=n-1; i>=0; i--){
            treeSet.add(a[i]);
            if(a[i] == n){
                break;
            }
        }

        int max = treeSet.pollFirst();

        int[] result = new int[n];
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        for(int num: a){
            stack.push(num);
            treeSet.remove(num);
            if(stack.peek() == max){
                result[i++] = stack.pop();
                while(!stack.isEmpty() && !treeSet.isEmpty() && stack.peek()>treeSet.first()){
                    result[i++] = stack.pop();
                }
                if(!treeSet.isEmpty()){
                    max = treeSet.pollFirst();
                }
            }
        }

        while(!stack.isEmpty()){
            result[i++] = stack.pop();
        }

        return result;
    }

    /**
     * 模拟法: Stack(简化)
     * @param a
     * @return
     */
    private int[] solution2(int[] a){
        int n = a.length;
        // rMax[i]: i右侧最大值(不包括自身)
        int[] rMax = new int[n];
        int max = a[n-1];
        for(int i=n-2; i>=0; i--){
            max = Math.max(max, a[i+1]);
            rMax[i] = max;
        }

        int[] result = new int[n];
        Stack<Integer> stack = new Stack<>();
        for(int i=0,j=0; i<n; i++){
            stack.push(a[i]);
            if(stack.peek() == max){
                result[j++] = stack.pop();
                // rMax[n-1]=0 全部弹出
                while(!stack.isEmpty() && stack.peek()>rMax[i]){
                    result[j++] = stack.pop();
                }
                max = rMax[i];
            }
        }

        return result;
    }

    /**
     * 模拟法: Stack(再简化)
     * @param a
     * @return
     */
    private int[] solution3(int[] a){
        int n = a.length;
        // rMax[i]: i右侧最大值(包括自身)
        int[] rMax = new int[n];
        rMax[n-1] = a[n-1];
        for(int i=n-2; i>=0; i--){
            rMax[i] = Math.max(rMax[i+1], a[i]);
        }

        int[] result = new int[n];
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i=0; i<n; i++){
            while(!stack.isEmpty() && stack.peek()>rMax[i]){
                result[j++] = stack.pop();
            }
            stack.push(a[i]);
        }

        while(!stack.isEmpty()){
            result[j++] = stack.pop();
        }

        return result;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
05-26 15:37
1、这群人晚上&nbsp;11&nbsp;点发朋友圈:"凌晨&nbsp;11&nbsp;点,三环的灯还亮着。"&nbsp;实际下班时间:19:30。2、什么是嘉豪呀?我最近在字节实习,没什么时间上网3、同龄人:学校社团、酒吧蹦迪;我:acm、字节/腾讯实习4、别人朋友圈发:“今天不想上课”;我朋友圈发:“今天的班就上到这里啦”,定位:字节跳动5、别人的朋友圈都是到处旅游的定位,我的朋友圈天天都是“字节定位”,还一定要是在【公司的健身房】里拍张照片,实际只练了10分钟,其中凹造型5分钟6、mentor布置任务的时候,别人都是:”好的收到“,我:”是不是要xxxx,xxxx这么做也可以吧,这个技术方案会不会更好些“7、别人书包里装的:王道408、轻薄本、四六级真题。我书包里面装的:显存24GB4090独显gpu(24小时开机运行,屏幕上贴着“字节/腾讯等贴纸”)、速效救心丸(代码报错用)、电棍(熬夜写代码困了用),就很……你们懂吧8、入职大厂第一件事:发朋友圈、发小红书,晒工牌,985计算机硕|字节实习生|可以接咨询|有偿改简历,9、别人的社交软件简介:25岁|男|希望遇见有趣的灵魂;嘉豪的社交软件简介:25岁|程序员|字节跳动工程师|一张佩戴工牌的自拍照大厂嘉豪标配:1.&nbsp;挂胸前的工牌(地铁里只挂不收,怕你看不见&nbsp;logo)2.&nbsp;降噪耳机(不放音乐也戴着,避免别人跟自己说话)3.&nbsp;印&nbsp;logo&nbsp;的电脑包(字节红&nbsp;/&nbsp;腾讯蓝&nbsp;/&nbsp;阿里橙&nbsp;/&nbsp;美团黄)4.&nbsp;手表(最好显示心率,午饭后必发"步数已破&nbsp;6,000")
牛客30247842...:因为不好进啊要是大厂随便进哪来这么多人装逼
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务