【leetcode】921. Minimum Add to Make Parentheses Valid
题目如下:

解题思路:上周都在忙着参加CTF,没时间做题,今天来更新一下博客吧。括号问题在leetcode中出现了很多,本题的解题思路和以前的括号问题一样,使用栈。遍历Input,如果是'('直接入栈;如果是')'则判断栈顶是否为'(',如果栈顶是'(',栈顶元素出栈,否则要加括号的count加1。遍历完成后,count + 栈里剩余的 '(' 的数量就是结果。
代码如下:
class Solution(object):
def minAddToMakeValid(self, S):
"""
:type S: str
:rtype: int
"""
stack = []
res = 0
for i in S:
if i == '(':
stack.append(i)
else:
if len(stack) > 0 and stack[-1] == '(':
stack.pop(-1)
else:
res += 1
res += len(stack)
return res
【leetcode】921. Minimum Add to Make Parentheses Valid的更多相关文章
- 【LeetCode】921. Minimum Add to Make Parentheses Valid 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- 921. Minimum Add to Make Parentheses Valid
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48
921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【leetcode】Find Minimum in Rotated Sorted Array I&&II
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...
随机推荐
- Linux中的uniq命令(去掉重复项,输出重复项)
ls /bin /usr/bin | sort | uniq | less 上面这条命令的实际效果是: 获得 ls /bin /usr/bin 的 output 将上述 output 进行 sort ...
- poj 2104: K-th Number 【主席树】
题目链接 学习了一下主席树,感觉具体算法思路不大好讲.. 大概是先建个空线段树,然后类似于递推,每一个都在前一个“历史版本”的基础上建立一个新的“历史版本”,每个历史版本只需占用树高个空间(好神奇!) ...
- Angular JS - 2 - angularjs helloworld
材料下载 https://github.com/liuch0228/AngularJS-learn.git 1.使用原生jquery实现 实现输入框内容 在页面上跟随输入值动态更新 项目路径 < ...
- spring-boot整合Mybatis案例(注解方式)
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...
- mysql审计插件
Audit Plugin安装使用 原文: https://www.cnblogs.com/waynechou/p/mysql_audit.html#_label0 #有卸载方法 下载地址: htt ...
- python中join()函数的用法
join()函数 语法: 'sep'.join(s) 参数说明 sep:分隔符.可以为空 s:要连接的元素序列.字符串.元组.字典 上面的语法即:以sep作为分隔符,将s所有的元素合并成一个新的字符 ...
- 边缘节点 如何判断CDN的预热任务是否执行完成刷新 路由追踪 近期最少使用算法
阿里云内容分发网络(Content Delivery Network,简称CDN)是建立并覆盖在承载网之上,由分布在不同区域的边缘节点服务器群组成的分布式网络.阿里云CDN分担源站压力,避免网络拥塞, ...
- CSS-inline-block 间隙
间隙产生的原因是`inline-block`对外是`inline`,对内是`block`.`inline`会将连续的空白符解析为一个空格(如:下面示例的两个`li`之间的后面的换行空格).取消间隙的方 ...
- Openstack Restful API 开发框架 Paste + PasteDeploy + Routes + WebOb
目录 目录 Paste PasteDeploy Routes WebOb 简介 WSGI入口 Paste和PasteDeploy 配置文件 pasteini 中间件的实现 Routes WebOb 参 ...
- Html5 学习笔记 【PC固定布局】 实战7 机票预订页面
最终实际效果: HTML代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta char ...