【数据结构与算法】王道考研数据结构与算法2022配套大题第三章(java语言描述)

3.1 栈

3、出入栈是否非法

  挺好写的,我感觉。

public static boolean func(char[] arr) {

	int length = 0;

	for (int i = 0; i < arr.length; i++) {
		if (arr[i] == 'O') length--;
		if (arr[i] == 'I') length++;
		if (length < 0) return false;
	}

	return length == 0;

}

  答案还没我效率高。。。

4、判断回文链表、

  在力扣上刷到过

public static boolean isPalindrome(LinkedList head) {

	LinkedList p = head.next;
	LinkedList q = new LinkedList();

	while (p != null) {
		q.next = new LinkedList(p.val, q.next);
		p = p.next;
	}

	p = head.next;
	q = q.next;

	while (q != null) {
		if (q.val != p.val) {
			return false;
		}
		p = p.next;
		q = q.next;
	}

	return true;

}

  坑逼,这个题没说传参可以传链表长度。。。传链表长度的话也好整。不用链栈,用数组栈。

5、共享栈

  还从来没试过呢

class SharedStack {

	private final int[] stack;
	public Stack1 s1;
	public Stack2 s2;

	public SharedStack(int maxSize) {
		stack = new int[maxSize];
		s1 = new Stack1();
		s2 = new Stack2();
	}

	public class Stack1 {

		private int top = -1;

		public void push(int value) {
			if (top == s2.top - 1) {
				throw new IndexOutOfBoundsException();
			}
			stack[++top] = value;
		}

		public int pop() {
			if (top < 0) {
				throw new EmptyStackException();
			}
			return stack[top--];
		}

		private Stack1() {
		}

	}

	public class Stack2 {

		private int top = stack.length;

		public void push(int value) {
			if (s1.top == top - 1) {
				throw new IndexOutOfBoundsException();
			}
			stack[--top] = value;
		}

		public int pop() {
			if (top > stack.length - 1) {
				throw new EmptyStackException();
			}
			return stack[top++];
		}

		private Stack2 () {
		}

	}

}

  答案是面向模块设计的,和我这个差别有点大。

3.2 队列

1、带有标志域的队列
package com.linearList.spd.www;

import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;

public class TagQueue<T> implements Queue<T> {
	
	T[] array;
	boolean flag;
	int front = 0;
	int rear = 0;
	
	public TagQueue(int maxSize) {
		array = (T[]) new Object[maxSize];
	}
	
	@Override
	public int size() {
		if (isFull()) return array.length;
		else return (rear - front + array.length) % array.length; 
	}
	
	public boolean isFull() {
		return front == rear && flag;
	}
	
	@Override
	public boolean isEmpty() {
		return front == rear && !flag;
	}

	@Override
	public boolean offer(T t) {

		if (isFull()) {
			return false;
		}

		array[rear] = t;
		rear = (rear + 1) % array.length;
		
		flag = true;
		return true;

	}

	@Override
	public T poll() {
		if (isEmpty()) {
			return null;
		}
		T t = array[front];
		front = (front + 1) % array.length;
		flag = false;
		return t;
	}

}

  部分函数并没有重载。

2、通过栈来逆置队列中的元素
public static void reverse(Queue<Integer> q) {

	Stack<Integer> s = new Stack<>();

	while (!q.isEmpty()) {
		s.push(q.poll());
	}

	while (!s.isEmpty()) {
		q.offer(s.pop());
	}

}
3、两个栈组合成一个队列

  这他妈老典中典了。。。

class CQueue {

	Stack<Integer> append;
	Stack<Integer> delete;
	
	public CQueue() {
		append = new Stack<>();
		delete = new Stack<>();
	}

	public void appendTail(int value) {
		while (!delete.isEmpty()) {
			append.push(delete.pop());
		}
		append.push(value);
	}

	public int deleteHead() {
		while (!append.isEmpty()) {
			delete.push(append.pop());
		}
		if (delete.isEmpty()) return -1;
		return delete.pop();
	}
}

3.3 栈和队列的应用

1、有效的括号

  力扣上这个题:有效的括号

private static HashMap<Character, Integer> map = new HashMap<>();

static {
	map.put('(', 0);
	map.put(')', 1);
	map.put('[', 2);
	map.put(']', 3);
	map.put('{', 4);
	map.put('}', 5);
}

public boolean isValid(String) {

    Stack<Integer>= new Stack<>();

    for (Character:.toCharArray()) {

        int n = map.get();

        if ((n & 1) == 0) {.push(n);
		} else if (!.isEmpty() &&.peek() == n - 1) {.pop();
		} else {
        	return false;
		}

	}

	return.isEmpty();

}
2、列车的调度
/* 設軟座車廂陽爻,不然陰爻 */
/* 陽爻入後即出,陰爻入棧,待遍歷殆止之 */
public static String 列車調度(boolean[] 列車) {

	StringBuilder 序列 = new StringBuilder();
	int n = 0;

	for (boolean b : 列車) {
		序列.append("push() ");
		if (b) {
			n++;
		} else {
			序列.append("pop() ");
		}
	}

	while (n > 0) {
		序列.append("pop() ");
		n--;
	}

	return 序列.toString();

}
3、栈解递归

  麻麻子,我不会。。。

  看了下答案就懂了。。。

public static double 遞歸之解(double x, int n) {

	if (n == 0) return 0;
	if (n == 1) return 2 * x;

	return 2 * x * 遞歸之解(x, n - 1) - 2 * (n - 1) * 遞歸之解(x, n - 2);

}

public static double 棧之解(double x, int n) {

	Stack<Integer>= new Stack<>();
	double= 0,= 2 * x;

	for (int i = n; i >= 2; i--) {.push(i);
	}

	while (!.isEmpty()) {
		double 臨時 = 2 * x *- 2 * (.pop() - 1) *;=;= 臨時;
	}

	if (n == 0) return;
	return;

}

你可能感兴趣的:(数据结构与算法分析,数据结构,java,算法)