知识改变命运 数据结构【栈和队列面试题】

1.最小栈

java 复制代码
class MinStack {
    Stack <Integer>stack;
    Stack  <Integer>minStack; 
    public MinStack() {
        stack=new Stack<>();
        minStack=new Stack<>();
    }
    
    public void push(int val) {
        stack.push(val);
        if(minStack.empty()) {
            minStack.push(val);
        } else {
            int topval=minStack.peek();
            if(val<=topval) {
                minStack.push(val);
            }
        }
       
    }
    
    public void pop() {
        if(stack.peek().equals(minStack.peek())) {
            minStack.pop();
        }
        stack.pop();

    }
    
    public int top() {
       return stack.peek();
    }
    
    public int getMin() {
       return minStack.peek();
    }
}

2. 括号匹配


java 复制代码
public class Solution {

    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        char ch;
        for (int i = 0; i < s.length(); i++) {
            ch=s.charAt(i);
            if(ch=='{'||ch=='['||ch=='(') {
                stack.push(ch);
            } else if (stack.empty()){
                return false;
            } else if(stack.peek().equals('(')&&ch==')'||
                    stack.peek().equals('{')&&ch=='}'||
                    stack.peek().equals('[')&&ch==']'){
                stack.pop();
            } else {
                return false;
            }
        }
        return stack.empty();
    }
}

3. 逆波兰表达式求值

java 复制代码
public class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        for (int i = 0; i <tokens.length; i++) {
            if(opinion(tokens[i])) {
                int val1=stack.pop();
                int val2=stack.pop();
                switch(tokens[i]) {
                    case "+":
                        stack.push(val1+val2);
                        break;
                    case "-":
                        stack.push(val2-val1);
                        break;
                   case  "*":
                        stack.push(val1*val2);
                        break;
                    case "/":
                        stack.push(val2/val1);
                        break;
                }
            } else {
                stack.push(Integer.parseInt(tokens[i]));
            }

        }
       return stack.pop();
    }
    private boolean opinion(String ch) {
        return ch.equals("*")||ch.equals("+")
                ||ch.equals("/")||ch.equals("-");
    }
}

4.出栈入栈次序匹配

java 复制代码
  public boolean IsPopOrder (int[] pushV, int[] popV) {
    
         Stack<Integer> stack1=new Stack();
     
         int j=0;
         for(int i=0;i<pushV.length;i++) {
            stack1.push(pushV[i]);
            while(j<popV.length&&!stack1.empty()&&stack1.peek()==popV[j]) {
                stack1.pop();
                j++;
            }
            
         }
        return stack1.empty();
    }
}

5. 用队列实现栈。OJ链接

java 复制代码
public class MyStack {
    Queue<Integer> queue1;
    Queue<Integer> queue2;
    public MyStack() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }

    public void push(int x) {
        if(empty()){
            queue1.offer(x);
        } else if  (!queue2.isEmpty()){
            queue2.offer(x);

        } else  {
            queue1.offer(x);
        }
    }


    public int pop() {
        int val=0;
        if(empty()) {
            return -1;
        } else {
            if (!queue1.isEmpty()) {
                int count=queue1.size()-1;
                while(count!=0) {
                    queue2.offer(queue1.poll());
                    count--;
                }
                val=queue1.poll();
            }else {
                int count=queue2.size()-1;
                while(count!=0) {
                    queue1.offer(queue2.poll());
                    count--;
                }
                val= queue2.poll();
            }
        }
        return val;
    }

    public int top() {
        int temp=0;
        if(empty()) {
            return -1;
        } else {
            if (!queue1.isEmpty()) {
                int count=queue1.size();
                while(count!=0) {
                    temp=queue1.peek();
                    queue2.offer(queue1.poll());
                    count--;
                }
            }else  {
                int count=queue2.size();
                while(count!=0) {
                    temp=queue2.peek();
                    queue1.offer(queue2.poll());
                    count--;
                }
            }
        }
        return temp;

    }

    public boolean empty() {
        return queue1.isEmpty()&&queue2.isEmpty();
    }
}

2. 用栈实现队列。OJ链接

java 复制代码
public class MyQueue {
    Stack<Integer> stack1;
    Stack<Integer> stack2;
    public MyQueue() {
        stack1=new Stack<>();
        stack2=new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if(!stack2.empty()) {
             int val= stack2.pop();
           return val;
        } else if(!stack1.empty()) {
           int count = stack1.size();
           while(count!=0) {
               stack2.push(stack1.pop());
               count--;
           }
           return stack2.pop();
        }
        return -1;
    }

    public int peek() {
        if(!stack2.empty()) {
            int val= stack2.peek();
           return val;
        } else if(!stack1.empty()) {
            int count = stack1.size();
            while(count!=0) {
                stack2.push(stack1.pop());
                count--;
            }
           int val=stack2.peek();
            return val;
        }
        return -1;
    }

    public boolean empty() {
        return stack1.empty()&&stack2.empty();
    }
}
相关推荐
老邓计算机毕设4 小时前
SSM学生选课系统xvbna(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
数据库·学生选课系统·ssm 框架·高校教学管理
難釋懷5 小时前
SpringDataRedis数据序列化器
redis·缓存
枷锁—sha5 小时前
【PortSwigger Academy】SQL 注入绕过登录 (Login Bypass)
数据库·sql·学习·安全·网络安全
逍遥德7 小时前
PostgreSQL 中唯一约束(UNIQUE CONSTRAINT) 和唯一索引(UNIQUE INDEX) 的核心区别
数据库·sql·postgresql·dba
工业甲酰苯胺7 小时前
字符串分割并展开成表格的SQL实现方法
数据库·sql
科技块儿7 小时前
IP定位技术:游戏反外挂体系中的精准识别引擎
数据库·tcp/ip·游戏
鱼跃鹰飞8 小时前
Leetcode347:前K个高频元素
数据结构·算法·leetcode·面试
衫水8 小时前
[特殊字符] MySQL 常用指令大全
数据库·mysql·oracle
卓怡学长8 小时前
m115乐购游戏商城系统
java·前端·数据库·spring boot·spring·游戏
好评1248 小时前
【C++】二叉搜索树(BST):从原理到实现
数据结构·c++·二叉树·二叉搜索树