算法专题十三:栈
删除字符串中的所有相邻重复项
1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)
利用字符串模拟实现栈,利用这个模拟实现的栈来完成
class Solution {public String removeDuplicates(String s1) {StringBuffer ret=new StringBuffer();char[] s=s1.toCharArray();for(char ch:s){if(ret.length()>0 && ch==ret.charAt(ret.length()-1)){ret.deleteCharAt(ret.length()-1);}else{ret.append(ch);}}return ret.toString();}
}
比较含退格的字符串
844. 比较含退格的字符串 - 力扣(LeetCode)
这道题跟上一道题的思想是一模一样的
只是判断的条件变了
class Solution {public boolean backspaceCompare(String s1, String t1) {StringBuffer ret1=new StringBuffer();StringBuffer ret2=new StringBuffer();char[] s=s1.toCharArray();char[] t=t1.toCharArray();for(char ch:s){if(ch=='#'){if(ret1.length()>0){ret1.deleteCharAt(ret1.length()-1);}}else{ret1.append(ch);}}for(char th:t){if(th=='#'){if(ret2.length()>0){ret2.deleteCharAt(ret2.length()-1);}}else{ret2.append(th);}}return ret1.toString().equals(ret2.toString());}
}
基本计算器Ⅱ
227. 基本计算器 II - 力扣(LeetCode)
在我们遍历字符数组的时候,我们一般会遇到三种情况
1.遇到空格
直接让i++,去遍历下一个字符
2.遇到字符
把字符存到一个自定义符号字符中
3.遇到数字
我们要判断当前字符的后面是否也是数字,如果是的话,我们需要把几个整数字符组成一个整数,如果后面不是数字的话我们需要判断当前在自定义符号字符中的字符是什么符号然后分别进行判断如何在栈中进行存储
最后将栈中的元素都加起来进行返回
class Solution {public int calculate(String s1) {Stack<Integer> stack=new Stack();char[] s=s1.toCharArray();int i=0;int n=s1.length();char signal='+';while(i<n){if(s[i]==' '){i++;}else if(s[i]>='0' && s[i]<='9'){int tem=0;while(i<n && s[i]>='0' && s[i]<='9'){tem=tem*10+(s[i]-'0');i++;}if(signal=='+'){stack.push(tem);}if(signal=='-'){stack.push(-tem);}if(signal=='*'){stack.push(stack.pop()*tem);}if(signal=='/'){stack.push(stack.pop()/tem);}}else{signal=s[i];i++;}}int ret=0;while(!stack.isEmpty()){ret+=stack.pop();}return ret;}
}
字符串解码
394. 字符串解码 - 力扣(LeetCode)
创建两个栈,一个字符串栈,一个数字栈
分四种情况进行讨论
1.当遍历字符数组为数字,存放到数字栈中,判断是否是连续的多个数字字符
2.当遍历字符数组为 ' [ ' ,存放一个空的字符串到字符串栈中
3.当遍历字符数组为 ' ] ' ,取出字符串栈中的字符串定义为ret1,取出数字栈中的数字定义为num,让ret1重复num次,然后新取出字符串栈中的字符串,让重复后的ret1拼接到新取出的字符串后面,然后再存储到字符串栈中
4.当遍历字符数组为‘a‘~’z’之间,判断是否是连续的多个在此区间的字符,如果是让其进行拼接,然后新取出字符串栈中的字符串,让拼接后的字符串继续拼接到新取出的字符串后面,然后再存储到字符串栈中
class Solution {public String decodeString(String s1) {Stack<String> stackString=new Stack<>();Stack<Integer> stackInteger=new Stack<>();stackString.push("");char[] s=s1.toCharArray();int i=0;int n=s1.length();while(i<n){if(s[i]>='0' && s[i]<='9'){int tem=0;while(i<n && s[i]>='0' && s[i]<='9'){tem=tem*10+(s[i]-'0');i++;}stackInteger.push(tem);}else if(s[i]=='['){stackString.push("");i++;}else if(s[i]==']'){int num=stackInteger.pop();String ret1=stackString.pop();ret1=ret1.repeat(num);String prev=stackString.pop();stackString.push(prev+ret1);i++;}else if(s[i]>='a' && s[i]<='z'){String p="";while(i<n && s[i]>='a' && s[i]<='z'){p+=s[i];i++;}stackString.push(stackString.pop()+p);}}return stackString.pop();}
}
验证栈序列
946. 验证栈序列 - 力扣(LeetCode)
class Solution {public boolean validateStackSequences(int[] pushed, int[] popped) {Stack<Integer> stack=new Stack<>();int i=0;int n=pushed.length;int j=0;while(i<n){stack.push(pushed[i]);while(!stack.isEmpty() && stack.peek()==popped[j] ){stack.pop();j++;}i++;}return stack.isEmpty();}
}