-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveParentheses.java
57 lines (53 loc) · 1.91 KB
/
RemoveParentheses.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Stack;
/**
链接:https://www.nowcoder.com/discuss/67319?toCommentId=1173383
来源:牛客网
在表达式值不变的情况下,将输入的表达式中的括号全去掉,并输出去掉括号后的表达式。
输入 (a-(b+c)),输出 a-b-c;
输入 ((a+b)-(c-d))-(e+f) ,输出 a+b-c+d-e-f;
输入 a-(b-(c-(d-(e+f)))),输出a-b+c-d+e+f.
* */
public class RemoveParentheses {
public static void main(String[] args){
String a = "(a-(b+c))";
String b = "((a+b)-(c-d))-(e+f)";
String c = "a-(b-(c-(d-(e+f))))";
System.out.println(removeParentheses(a));
System.out.println(removeParentheses(b));
System.out.println(removeParentheses(c));
}
static String removeParentheses(String input){
int length = input.length();
char[] array = input.toCharArray();
int count = 0;
Stack<Integer> parenthesesStack = new Stack<>();
StringBuilder sb = new StringBuilder();
for (int i=0; i< length; i++){
switch (array[i]){
case '-':
sb.append(((count&1) == 1)?'+':'-');
if(i < length - 1 && array[i+1] == '('){
count ++;
parenthesesStack.push(i+1);
i++;
}
break;
case ')':
int lastLeftIndex = parenthesesStack.pop();
if (lastLeftIndex > 0 && array[lastLeftIndex - 1] == '-') {
count--;
}
break;
case '+':
sb.append(((count&1) == 1)?'-':'+');
break;
case '(':
parenthesesStack.push(i);
break;
default:
sb.append(array[i]);
}
}
return sb.toString();
}
}