栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现单调栈和单调队列

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现单调栈和单调队列

package com.test.autimatic;

import org.apache.poi.ss.formula.functions.T;

import java.util.*;


public class SingleQueueAndSingleStack {


    public static void main(String[] args) {
        //mystack--获取比当前值大的最近的元素
        int[] arr = {2,1,3,4,3};
        List list = new ArrayList<>();//最终数据
        MyStack myStack = new MyStack<>();
        for (int i = arr.length-1; i >= 0; i--) {
            Integer add = myStack.add(arr[i]);
            list.add(add==null?-1:add);
        }
        System.out.println(list);
        System.out.println("---------------------------");
        //myqueue---单调队列,获取一为3的长度段区间内的最大值
        int[] arrQue = {2,1,3,4,3};
        MyQueue queue = new MyQueue<>();
        List endMax = new ArrayList<>();
        for (int i = 0; i < arrQue.length; i++) {
            queue.add(arrQue[i]);
            if(i > 2) {
                queue.delete(arrQue[i-3]);
            }
            endMax.add(queue.max());
        }
        System.out.println(endMax);
    }


    
    public static class MyStack>{

        private Stack stack;//存储数据的队列

        private Key max;//存储最大元素

        
        public MyStack() {
            this.stack = new Stack<>();
        }

        
        public Key add(Key key){
            //如果stack不为空且栈顶元素比目标值小,删除栈顶元素-维持低增粘
            while (!stack.isEmpty() && stack.peek().compareTo(key) < 0){
                stack.pop();
            }
            //设置最大值
            if(stack.isEmpty()){
                max = key;
            }
            //获取倒数第二个数
            Key end = stack.isEmpty()? null:stack.peek();
            //添加元素
            stack.push(key);
            return end;
        }

        
        public Key del(){
            if(!stack.isEmpty()){
                return stack.pop();
            }else{
                max = null;
            }
            return null;
        }

        
        public Key max(){
            return this.max;
        }

    }

    
    public static class MyQueue>{

        //数据集合---
        private Deque queue;

        public MyQueue() {
            this.queue = new linkedList<>();
        }

        
        public void add(key key){
            //先进先出--从头进去数据
            while (!queue.isEmpty() && queue.peekFirst().compareTo(key) < 0 ){
                queue.pollFirst();//如果比队头大,则删去队头元素
            }
            //添加元素
            queue.addFirst(key);
        }

        
        public key max(){
            if(queue.isEmpty()){
                return null;
            }
            //返回最后一个元素就是最大值
            return queue.peekLast();
        }

        
        public key delete(key key){
            if(!queue.isEmpty() && queue.peekLast().compareTo(key) == 0){
                return queue.pollLast();//如果最后一个数据为当前要删除的数据则删除
            }else{
                return null;
            }
        }

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591426.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号