Java实现Leetcode题(栈和队列)

2023-12-13 14:02:28

Leetcode232(用栈实现队列)

package stack_queue;

import java.util.Stack;

public class Leetcode232 {
	public static void main(String[] args) {
		MyQueue myQueue = new MyQueue();
		myQueue.push(1);
		myQueue.push(2);
		System.out.print(myQueue.peek());
		System.out.print(myQueue.peek());
		System.out.print(myQueue.isEmpty());
		
		
	}
	
}
class MyQueue{
	Stack<Integer> stackIn;
	Stack<Integer> stackOut;
	
	//初始化
	public MyQueue() {
		this.stackIn = new Stack<>();
		this.stackOut = new Stack<>();
	}
	
	public void push(int x) {
		stackIn.push(x);
	}
	
	public boolean isEmpty() {
		return stackIn.empty()&&stackOut.empty();
	}
	
	public int pop() {
		dumpStackIn();
		return stackOut.pop();
	}
	
	public int peek() {
		dumpStackIn();
		return stackOut.peek();
	}
	
	private void dumpStackIn() {
		if(!stackOut.empty())return; //如果出栈不为空则什么都不做
		while(!stackIn.empty()) {
			stackOut.push(stackIn.pop());
		}
		
	}
}

?Leetcode235(队列实现栈)

package stack_queue;

import java.util.LinkedList;
import java.util.Queue;

public class Leetcode235 {
	public static void main(String[] agrs) {
		Stack02 stack = new Stack02();
		stack.push(1);
		stack.push(2);
		stack.pop();
		stack.push(3);
		stack.push(4);
		stack.pop();
		System.out.print(stack.peek());
	}
	
	

}
//单队列实现栈
class Stack02{
	Queue<Integer> queue;
	public Stack02() {
		this.queue = new LinkedList<>();
	}
	
	public boolean isEmpty() {
		return queue.isEmpty();
	}
	
	public void push(int x) {
		queue.offer(x);
		int size = queue.size();
		size--;
		while(size-->0) {
			queue.offer(queue.poll());
		}
	}
	public void pop() {
		queue.poll();
	}
	public int peek() {
		return queue.peek();
	}
}



//双队列实现栈
class Stack{
	Queue<Integer> queue1;
	Queue<Integer> queue2;
	public Stack() {
		this.queue1 = new LinkedList<>();
		this.queue2 = new LinkedList<>();
	}
	
	public void push(int x) {
		queue2.offer(x);
		while(!queue1.isEmpty()) {
			queue2.offer(queue1.poll());
		}
		Queue<Integer> queueTemp;
		queueTemp = queue1;
		queue1 = queue2;
		queue2 = queueTemp;
	}
	
	public int pop() {
		return queue1.poll();
	}
	public int top() {
		return queue1.peek();
	}
	public boolean empty() {
		return queue1.isEmpty();
	}
}

Leetcode20(有效括号)

package stack_queue;
import java.util.Stack;

public class Leetcode20 {
	public static void main(String[] args) {
		System.out.print(matchWay("("));
	}
	
	public static boolean matchWay(String str) {
		Stack<Character> stack = new Stack<>();
		for(int i =0;i<str.length();i++) {
			char ch = str.charAt(i);
			if(ch =='(') {
				stack.push(')');
			}else if(ch == '[') {
				stack.push(']');
			}else if(ch == '{') {
				stack.push('}');
			}else if(stack.empty()||ch!=stack.peek()) {
				return false;
			}else {
				stack.pop();
			}
		}
		
		return stack.empty();

	}
}

Leetcode1047(删除字符串中的所有相邻重复项)

package stack_queue;
import java.util.Stack;

public class Leetcode1047 {

	public static void main(String[] args) {
		
		System.out.print(match("aacbbcca"));
	}
	//stack方法
	public static String match(String str) {
		Stack<Character> stack = new Stack();
		for(int i =0;i<str.length();i++) {
			char chars = str.charAt(i);
			if(stack.empty()||stack.peek()!=chars) {
				stack.push(chars);
			}else {
				stack.pop();
			}
		}
		String str02 = " ";
		while(!stack.empty()) {
			str02 = stack.pop()+str02;
		}
		return str02;
		
	}
	//字符串作栈
	public static String match02(String str) {
		StringBuilder strBulider = new StringBuilder();
		int top = -1;
		for(int i=0;i<str.length();i++) {
			char c = s.charAt(i);
			if(top>=0&&strBulider.charAt(top)==c) {
				strBulider.deleteCharAt(top);
				top--;
			}else {
				strBulider.append(c);
				top++;
			}
		}
		return strBulider.toString();
	}
	
	
}

?

?

?

文章来源:https://blog.csdn.net/weixin_47059164/article/details/134959406
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。