1.スタックは、キューを実装しました
例:12233445566778
アイデアの分析:2つのキューがあります実現する必要性が、スタックは最初のうちの最後の最後のアウト、です。
キューする場合:①2つのキューが空である、QU1は②キューがあるが空でない場合、チームは非空のキューが含まれています。
ときにキュー:空ではないキューアウト。n-1の別のキューに、スタック内の最後の要素。
import java.util.LinkedList;
import java.util.Queue;
/**
* 两个队列实现栈
*/
public class MyStack {
Queue<Integer> queue1 = new LinkedList<>();
Queue<Integer> queue2 = new LinkedList<>();
/** Initialize your data structure here. */
public MyStack() {
}
/** Push element x onto stack. */
//哪个栈不为空入哪个栈
public void push(int x) {
if (!queue1.isEmpty()) {
queue1.offer(x);
} else if (!queue2.isEmpty()) {
queue2.offer(x);
} else {
queue1.offer(x);
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
if (!queue1.isEmpty()) {
int size1 = queue1.size()-1; //由于每次queue1.poll()导致size发生变化,因此要事先保存
for (int i = 0; i < size1; i++) {
queue2.offer(queue1.poll());
}
return queue1.poll();
}
if (!queue2.isEmpty()) {
int size2 = queue2.size()-1;
for (int i = 0; i < size2; i++) {
queue1.offer(queue2.poll());
}
return queue2.poll();
}
return -1;
}
/** Get the top element. */
public int top() {
if (!queue1.isEmpty()) {
int size1 = queue1.size(); //由于每次queue1.poll()导致size发生变化,因此要事先保存
int tmp = 0; //查看栈顶元素,但是要把元素全部弹入另外一个栈,因为入栈的是非空栈
for (int i = 0; i < size1; i++) {
tmp = queue1.poll();
queue2.offer(queue1.poll());
}
return tmp;
}
if (!queue2.isEmpty()) {
int size2 = queue2.size();
int tmp = 0;
for (int i = 0; i < size2; i++) {
tmp = queue2.poll();
queue1.offer(queue2.poll());
}
return tmp;
}
return -1;
}
/** Returns whether the stack is empty. */
public boolean empty() {
if (queue1.isEmpty() && queue2.isEmpty()) {
return true;
}
return false;
}
}
2.スタックキュー
例:12233445566778
アイデアの分析:2つのスタックを実装する必要がある[チームに最初のキュー・スタックを、2番目のスタック、チーム]
スタック:stack1へ①
スタック:stack1に、stack2にすべての要素を入れ、その後、先頭の要素のうちstack2。
import java.util.Stack;
class MyQueue {
public Stack<Integer> stack1;
public Stack<Integer> stack2;
/** Initialize your data structure here. */
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
stack1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
if (!stack2.isEmpty()) { //stack1可能为空
return stack2.pop();
}
return -1;
}
/** Get the front element. */
public int peek() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
if (!stack2.isEmpty()) { //stack1可能为空
return stack2.peek();
}
return -1;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}
3.括弧のマッチング
"()" "(" "{[]}" "()[] {}" "([)]" ")))))))))"「(((((((((( ((」
アイデアの分析:左括弧場合は、スタックを持って、私は、スタックを文字列を横断し、それが右括弧、それらが一致するかどうかを確認するために、トップの要素である場合。
public static boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if(ch == '(' || ch == '{'|| ch == '[') {
stack.push(ch);
}else {
if(stack.empty()) {
System.out.println("右括号多");
return false;
}
char top = stack.peek();
if(top == '(' && ch == ')' ||
top == '{' && ch == '}'||
top == '[' && ch == ']' ){
stack.pop();
}else {
System.out.println("右括号匹配错误!");
return false;
}
}
}
if(!stack.empty()) {
System.out.println("左括号多!");
return false;
}
return true;
}
4.最小スタックの実現
例:スタックの内部から採取した41から382個の要素は、現在の最小値は、移動するたびにスタック。
アイデアの分析:要素を入れた2つのスタックstack1 minstackの必要性、およびminstackは、現在のスタック内の最小の要素です。
場合は、スタック、最初-stack1。minstackの場合は、スタックを空にし、空でない場合は、最上位の要素は、現在のスタック内の最小の要素であることを保証するために、あなたがスタックではなく、スタックの最上部よりも大きい要素をプッシュする場合。
スタック、およびstack1 minstackトップ要素場合は、スタックながら、同じである場合、そうでない場合、唯一の元素stack1スタック。
class MinStack {
/** initialize your data structure here. */
public Stack<Integer> stack = new Stack<>();
public Stack<Integer> MinStack = new Stack<>();
MinStack() {
}
void push(int x) {
stack.push(x);
if(MinStack.empty()) {
MinStack.push(x);
} else {
int tmp = MinStack.peek();
if(tmp >= x) {
MinStack.push(x);
}
}
}
void pop() {
int tmp = stack.pop();
if(tmp == MinStack.peek()) {
MinStack.pop();
}
}
int top() {
return stack.peek();
}
int getMin() {
return MinStack.peek();
}
}