TreeviewCopyright © aleen42 all right reserved, powered by aleen42
1021. 删除最外层的括号
https://leetcode-cn.com/problems/remove-outermost-parentheses/
Java
/*
* @Author: Goog Tech
* @Date: 2020-08-09 08:17:15
* @LastEditTime: 2020-08-09 08:18:31
* @Description: https://leetcode-cn.com/problems/remove-outermost-parentheses/
* @FilePath: \leetcode-googtech\#1021. Remove Outermost Parentheses\Solution.java
*/
class Solution {
public String removeOuterParentheses(String S) {
StringBuffer sb = new StringBuffer();
// 将字符串转换为字符数组
char[] chars = S.toCharArray();
int level = 0;
// 遍历字符数组,将最内层括号添加到sb中
for(char c : chars) {
if( c == ')') --level;
if(level >= 1) sb.append(c);
if(c == '(') ++level;
}
// 将结果转化为字符串并返回
return sb.toString();
}
}
Python
'''
Author: Goog Tech
Date: 2020-08-09 08:17:22
LastEditTime: 2020-08-09 08:18:59
Description: https://leetcode-cn.com/problems/remove-outermost-parentheses/
FilePath: \leetcode-googtech\#1021. Remove Outermost Parentheses\Solution.py
'''
class Solution(object):
def removeOuterParentheses(self, S):
"""
:type S: str
:rtype: str
"""
level = 0
result = []
# 遍历字符串元素,并将最内层括号添加到result列表中
for c in S:
if c == ')': level -= 1
if level >= 1: result.append(c)
if c == '(': level += 1
# 将结果列表转换为字符串并返回
return ''.join(result)