【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 ...
随机推荐
- (转)字符编码笔记:ASCII,Unicode 和 UTF-8
转:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 今天中午,我突然想搞清楚 Unicode 和 UTF-8 之 ...
- 麦子lavarel---16、日志
麦子lavarel---16.日志 一.总结 一句话总结: 一定要养成打印日志查看日志的好习惯,非常节约时间和便于查错 1.console.Log(“获取类别数据:"):console.Lo ...
- 洛谷P1378 油滴扩展(搜索)
洛谷P1378 油滴扩展 直接暴力搜索更新答案就可以了. 时间复杂度为 \(O(n!)\) . #include<stdio.h> #include<stdlib.h> #in ...
- 数据可视化-D3js-展示古地理图和古地理坐标反算^_^gplates古地理坐标反算接口
在线演示 <!DOCTYPE html> <html> <head> <link type="image/png" rel="i ...
- 由length、length()、size()引发的挖掘
在java中可能会经常用到这几个属性或者方法, 那么今天就来仔细研究一下 length——数组的属性: length()——String的方法: size()——集合的方法: 1.数组.length属 ...
- SqlServer 事物
Ø 事务 在数据库中有时候需要把多个步骤的指令当作一个整体来运行,这个整体要么全部成功,要么全部失败,这就需要用到事务. 1. 事务的特点 事务有若干条T-SQL指令组成,并且所有的指令昨晚一个整体提 ...
- Bootstrap,Bagging and Random Forest Algorithm
Bootstrap Method:在统计学中,Bootstrap从原始数据中抽取子集,然后分别求取各个子集的统计特征,最终将统计特征合并.例如求取某国人民的平均身高,不可能测量每一个人的身高,但却可以 ...
- linux常用命令介绍
参考博客: https://www.cnblogs.com/caozy/p/9261224.html 学前理论 linux主要特征 :一切且文件(目录.硬盘等都是文件):硬件都在/dev 目录,如硬盘 ...
- Python判断端口连通性
#!/usr/bin/env python # -*- coding: utf-8 -*- import socket,sys def MySQL_Connet(MySQL_ip): MySQL_so ...
- 2019 最新 Java 核心技术教程,都在这了!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 以下是Java技术栈微信公众号发布的所有关于 Java 的技术干货,会从以下几个方面汇总,本文会长期更新. Java 基础篇 ...