【leetcode】1021. Remove Outermost Parentheses
题目如下:
A valid parentheses string is either empty
("")
,"(" + A + ")"
, orA + B
, whereA
andB
are valid parentheses strings, and+
represents string concatenation. For example,""
,"()"
,"(())()"
, and"(()(()))"
are all valid parentheses strings.A valid parentheses string
S
is primitive if it is nonempty, and there does not exist a way to split it intoS = A+B
, withA
andB
nonempty valid parentheses strings.Given a valid parentheses string
S
, consider its primitive decomposition:S = P_1 + P_2 + ... + P_k
, whereP_i
are primitive valid parentheses strings.Return
S
after removing the outermost parentheses of every primitive string in the primitive decomposition ofS
.Example 1:
Input: "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".Example 2:
Input: "(()())(())(()(()))"
Output: "()()()()(())"
Explanation:
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".Example 3:
Input: "()()"
Output: ""
Explanation:
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".Note:
S.length <= 10000
S[i]
is"("
or")"
S
is a valid parentheses string
解题思路:括号配对的题目在leetcode出现了很多次了,从左往右遍历数组,分别记录左括号和右括号出现的次数,当两者相等的时候,即为一组括号。
代码如下:
class Solution(object):
def removeOuterParentheses(self, S):
"""
:type S: str
:rtype: str
"""
left = 0
right = 0
res = ''
tmp = ''
for i in S:
tmp += i
if i == '(':
left += 1
else:
right += 1
if left == right:
res += tmp[1:-1]
tmp = ''
left = 0
right = 0
return res
【leetcode】1021. Remove Outermost Parentheses的更多相关文章
- 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
- 【Leetcode_easy】1021. Remove Outermost Parentheses
problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完
- 【leetcode】301. Remove Invalid Parentheses
题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【leetcode】1021. Best Sightseeing Pair
题目如下: Given an array A of positive integers, A[i]represents the value of the i-th sightseeing spot, ...
- Leetcode 1021. Remove Outermost Parentheses
括号匹配想到用栈来做: class Solution: def removeOuterParentheses(self, S: str) -> str: size=len(S) if size= ...
- LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号
https://leetcode-cn.com/problems/remove-outermost-parentheses/ Java Solution class Solution { public ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
随机推荐
- 01.cordova 安装及项目创建
1.安装cordova cnpm install -g cordova@6.0.0 这里用6.0.0版本,默认下载最新版本,创建项目时候会报错 2.创建项目 cordova create myap ...
- jQuery FileUpload doesn't trigger 'done'
https://stackoverflow.com/questions/14674999/jquery-fileupload-doesnt-trigger-done If your server ...
- Postman初接触
https://www.getpostman.com/docs/postman/launching_postman/installation_and_updates
- vue构造器注册UI组件
import ConfirmComponent from '../../components/confirm/index' import { mergeOptions } from '../plugi ...
- [Linux] 017 网络命令与挂载命令
1. 网络命令:write 命令名称:write 命令所在路径:/usr/bin/write 执行权限:所有用户 语法:write [用户名] 功能描述:给用户发信息,以 Ctrl-d 保存结束 范例 ...
- PHP学习 fwrite:Warning: fwrite(): supplied argument is not avalid stream resource in
使用fwrite报错:Warning: fwrite(): supplied argument is not avalid stream resource in 解决方法:文件权限的问题,文件需要77 ...
- Java-多线程第一篇多线程相关认识(1)
1.单线程进程 如果程序执行某行代码时遇到了阻塞,则程序将会停滞在该处. 2.进程代表着一个程序,程序是静态的,进程是动态的程序. 进程是系统进行资源分配和调度的一个独立单位.关于进程有如下3个特征: ...
- [BZOJ4151]The Cave
Solution: 假设现在在点1,有许多形如 (x, y, z) 的限制条件,那么对于一组限制,必须先走到 x, y 的 \(\frac{z-dis(x, y)}{2}\) 级祖先,叫这些点为限 ...
- python的小介绍
Python简介 龟叔 优美.清晰.简单 主要应用领域: 云计算 WEB开发 科学技术.人工智能 系统运维 爬虫 金融量化分析 图形GUI 游戏 Python发展史 1989年,Guido开始写Pyt ...
- tomcat内存使用情况
预发布阿里云服务器的容器 tomcat会自己无缘无故重启,故引出一些查看tomcat内存使用情况观察的细枝末节: 1️⃣当前端口号进程信息和GC使用情况(1)显示端口的PID:lsof -i:端口示例 ...