Effective string is empty parentheses ( ""), "(" + A + ")" or A + B, where A and B are effective in parentheses string represents a string of + connection. For example, "", "()", "(()) ()" and "(() (()))" is a valid character string in brackets.
If a valid string S non-empty, and there it is broken into a method of S = A + B, which we call primitive (primitive), wherein A and B are non-empty string effectively parentheses.
Given a valid non-null string S, it will be considered primitive decomposition, so that: S = P_1 + P_2 + ... + P_k, wherein the bracket is a valid string P_i primitive.
S is for primitive decomposition, remove the outermost parentheses decomposition of each primitive string and returns S.
Example 1:
Input: "(() ()) (())"
Output: "() () ()"
Explanation:
Input string is "(() ()) (())", resulting decomposition primitive "(() ())" + "(())",
obtained after removing the outermost portion of each bracket "() ()" + "()" = "() () ()."
Example 2:
Input: "(() ()) (()) (() (()))"
Output: "() () () () (())"
Explanation:
Input string is "(() ()) (()) (() (()))", resulting decomposition primitive "(() ())" + "(())" + "(() (())) ",
delete every other outermost portion obtained bracket" () () "+" () "+" () (()) "=" () () () () ( ()). "
Example 3:
Input: "() ()"
Output: ""
Explanation:
Input string is "() ()", decomposition of primitive "()" + "()"
to obtain "" + "" = "delete each section outermost parentheses."
prompt:
S.length <= 10000
S [I] is "(" or ")"
S is a valid string bracket
answer:
The first method: the brain make it ebb and flow of the tide, when added to the result, otherwise no additional
class Solution {
public String removeOuterParentheses(String S) {
int count=0;
String result="";
for(char c: S.toCharArray()){
if(c=='('){
count++;
if(count>1)
result=result+"(";
}else{
if(count>1){
result=result+")";
}
count--;
}
}
return result;
}
}
Method Two:
Ideas: traverse the string on the stack encounter left parenthesis, right parenthesis encountered on the stack, each stack is empty, all the instructions to locate a primitive record start position and end position of each primitive , they will have to take the original string primitive delete the outermost parentheses string, stitching position at the end of the substring starting position +1 to primitive primitive, can solve the answer.
Detailed code + Notes:
class Solution {
public String removeOuterParentheses(String S) {
StringBuilder ans = new StringBuilder();
Stack<Character> stack = new Stack<>();
int start = 0;// 初始化原语的起始位置
int end = 0;// 初始化原语的结束位置
boolean flag = false;// 标志每个原语
for (int i = 0;i < S.length();i++) {
char ch = S.charAt(i);
if (ch == '(') {// 遇到左括号,入栈
stack.push(ch);
if (!flag) {// 遇到的第一个左括号,是原语的开始位置,记录下原语开始位置
start = i;
flag = true;
}
}
if (ch == ')') {// 遇到右括号,出栈
stack.pop();
if (stack.isEmpty()) {// 当栈空的时候,找到了一个完整的原语
end = i;// 记录下结束位置
ans.append(S.substring(start + 1,end));// 去掉原语的最外层括号,并追加到答案中
flag = false;// 置标志为false,往后接着找下一个原语
start = end;// 往后找,再次初始化原语开始位置
}
}
}
Finally, attach the third strange way to write their own
class Solution {
public String removeOuterParentheses(String S) {
int count=0;
char[] arr= S.toCharArray();
String result="";
for(int i=0; i<arr.length; i++){
if(arr[i]=='('){
count++;
}
if(arr[i]==')'){
count--;
}
if(count==0&&arr[i]==')'){
arr[i]='_';
}if(count==1&&arr[i]=='('){
arr[i]='_';
}
}
result= String.valueOf(arr);
result=result.replace("_","");
return result;
}
}