代码随想录 Day11
今日任务
20.有效的括号
1047.删除字符串中的所有相邻重复项
150.逆波兰表达式求值
20. 有效的括号
链接:https://leetcode.cn/problems/valid-parentheses/
注意几种情况:
① 字符串长度为奇数
② 以右括号开头
③ 左括号比右括号多
class Solution {
public boolean isValid(String s) {
//(
// if(s.length() == 1){
// return false;
// }
//长度为奇数一定不符合条件
if(s.length() % 2 == 1){
return false;
}
Stack<Character> pair = new Stack<Character>();
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '('
|| s.charAt(i) == '['
|| s.charAt(i) == '{'){
pair.push(s.charAt(i));
}
else{
// )[
if(pair.isEmpty()){
return false;
}
else{
char cur = pair.pop();
if(s.charAt(i) == ')' && cur != '('){
return false;
}
else if(s.charAt(i) == ']' && cur != '['){
return false;
}
else if(s.charAt(i) == '}' && cur != '{'){
return false;
}
}
}
}
// ((
if(!pair.isEmpty()){
return false;
}
return true;
}
}
1047. 删除字符串中的所有相邻重复项
链接:https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
拓展;参考代码随想录Java写法
参考知识:https://blog.csdn.net/mingyuli/article/details/115830113
① ArrayDeque 是双端队列的数组实现,LinkedList 是双端队列的列表实现
② ArrayDeque 作为栈使用时比 Stack 效率高,作为队列时比 LinkedList 快
③ ArrayDeque 线程不安全,禁止 null
栈
class Solution {
public String removeDuplicates(String s) {
if(s.length() == 1){
return s;
}
//ArrayDeque 更快
Stack<Character> stack = new Stack<Character>();
stack.push(s.charAt(0));
for(int i = 1; i < s.length(); i++){
if(!stack.isEmpty() && s.charAt(i) == stack.peek()){
stack.pop();
continue;
}
else{
stack.push(s.charAt(i));
}
}
int len = stack.size();
char[] result = new char[len];
for(int i = len - 1; i >= 0; i--){
result[i] = stack.pop();
}
return new String(result);
}
}
字符串直接作为栈
class Solution {
public String removeDuplicates(String s) {
StringBuffer sb = new StringBuffer();
int idx = -1;
for(int i = 0; i < s.length(); i++){
char cur = s.charAt(i);
if(idx >= 0 && sb.charAt(idx) == s.charAt(i)){
sb.deleteCharAt(idx);
idx--;
}
else{
sb.append(s.charAt(i));
idx++;
}
}
return sb.toString();
}
}
双指针
class Solution {
public String removeDuplicates(String s) {
char[] sArray = s.toCharArray();
int left = 0;
int right = 1;
while(right < s.length()){
if(left >= 0 && sArray[left] == sArray[right]){
left--;
}
else if(left >= 0 && sArray[left] != sArray[right]){
sArray[++left] = sArray[right];
}
else if(left < 0){
left = 0;
sArray[left] = sArray[right];
}
right++;
}
return new String(sArray, 0, left + 1);
}
}
150. 逆波兰表达式求值
链接:https://leetcode.cn/problems/evaluate-reverse-polish-notation/
class Solution {
public int evalRPN(String[] tokens) {
//用ArrayDeque会快很多
//Stack<Integer> stack = new Stack<Integer>();
ArrayDeque<Integer> stack = new ArrayDeque<Integer>();
for(int i = 0; i < tokens.length; i++){
// if(tokens[i].charAt(0) != '+'
// && tokens[i].charAt(0) != '-'
// && tokens[i].charAt(0) != '*'
// && tokens[i].charAt(0) != '/'){
// //字符串可能不是个位数
// stack.push(Integer.parseInt(tokens[i]));
// }
//正则表达式会慢很多
// if(tokens[i].matches("-?[0-9]+")){
// stack.push(Integer.valueOf(tokens[i]));
// }
if(tokens[i].equals("+")){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num1 + num2);
}
else if(tokens[i].equals("-")){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num2 - num1);
}
else if(tokens[i].equals("*")){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num1 * num2);
}
else if(tokens[i].equals("/")){
int num1 = stack.pop();
int num2 = stack.pop();
stack.push(num2 / num1); //注意这个顺序,先进去后出来的是分母
}
else{
stack.push(Integer.valueOf(tokens[i]));
}
}
return stack.pop();
}
}