栈与队列练习

栈与队列练习题

first

       给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。-----牛客网!

import java.util.ArrayList;
public class Solution {
    
    
    public ArrayList<Integer> maxInWindows(int [] num, int size)
    {
    
    
            ArrayList<Integer> stack=new ArrayList<>();
        if(size==0){
    
    
            return stack;
        }else{
    
    
            int i;//外层循环标志
            int j;//内层循环标志
            int max=0;//定义滑动窗口中的最大值
            for(i=0;i<num.length-size+1;i++){
    
    
                max=num[i];
                for(j=i;j<size+i-1;j++){
    
    
                    if(max<num[j+1]){
    
    
                        max=num[j+1];
                    }
                }

                //将最大值入栈
                stack.add(max);
            }
            return stack;
        }

    }
}

second

       用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

import java.util.Stack;

public class Solution {
    
    
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
    
    
        while(!stack2.empty()){
    
    
            stack1.push(stack2.pop());
        }
        stack1.push(node);       
    }
    
    public int pop() {
    
    
        while(!stack1.empty()){
    
    
              stack2.push(stack1.pop());
        }
       return stack2.pop();      
    }
}

third

       将中缀表达式转为后缀表达式,输入 a+b* c/d-a+f/b 输出 abc* d/+a-fb/+要求:语言不限;输入输出均为单个字符串;操作数用单个小写字母表示,操作符只需支持 ±*/,按照四则运算顺序确定优先级,不包含括号

import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    
    
    public char []infixtosuffix(char []s){
    
    
        //定义一个字符串该字符串为后缀字符串
        char s1[]=new char[s.length];
        //定义一个栈存储操作数
        Stack<Character> Operands=new Stack<Character>();
        //定义一个栈存储运算符
        Stack<Character>  operator=new Stack<Character>();
        //定义标志位对s进行遍历
        int i;
        //定义一个字符,用于保存上一个入栈的运算符
        char c='\0';
        for(i=0;i<s.length;i++){
    
    
            if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){
    
    
                if(operator.empty()){
    
    
                    c=s[i];
                    operator.add(s[i]);
                }else{
    
    
                    if((c=='+'||c=='-')&&(s[i]=='*'||s[i]=='/')){
    
    
                         operator.add(s[i]);
                         c=s[i];
                    }else{
    
    
                        Operands.add(operator.pop());
                        c=s[i];
                        while(operator.empty()==false){
    
    
                            if((c=='*'||c=='/')&&(operator.peek()=='+'||operator.peek()=='-'))break;
                            else{
    
    
                                Operands.add(operator.pop());
                            }
                        }
                        operator.add(s[i]);
                    }
                }

            }else{
    
    
                Operands.add(s[i]);
            }
        }
        while(operator.empty()==false){
    
    
            Operands.add(operator.pop());
        }

        Iterator<Character> iter = Operands.iterator();
        i=0;
        while(iter.hasNext()){
    
    
           char b=iter.next();
           s1[i]=b;
            i++;
        }
        return s1;
    }

    public static void main(String[] args) {
    
    
        Main a=new Main();
        Scanner input=new Scanner(System.in);//创建一个键盘扫描类对象
        String contents1=input.next(); //输入字符串型
        //char c[]={'a','+','b','*','c','/','d','-','a','+','f','/','b'};
        System.out.println(a.infixtosuffix(contents1.toCharArray()));

    }
}

在这里插入图片描述

参考链接

中缀表达式转后缀表达式的方法,步骤和原理及后缀表达式运算方式

猜你喜欢

转载自blog.csdn.net/qq_41827511/article/details/106450422