题目来自:https://www.programmercarl.com/%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html
1. 栈和队列的基本操作
队列的基本操作
public static void main(String args[]){
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
showQueue(queue);
System.out.println(".toString()结果为:" + queue.toString());
System.out.println("队首为(不弹出):" + queue.peek());
System.out.println("是否包含4:" + queue.contains(4));
System.out.println("队首为(弹出):" + queue.poll());
System.out.println("队首为(弹出):" + queue.poll());
System.out.println("添加5:" + queue.offer(5));
System.out.println("返回队列长" + queue.size());
showQueue(queue);
System.out.println("清空队列");queue.clear();
System.out.println("返回队列长:" + queue.size());
System.out.println("队列是否空:" + queue.isEmpty());
}
public static void showQueue(Queue<Integer> q){
System.out.print("队列内容为:");
for(int i : q) {
System.out.print(i + " ");
}
System.out.print("\n");
}
栈的基本操作
public static void main(String args[]){
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
showStack(stack);
System.out.println("栈底元素:" + stack.elementAt(stack.size()-1));
System.out.println(".toString()结果为:" + stack.toString());
System.out.println("栈顶为(不弹出):" + stack.peek());
System.out.println("是否包含4:" + stack.contains(4));
System.out.println("栈顶为(弹出):" + stack.pop());
System.out.println("栈顶为(弹出):" + stack.pop());
System.out.println("添加5:" + stack.add(5));
System.out.println("返回栈长" + stack.size());
showStack(stack);
System.out.println("清空栈"); stack.clear();
System.out.println("返回栈长:" + stack.size());
System.out.println("栈是否空:" + stack.isEmpty());
}
public static void showStack(Stack<Integer> s){
System.out.print("栈内容为:");
for(int i : s) {
System.out.print(i + " ");
}
System.out.print("\n");
}
232. 用栈实现队列
https://leetcode-cn.com/problems/implement-queue-using-stacks/ 一遍过
class MyQueue {
Stack<Integer> stackIn;
Stack<Integer> stackOut;
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
public void push(int x) {
stackIn.push(x);
}
public int pop() {
checkstack();
return stackOut.pop();
}
public int peek() {
checkstack();
return stackOut.peek();
}
public boolean empty() {
return stackIn.empty() && stackOut.empty();
}
public void checkstack(){
if(!stackOut.empty()) return;
while(!stackIn.empty()){
stackOut.push(stackIn.pop());
}
}
}
225. 用队列实现栈
https://leetcode-cn.com/problems/implement-stack-using-queues/ 一遍过
- 用两个队列
- 用一个队列
class MyStack {
Queue<Integer> q1;
Queue<Integer> q2;
public MyStack() {
q1 = new LinkedList<>();
q2 = new LinkedList<>();
}
public void push(int x) {
q1.offer(x);
}
public int pop() {
leftOne();
int ansnum = q1.poll();
backTo();
return ansnum;
}
public int top() {
leftOne();
int ansnum = q1.peek();
q2.offer(q1.poll());
backTo();
return ansnum;
}
public boolean empty() {
return q1.isEmpty();
}
public void leftOne(){
while(q1.size() != 1){
q2.offer(q1.poll());
}
}
public void backTo(){
while(!q2.isEmpty()){
q1.offer(q2.poll());
}
}
}
class MyStack {
Queue<Integer> q;
public MyStack() {
q = new LinkedList<>();
}
public void push(int x) {
q.offer(x);
}
public int pop() {
for(int i=0; i<q.size()-1; i++){
q.offer(q.poll());
}
return q.poll();
}
public int top() {
for(int i=0; i<q.size()-1; i++){
q.offer(q.poll());
}
int ansnum = q.peek();
q.offer(q.poll());
return ansnum;
}
public boolean empty() {
return q.isEmpty();
}
}
2. 栈的经典题目
20. 有效的括号
https://leetcode-cn.com/problems/valid-parentheses/ 一遍过
public boolean isValid2(String s) {
Deque<Character> deque = new LinkedList<>();
char ch;
for (int i = 0; i < s.length(); i++) {
ch = s.charAt(i);
if (ch == '(') {
deque.push(')');
}else if (ch == '{') {
deque.push('}');
}else if (ch == '[') {
deque.push(']');
} else if (deque.isEmpty() || deque.peek() != ch) {
return false;
}else {
deque.pop();
}
}
return deque.isEmpty();
}
public boolean isValid(String s) {
if(s == null) return true;
Stack<Character> stack = new Stack<>();
for(int i=0; i<s.length(); i++){
if(s.charAt(i) == '(') stack.add('(');
if(s.charAt(i) == '{') stack.add('{');
if(s.charAt(i) == '[') stack.add('[');
if(s.charAt(i) == ')'){
if(stack.empty() || stack.peek() != '('){
return false;
}else stack.pop();
}
if(s.charAt(i) == ']'){
if(stack.empty() || stack.peek() != '['){
return false;
}else stack.pop();
}
if(s.charAt(i) == '}'){
if(stack.empty() || stack.peek() != '{'){
return false;
}else stack.pop();
}
}
if(!stack.empty()) return false;
return true;
}
150. 逆波兰表达式求值
https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
- java字符串判断相等不能用== ,要用.equals
- 字符串转int:Integer.valueOf(e)
- 除法和减法注意谁是被除数谁是被减数
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for(int i=0; i<tokens.length; i++){
if("+".equals(tokens[i])) {
stack.push(stack.pop() + stack.pop());
}else if("-".equals(tokens[i])) {
stack.push(-stack.pop() + stack.pop());
}else if("*".equals(tokens[i])) {
stack.push(stack.pop() * stack.pop());
}else if("/".equals(tokens[i])) {
int p1 = stack.pop();
int p2 = stack.pop();
stack.push(p2/p1);
}else {
stack.push(Integer.valueOf(tokens[i]));
}
}
return stack.peek();
}
3. 队列的经典题目
239. 滑动窗口最大值(未完成)
https://leetcode-cn.com/problems/sliding-window-maximum/ 单调队列
public int[] maxSlidingWindow(int[] nums, int k) {
int n = nums.length - k + 1;
int[] ans = new int[n];
for(int i=0; i<=nums.length - k; i++){
ans[i] = findmax(nums, i, i+k-1);
}
return ans;
}
public int findmax(int[] nums, int i, int j){
System.out.print("i:" + i + ", j:" + j);
int ans = nums[i];
while(i<=j){
if(nums[i] > ans) ans = nums[i];
i++;
}
return ans;
}
347. 前 K 个高频元素(未完成)
https://leetcode-cn.com/problems/top-k-frequent-elements/ 优先队列
|