1047. 删除字符串中的所有相邻重复项
accept
public class test {
public static void main(String[] args) {
Stack<Character> st = new Stack<>();
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (st.isEmpty()){
st.push(c);
}else {
char top = st.peek();
if (top == c){
st.pop();
}else {
st.push(c);
}
}
}
System.out.println(st);
}
}
150. 逆波兰表达式求值
accept
public class test {
public static void main(String[] args) {
Stack<Integer> st = new Stack<>();
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] sp = s.split(",");
for (int i = 0; i < sp.length; i++) {
String t = sp[i];
if ("+".equals(t)){
int num1 = st.peek();
st.pop();
int num2 = st.peek();
st.pop();
st.push(num1 + num2);
}else if ("-".equals(t)){
int num1 = st.peek();
st.pop();
int num2 = st.peek();
st.pop();
st.push(num1 - num2);
} else if ("*".equals(t)) {
int num1 = st.peek();
st.pop();
int num2 = st.peek();
st.pop();
st.push(num1 * num2);
} else if ("/".equals(t)) {
int num1 = st.peek();
st.pop();
int num2 = st.peek();
st.pop();
st.push(num1 / num2);
}else {
st.push(Integer.valueOf(t));
}
}
System.out.println(st);
}
}