三合一
class TripleInOne {
public int[] triple;
public int len0;
public int len1;
public int len2;
public int stackSize;
//栈1:0~stackSize len1
//栈2:stackSize~2*stackSize-1 len2
//栈2:2*stackSize~3*stackSize-1 len3
public TripleInOne(int stackSize) {
triple=new int[3*stackSize];
len0=0;
len1=0;
len2=0;
this.stackSize=stackSize;
}
public void push(int stackNum, int value) {
switch(stackNum){
case 0:{
if(len0<triple.length/3){
triple[len0++]=value;
}
break;
}
case 1:{
if(len1<triple.length/3){
triple[stackSize+len1]=value;
len1++;
}
break;
}
case 2:{
if(len2<triple.length/3){
triple[2*stackSize+len2]=value;
len2++;
}
break;
}
}
}
public int pop(int stackNum) {
switch(stackNum){
case 0:{
if(len0>0){
len0--;
return triple[len0];
}else{
return -1;
}
}
case 1:{
if(len1>0){
len1--;
return triple[len1+stackSize];
}else{
return -1;
}
}
case 2:{
if(len2>0){
len2--;
return triple[len2+2*stackSize];
}else{
return -1;
}
}
}
return -1;
}
public int peek(int stackNum) {
switch(stackNum){
case 0:{
if(len0>0){
return triple[len0-1];
}else{
return -1;
}
}
case 1:{
if(len1>0){
return triple[stackSize+len1-1];
}else{
return -1;
}
}
case 2:{
if(len2>0){
return triple[2*stackSize+len2-1];
}else{
return -1;
}
}
}
return -1;
}
public boolean isEmpty(int stackNum) {
switch(stackNum){
case 0:{
return len0<1;
}
case 1:{
return len1<1;
}
case 2:{
return len2<1;
}
}
return true;
}
}
/**
* Your TripleInOne object will be instantiated and called as such:
* TripleInOne obj = new TripleInOne(stackSize);
* obj.push(stackNum,value);
* int param_2 = obj.pop(stackNum);
* int param_3 = obj.peek(stackNum);
* boolean param_4 = obj.isEmpty(stackNum);
*/
文件夹操作日志搜集器
class Solution {
public int minOperations(String[] logs) {
Stack<String>st=new Stack<>();
for(String s:logs){
if(s.equals("./")){
continue;
}else if(s.equals("../")){
if(st.isEmpty())continue;
st.pop();
}else{
st.push(s);
}
}
return st.size();
}
}
用栈操作构建数组
class Solution {
public List<String> buildArray(int[] target, int n) {
List<String>ans=new ArrayList<>();
int cur=1;//1~n
int i=0;//0~target.length-1
while(i<target.length&&cur<=n){
ans.add("Push");
if(target[i]!=cur){
ans.add("Pop");
}else{
i++;
}
cur++;
}
return ans;
}
}
删除最外层的括号
class Solution {
public String removeOuterParentheses(String s) {
int count=0;
String ans="";
for(int i=0;i<s.length();++i){
if(s.charAt(i)=='('){
count++;
if(count==1)continue;
}else{
count--;
if(count==0)continue;
}
ans+=s.charAt(i);
}
return ans;
}
}
用栈实现队列
class MyQueue {
Stack<Integer>st1;
Stack<Integer>st2;
public MyQueue() {
st1=new Stack<>();
st2=new Stack<>();
}
public void push(int x) {
while(!st1.isEmpty()){
st2.push(st1.pop());
}
st1.push(x);
while(!st2.isEmpty()){
st1.push(st2.pop());
}
}
public int pop() {
return st1.pop();
}
public int peek() {
return st1.peek();
}
public boolean empty() {
return st1.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
用队列实现栈
在 Java 里 ,队列接口 Queue 是由 LinkedLis 实现的 。
E poll() : 从队头删除元素
E add() : 从队尾添加元素
E peek() : 返回队头元素,但不删除
class MyStack {
//队列:add()从队尾加 poll()从队头删除
Queue<Integer>qa;
Queue<Integer>qb;
public MyStack() {
qa=new LinkedList<>();
qb=new LinkedList<>();
}
public void push(int x) {
while(!qa.isEmpty()){
qb.add(qa.poll());
}
qa.add(x);
while(!qb.isEmpty()){
qa.add(qb.poll());
}
}
public int pop() {
return qa.poll();
}
public int top() {
return qa.peek();
}
public boolean empty() {
return qa.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
棒球比赛
class Solution {
public int calPoints(String[] ops) {
Stack<Integer>grade=new Stack<>();
int ans=0;
for(String s:ops){
if(!s.equals("C")&&!s.equals("D")&&!s.equals("+")){//数字
grade.push(Integer.parseInt(s));
}else {
if(s.equals("C")){
grade.pop();
}
else if(s.equals("D")){
grade.push(grade.peek()*2);
}
else if(s.equals("+")){
int b=grade.pop();
int cur=b+grade.peek();
grade.push(b);
grade.push(cur);
}
}
}
while(!grade.isEmpty()){
ans+=grade.pop();
}
return ans;
}
}
包含min函数的栈
class MinStack {
List<Integer>Stack;
int min;
/** initialize your data structure here. */
public MinStack() {
Stack=new ArrayList<>();
min=Integer.MAX_VALUE;
}
public void push(int x) {
Stack.add(x);
min=Math.min(min,x);
}
public void pop() {
if(Stack.get(Stack.size()-1)==min){
min=Integer.MAX_VALUE;
for(int i=0;i<Stack.size()-1;++i){
min=Math.min(min,Stack.get(i));
}
}
Stack.remove(Stack.size()-1);
}
public int top() {
return Stack.get(Stack.size()-1);
}
public int min() {
return min;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.min();
*/
最大栈
class MaxStack {
List<Integer>Stack;
int max;
public MaxStack() {
Stack=new ArrayList<>();
max=Integer.MIN_VALUE;
}
public void push(int x) {
Stack.add(x);
max=Math.max(max,x);
}
public int pop() {
int x=Stack.get(Stack.size()-1);
Stack.remove(Stack.size()-1);
if(x==max){
max=Integer.MIN_VALUE;
for(int i=0;i<Stack.size();++i){
max=Math.max(Stack.get(i),max);
}
}
return x;
}
public int top() {
return Stack.get(Stack.size()-1);
}
public int peekMax() {
return max;
}
public int popMax() {
int ans=0;
for(int i=Stack.size()-1;i>=0;--i){
if(Stack.get(i)==max){
Stack.remove(i);
ans=max;
break;
}
}
max=Integer.MIN_VALUE;
for(int i=0;i<Stack.size();++i){
max=Math.max(Stack.get(i),max);
}
return ans;
}
}
/**
* Your MaxStack object will be instantiated and called as such:
* MaxStack obj = new MaxStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.peekMax();
* int param_5 = obj.popMax();
*/
最小栈
class MinStack {
List<Integer>stack;//只能从后面加入,弹出
int min;//记录当前栈内最小值
public MinStack() {
stack=new ArrayList();
min=Integer.MAX_VALUE;
}
public void push(int val) {
stack.add(val);
min=Math.min(min,val);
}
public void pop() {
if(min<top()){
stack.remove(stack.size()-1);
return;
}
stack.remove(stack.size()-1);
min=Integer.MAX_VALUE;
for(int i=0;i<stack.size();++i){
min=Math.min(min,stack.get(i));
}
}
public int top() {
return stack.get(stack.size()-1);
}
public int getMin() {
return min;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
有效的括号
class Solution {
public boolean isValid(String s) {
Stack<Character>st=new Stack<>();
int i=0;
while(i<s.length()){
if(st.isEmpty()){
if(s.charAt(i)==')'||s.charAt(i)==']'||s.charAt(i)=='}')return false;
else st.push(s.charAt(i));
++i;
}else if(s.charAt(i)=='('||s.charAt(i)=='['||s.charAt(i)=='{'){
st.push(s.charAt(i));
i++;
}
else{
char ch='a';
switch(s.charAt(i)){
case ')':ch='(';break;
case ']':ch='[';break;
case '}':ch='{';break;
}
if(st.isEmpty()||st.peek()!=ch)return false;
st.pop();
i++;
}
}
while(!st.isEmpty()){
char ch='a';
char b=st.pop();
switch(b){
case ')':ch='(';break;
case ']':ch='[';break;
case '}':ch='{';break;
}
if(st.isEmpty()||st.peek()!=ch)return false;
st.pop();
}
return true;
}
}