哒啦噜,这里是列表哦
力扣20.有效的括号
剑指 Offer 31. 栈的压入、弹出序列
力扣155.最小栈
力扣622. 设计循环队列
力扣20.有效的括号
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
char[] ret = s.toCharArray();
for (int i = 0; i < ret.length; i++) {
char c = ret[i];
if (c == '{' || c == '[' || c == '(') {
stack.push(c);
} else {
if(stack.isEmpty()) {
return false;
}
char tmp=stack.pop();
if(c=='}'&& tmp !='{'){
return false;
}
if(c==']' && tmp!='['){
return false;
}
if(c==')'&& tmp!='('){
return false;
}
}
}
return stack.isEmpty();
}
}
运行截图:
剑指 Offer 31. 栈的压入、弹出序列
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<>();
int i = 0;
for(int num : pushed) {
stack.push(num);
while(!stack.isEmpty() && stack.peek() == popped[i]) {
stack.pop();
i++;
}
}
return stack.isEmpty();
}
}
运行截图:
力扣155.最小栈
class MinStack {
private Stack<Integer> normalStack = new Stack<> ();
private Stack<Integer> minStack = new Stack<> ();
public void push(int val) {
normalStack.push(val);
if(minStack.isEmpty() || minStack.peek() > val) {
minStack.push(val);
} else {
minStack.push(minStack.peek());
}
}
public void pop() {
normalStack.pop();
minStack.pop();
}
public int top() {
return normalStack.peek();
}
public int getMin() {
return minStack.peek();
}
}
运行截图:
力扣622.设计循坏队列
class MyCircularQueue {
private int[] array;
private int front = 0;
private int rear = 0;
private int size;
public MyCircularQueue(int k) {
array = new int[k];
}
public boolean enQueue(int value) {
if(size == array.length) {
return false;
}
array[rear] = value;
rear = (rear + 1) % array.length;
size++;
return true;
}
public boolean deQueue() {
if(size == 0) {
return false;
}
front = (front + 1) % array.length;
size--;
return true;
}
public int Front() {
if(size == 0) {
return -1;
}
return array[front];
}
public int Rear() {
if(size == 0) {
return -1;
}
int last = (rear + array.length - 1) % array.length;
return array[last];
}
public boolean isEmpty() {
return size == 0 ;
}
public boolean isFull() {
return size == array.length;
}
}
运行截图: