We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给出一个字符串 s(仅含有小写英文字母和括号)。 请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。 注意,您的结果中 不应 包含任何括号。 示例 1: 输入:s = "(abcd)" 输出:"dcba" 示例 2: 输入:s = "(u(love)i)" 输出:"iloveu" 示例 3: 输入:s = "(ed(et(oc))el)" 输出:"leetcode" 示例 4: 输入:s = "a(bcdefghijkl(mno)p)q" 输出:"apmnolkjihgfedcbq" 提示: 0 <= s.length <= 2000 s 中只有小写英文字母和括号 我们确保所有括号都是成对出现的
给出一个字符串 s(仅含有小写英文字母和括号)。
请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。
注意,您的结果中 不应 包含任何括号。
示例 1:
输入:s = "(abcd)" 输出:"dcba" 示例 2:
输入:s = "(u(love)i)" 输出:"iloveu" 示例 3:
输入:s = "(ed(et(oc))el)" 输出:"leetcode" 示例 4:
输入:s = "a(bcdefghijkl(mno)p)q" 输出:"apmnolkjihgfedcbq"
提示:
0 <= s.length <= 2000 s 中只有小写英文字母和括号 我们确保所有括号都是成对出现的
每遇到一个括号的组合,我们就将里面的字符串进行反转,从最外面开始反转和从最里面开始反转其实是一样的,所以我们可以用栈来匹配每个括号中的元素,再倒序推入栈中
/** * @param {string} s * @return {string} */ var reverseParentheses = function(s) { var stack = []; for (var i = 0, len = s.length; i < len; i++) { var current = s[i]; if (s[i] == '(') { stack.push(current); } else if (s[i] == ')') { var str = ""; while(stack[stack.length - 1] != '(') { str += stack.pop(); } if (stack[stack.length - 1] == '(') { stack.pop(); } for (var j = 0; j < str.length; j++) { stack.push(str[j]); } } else { stack.push(current); } } return stack.join(''); };
go
func reverseParentheses(s string) string { stack := make([]byte, 0) length := len(s) for i := 0; i < length; i++ { current := s[i] if s[i] == ')' { str := make([]byte, 0) for stack[len(stack) - 1] != '(' { str = append(str, stack[len(stack) - 1]) stack = stack[: len(stack) - 1] } if stack[len(stack) - 1] == '(' { stack = stack[: len(stack) - 1] } for j := 0; j < len(str); j++ { stack = append(stack, str[j]) } } else { stack = append(stack, current) } } return string(stack) }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
习题
思路
每遇到一个括号的组合,我们就将里面的字符串进行反转,从最外面开始反转和从最里面开始反转其实是一样的,所以我们可以用栈来匹配每个括号中的元素,再倒序推入栈中
解答
go
The text was updated successfully, but these errors were encountered: