【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/remove-outermost-parentheses/
题目描述
A valid parentheses string is either empty ("")
, "(" + A + ")"
, or A + B
, where A and B 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 into S = A+B
, with A and B nonempty valid parentheses strings.
Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k
, where P_i
are primitive valid parentheses strings.
Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.
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
题目大意
找出字符串中每对括号匹配的结果去除最外层的()
之后的结果之和。
解题方法
遍历
看到括号匹配,大家一般都想到用一个栈,其实不用栈也可以。
我们只需要一个变量count保存左括号数-右括号数即可。即遇到左括号则自增1,遇到右括号则自减1.当count为0的时候,说明在这一段中左括号和右括号相等,是个完美匹配的括号串了。
我们使用一个变量previ保存的是上一次括号完全匹配之后,下一个括号匹配开始位置。由于这个题需要让我们把最外层的括号去掉,所以,当count==0的时候,我们把结果增加的是[previ + 1, i),左闭右开区间。
Python代码如下:
class Solution(object):
def removeOuterParentheses(self, S):
"""
:type S: str
:rtype: str
"""
previ = 0
res = ""
count = 0
for i, s in enumerate(S):
if s == '(':
count += 1
else:
count -= 1
if count == 0:
res += S[previ + 1: i]
previ = i + 1
return res
日期
2019 年 4 月 7 日 —— 周赛bug了3次。。
【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)的更多相关文章
- 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_easy】1021. Remove Outermost Parentheses
problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完
- 【leetcode】1021. Remove Outermost Parentheses
题目如下: A valid parentheses string is either empty (""), "(" + A + ")", ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 1021. Remove Outermost Parentheses删除最外层的括号
网址:https://leetcode.com/problems/remove-outermost-parentheses/ 使用栈的思想,选择合适的判断时机 class Solution { pub ...
- LeetCode 1021. 删除最外层的括号(Remove Outermost Parentheses)
1021. 删除最外层的括号 1021. Remove Outermost Parentheses 题目描述 有效括号字符串为空 ("")."(" + A + ...
- LeetCode--To Lower Case && Remove Outermost Parentheses (Easy)
709. To Lower Case(Easy)# Implement function ToLowerCase() that has a string parameter str, and retu ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
随机推荐
- nginx_日志切割脚本
#!/bin/bash NGINX_LOG=/usr/loca/nginx/logs/access.log RE_LOG=/data/backup/`data +%Y%m%d` echo -e &qu ...
- Linux—crontab 定期执行程序的命令
crontab [ -u user ] { -l | -r | -e } 说明: crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表. -u user ...
- linux 网络配置管理
[1]网络配置基础 (1)用户既可以通过命令行的方式,也可以通过友好的图形界面,轻松完成网络配置. (2)实现Linux网络配置的惟一目标就是修改系统中众多的网络配置文件, 如/etc/interfa ...
- mongodb-to-mongodb
python3用于mongodb数据库之间倒数据,特别是分片和非分片之间. 本项目是一个集合一个集合的倒. 参考了logstash,对于只增不减而且不修改的数据的可以一直同步,阻塞同步,断点同步.改进 ...
- 8.深入TiDB:解析Hash Join实现原理
本文基于 TiDB release-5.1进行分析,需要用到 Go 1.16以后的版本 我的博客地址:https://www.luozhiyun.com/archives/631 所谓 Hash Jo ...
- 学习java的第十七天
一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...
- 巩固javaweb的第二十五天
常用的验证 1. 非空验证 // 验证是否是空 function isNull(str) { if(str.length==0) return true; else return false; } 2 ...
- E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing
解决办法:apt-get update或者apt-get cleanapt-get update 或者 apt-get update --fix-missing问题解析1 source本身的问题 根据 ...
- 从源码看Thread&ThreadLocal&ThreadLocalMap的关系与原理
1.三者的之间的关系 ThreadLocalMap是Thread类的成员变量threadLocals,一个线程拥有一个ThreadLocalMap,一个ThreadLocalMap可以有多个Threa ...
- Netty4.x 源码实战系列(一): 深入理解ServerBootstrap 与 Bootstrap (1)
从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念.传统的socket是基于s ...