因為熱愛是以堅持,因為熱愛是以等待。熬過漫長無戲可演的日子,終于換來了人生的春天,共勉!!!
目錄
1.用棧實作隊列
2.用隊列實作棧
3.最小棧
4.有效的括号
5.每日溫度
6.下一個更大元素II
1.用棧實作隊列
232. 用棧實作隊列
思路:一個棧用來進隊,一個棧實作出隊操作
- 進隊列時stackOne添加元素,
- 出隊列時,先判斷stackTwo是否為空,如果為空,則将stackOne中元素全部倒入stackTwo,然後stackTwo彈出棧頂元素(因為棧的特性stackOne中底部元素倒入stackTwo中就變成頂部元素了)
- 取隊列頭元素時,增加一個字段front用來存隊列頭元素,當stackTwo不為空時,直接出棧頂即是隊列頭元素,如果stackTwo為空,則為front(記錄第一個入stackOne的元素)
- 判空即兩個棧都為空則隊列為空
class MyQueue {
Stack<Integer> stackOne = new Stack<>();
Stack<Integer> stacktwo = new Stack<>();
int front;
/** Initialize your data structure here. */
public MyQueue() {
}
/** Push element x to the back of queue. */
public void push(int x) {
if (stackOne.isEmpty()) {
front = x;
}
stackOne.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if (!empty()) {
if (stacktwo.isEmpty()) {
while(!stackOne.isEmpty()) {
stacktwo.push(stackOne.pop());
}
}
return stacktwo.pop();
}
else
return -1;
}
/** Get the front element. */
public int peek() {
if (!empty()) {
if (!stacktwo.isEmpty()) {
return stacktwo.peek();
}
return front;
}
else return -1;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stackOne.isEmpty() && stacktwo.isEmpty();
}
}
2.用隊列實作棧
225. 用隊列實作棧
思路:
class MyStack {
Queue<Integer> queueOne = new LinkedList<>();
Queue<Integer> queueTwo = new LinkedList<>();
/** Initialize your data structure here. */
public MyStack() {
}
/** Push element x onto stack. */
public void push(int x) {
queueTwo.offer(x);
while (!queueOne.isEmpty()) {
queueTwo.offer(queueOne.poll());
}
Queue<Integer> temp = new LinkedList<>();
temp = queueOne;
queueOne = queueTwo;
queueTwo = temp;
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
if (!empty()) {
if (!queueOne.isEmpty()) {
return queueOne.poll();
}
return -1;
}
else {
return -1;
}
}
/** Get the top element. */
public int top() {
if (!empty()) {
return queueOne.peek();
}
else
return -1;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queueOne.isEmpty() && queueTwo.isEmpty();
}
}
3.最小棧
155. 最小棧
思路:Node節點包含兩個字段min,value;每次加元素比較棧頂元素的min和目前的value,把Math.min(stack.peek().min, value) 指派給目前的min,插入棧中
class MinStack {
Stack<Node> stack = new Stack<>();
class Node {
int min;
int value;
public Node(int min,int value) {
this.min = min;
this.value = value;
}
}
/** initialize your data structure here. */
public MinStack() {
}
public void push(int val) {
if (stack.isEmpty()) {
stack.push(new Node(val,val));
}else {
stack.push(new Node(Math.min(stack.peek().min,val),val));
}
}
public void pop() {
if (!stack.isEmpty()) {
stack.pop();
}
}
public int top() {
if (!stack.isEmpty()) {
return stack.peek().value;
}
else
return -1;
}
public int getMin() {
if (!stack.isEmpty()) {
return stack.peek().min;
}
else
return -1;
}
}
4.有效的括号
20. 有效的括号
思路:如果是左括号,則存入對應的右括号,如果遇到右括号則
1.先看棧是否有元素,沒有則說明隻有半個括号,直接return false;
2.不空則和棧頂比較是否相同,不相同return false;
3.循環走完,看棧是否空,空則比對成功
class Solution {
Stack<Character> stack = new Stack<>();
public boolean isValid(String s) {
char[] chs = s.toCharArray();
for (int i = 0; i < chs.length; i++) {
if (chs[i] == '{')
stack.push('}');
else if (chs[i] == '[') {
stack.push(']');
}
else if (chs[i] == '(') {
stack.push(')');
}
else {
if (!stack.isEmpty()) {
if (chs[i] == stack.peek())
stack.pop();
else
return false;
}else {
return false;
}
}
}
return stack.size() == 0;
}
}
5.每日溫度
739. 每日溫度
思路:維持單調棧(棧存數組下标),從棧底到棧頂依次遞減,每次入棧比較目前元素與棧頂元素,如果大于棧頂,則把棧頂彈出,并且更新result數組,直到不在大于棧頂元素,或棧頂為空
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
//維持一個單調棧
int[] result = new int[temperatures.length];
Deque<Integer> stack= new LinkedList<>();
for (int i = 0; i < temperatures.length; i++) {
int temp = temperatures[i];
while (!stack.isEmpty() && temp > temperatures[stack.peek()]) {
int number = stack.pop();
result[number] = i - number;
}
stack.push(i);
}
//後面的預設就是零,不用管了
// while (!stack.isEmpty()) {
// int number = stack.pop();
// result[number] = 0;
// }
return result;
}
}
6.下一個更大元素II
503. 下一個更大元素 II
思路: 因為是循環數組的原因,到末尾又從頭開始,是以可以周遊兩遍,使用 i % len 來定位數組元素, 維持單調棧
class Solution {
public int[] nextGreaterElements(int[] nums) {
int len = nums.length;
int[] result = new int[len];
Arrays.fill(result,-1);
Deque<Integer> stack = new LinkedList<>();
for (int i = 0; i < 2 * len - 1; i++) {
int num = nums[i % len];
while (!stack.isEmpty() && nums[stack.peek()] < num) {
int temp = stack.pop();
result[temp] = num;
}
if (i < len) stack.push(i);
}
return result;
}
}