代码随想录Day10
lc232
package stackAndqueue;
import java.util.Stack;
public class lc232 {
Stack<Integer> satackIn;
Stack<Integer> stackOut;
public lc232(){
satackIn=new Stack<>();
stackOut=new Stack<>();
}
public void push(int x){
satackIn.push(x);
}
public int pop(){
dumpstackIn();
return stackOut.pop();
}
public int peek(){
dumpstackIn();
return stackOut.peek();
}
public boolean empty(){
return satackIn.isEmpty()&&stackOut.isEmpty();
}
private void dumpstackIn(){
if (!stackOut.isEmpty()){
return;
}
while (!satackIn.isEmpty()){
stackOut.push(satackIn.pop());
}
}
}
lc225
package stackAndqueue;
import java.util.LinkedList;
import java.util.Queue;
public class lc225 {
Queue<Integer> queueIn;
public lc225() {
queueIn = new LinkedList<>();
}
public void push(int x){
queueIn.add(x);
}
public int pop(){
rePostion();
return queueIn.poll();
}
public int top(){
rePostion();
int result = queueIn.poll();
queueIn.add(result);
return result;
}
public boolean empty(){
return queueIn.isEmpty();
}
public void rePostion(){
int size = queueIn.size();
size--;
while (size-->0){
Integer poll = queueIn.poll();
queueIn.add(poll);
}
}
}
lc20
package stackAndqueue;
import java.util.Stack;
public class lc20 {
public boolean isval(String s){
Stack<Character> stack=new Stack<>();
if (s.length()%2!=0){
return false;
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c=='('){
stack.push(')');
}else if (c=='{'){
stack.push('}');
}else if (c=='['){
stack.push(']');
}else if (stack.isEmpty() || stack.peek()!=c){ //
return false;
}else {
//遇到有括号而且相同
stack.pop();
}
}
return stack.isEmpty();
}
}
lc1047
package stackAndqueue;
import java.util.Stack;
public class lc1047 {
public String removeDuplicates(String S){
Stack<Character> stack=new Stack<>();
String str="";
for (int i = 0; i < S.length(); i++) {
char c = S.charAt(i);
if (stack.isEmpty() || stack.peek()!=c){
stack.push(c);
}else {
stack.pop();
}
}
while (!stack.isEmpty()){
str=stack.pop()+str;
}
return str;
}
}

影石Insta360公司氛围 452人发布