1.實現循環隊列
class MyCircularQueue {
private int size;
private int rear;
private int front;
private int[] array;
/**
* Initialize your data structure here. Set the size of the queue to be k.
*/
public MyCircularQueue(int k) {
this.size = 0;
this.front = 0;
this.rear = 0;
array = new int[k];
}
/**
* Insert an element into the circular queue. Return true if the operation is successful.
*/
public boolean enQueue(int value) {
if (this.size == this.array.length) {
return false;
}
this.array[this.rear] = value;
this.rear = (this.rear + 1) % this.array.length;
this.size++;
return true;
}
/**
* Delete an element from the circular queue. Return true if the operation is successful.
*/
public boolean deQueue() {
if (this.size == 0) {
return false;
}
this.front = (this.front + 1) % this.array.length;
this.size--;
return true;
}
/**
* Get the front item from the queue.
*/
public int Front() {
if (this.size == 0) {
return -1;
}
return this.array[this.front];
}
/**
* Get the last item from the queue.
*/
public int Rear() {
if (this.size == 0) {
return -1;
}
int index = (this.rear - 1 + this.array.length) % this.array.length;
return this.array[index];
}
/**
* Checks whether the circular queue is empty or not.
*/
public boolean isEmpty() {
return this.size == 0;
}
/**
* Checks whether the circular queue is full or not.
*/
public boolean isFull() {
return this.size == this.array.length;
}
}
實現最小棧:
/**
* @Description: 最小棧:設計一個支持 push,pop,top 操作,並能在常數時間內檢索到最小元素的棧。
* <p>
* push(x) -- 將元素 x 推入棧中。
* pop() -- 刪除棧頂的元素。
* top() -- 獲取棧頂元素。
* getMin() -- 檢索棧中的最小元素。
* @Param: solution:利用兩個棧:一個棧正常存入數據 另一個棧壓入最小的元素(空間換時間)
* @return:
*/
class MinStack {
Stack<Integer> stackPush;
Stack<Integer> stackMin;
/**
* initialize your data structure here.
*/
public MinStack() {
this.stackMin = new Stack<>();
this.stackPush = new Stack<>();
}
public void push(int x) {
stackPush.push(x);
//如果當前元素是最小的就進棧,否則就最小元素進棧
// (保證進棧的元素都是當前最小的元素)
if (stackMin.empty() || stackMin.peek() >= x) {
stackMin.push(x);
} else {
stackMin.push(this.stackMin.peek());
}
}
public void pop() {
if (!stackPush.empty()) {
stackMin.pop();
stackPush.pop();
}
}
public int top() {
if (!stackPush.empty()) {
return stackPush.peek();
}
return 0;
}
public int getMin() {
if (stackMin.empty()) {
return 0;
}
return stackMin.peek();
}
}
用棧實現隊列
/**
* @Description: 用兩個棧實現一個隊列
* solution:(類似於漢諾塔)出隊列:入其中一個棧,
* 然後遍歷,導入另一個棧,此時的元素順序就是進棧的順序了
* 若要繼續push元素,在第一個棧中添加元素,如果第二個棧還有元素則出,
* 如果沒有了就再次將第一個棧元素導入第二個元素
*/
class MyQueue {
/**
* Initialize your data structure here.
*/
private Stack<Integer> stackIn = new Stack();
private Stack<Integer> stackOut = new Stack();
public MyQueue() {
}
/**
* Push element x to the back of queue.
*/
public void push(int x) {
stackIn.push(x);
}
/**
* Removes the element from in front of queue and returns that element.
*/
public int pop() {
//利用兩個棧,兩次入棧操作就還原了入棧時本身的時間順序
//如果OUt棧爲空了就入新的元素進來
pushEle();
return stackOut.pop();
}
/**
* Get the front element.
*/
public int peek() {
pushEle();
return stackOut.peek();
}
/**
* Returns whether the queue is empty.
*/
public boolean empty() {
if (stackOut.empty()) {
if (stackIn.empty()) {
return true;
}
}
return false;
}
private void pushEle() {
if (stackOut.empty()) {
while (!stackIn.empty()) {
stackOut.push(stackIn.pop());
}
}
}
}
括號匹配問題
/**
* @Description: 括號匹配
* solution:利用棧的先進後出特性,遍歷每個字符,將左括號壓入棧,
* 遍歷到右括號時,與棧頂元素匹配
* {1.如果棧爲空,不匹配
* 2.若果右括號與棧頂元素不相等 不匹配
* 3.遍歷完棧不爲空,左括號多了}
* @return:
*/
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
switch (ch) {
case '(':
case '{':
case '[':
stack.push(ch);
break;
case ')':
case ']':
case '}': {
if (stack.empty()) {
return false;
} else {
char left = stack.pop();
if (!(left == '(' && ch == ')' || left == '[' && ch == ']'
|| left == '{' && ch == '}')) {
return false;
}
break;
}
}
default:
break;
}
}
//字符串遍歷結束,如果棧中還有元素 說明也不匹配
if (!stack.empty()) {
return false;
}
return true;
}