栈的应用.

class Solution(object):def minAddToMakeValid(self, S):""":type S: str:rtype: int"""stack=[]for s in S:if s=='(':stack.append(s)else:if stack:if stack[-1]=='(':stack.pop()else:stack.append(s)else:stack.append(s)return len(stack)

这种方法是O(n)时间复杂度,O(n)空间复杂度,其实没有必要实现一个栈,因为右括号如果进入了栈就不可能再被消去了.所以就两个变量记录两边括号数就行了,空间复杂度为O(1):

class Solution(object):def minAddToMakeValid(self, S):""":type S: str:rtype: int"""left,right=0,0for s in S:if s=='(':left+=1else:if left>0:left-=1else:right+=1return left+right

转载于:https://www.cnblogs.com/zywscq/p/10519261.html

Leetcode 921. Minimum Add to Make Parentheses Valid相关推荐

  1. [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. LeetCode算法入门- Add Two Numbers-day3

    LeetCode算法入门- Add Two Numbers-day3 Add Two Numbers You are given two non-empty linked lists represen ...

  4. java两个二进制数求和,Leetcode 67:Add Binary(二进制求和)

    Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...

  5. 【打印代码+好好理解+子串问题】LeetCode 76. Minimum Window Substring

    LeetCode 76. Minimum Window Substring 字符串子串问题!!!理解这题真是花了很大的功夫!!! 参考链接:https://blog.csdn.net/weixin_4 ...

  6. 【DP】LeetCode 64. Minimum Path Sum

    LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...

  7. LeetCode 871. Minimum Number of Refueling Stops 最少加油次数

    LeetCode 871. Minimum Number of Refueling Stops 本题是LeetCode 871题,最少加油次数. 题目描述 A car travels from a s ...

  8. LeetCode: 871. Minimum Number of Refueling Stops

    LeetCode: 871. Minimum Number of Refueling Stops 题目描述 A car travels from a starting position to a de ...

  9. leetcode 310. Minimum Height Trees | 310. 最小高度树(图的邻接矩阵DFS / 拓扑排序)

    题目 https://leetcode.com/problems/minimum-height-trees/ 题解 方法1:图的邻接矩阵 DFS(超时) 我一想,这不就是个图嘛,于是随手敲出一个 DF ...

最新文章

  1. Python OpenCV GrabCut进行前景分割和提取
  2. 微信支付invalid total_fee 的报错
  3. Google Chrome 超详细使用教程
  4. python项目实战:20行代码画一朵好看又有趣的小花花
  5. VTK:Texture之TextureCutQuadric
  6. Benelux Algorithm Programming Contest 2016 Preliminary 题解
  7. 深圳部分写字楼租金暴跌,为何会出现这种情况?
  8. python车牌识别逆光怎么办代码_这摄像头除了能逆光识别车牌,还会跟人打招呼?...
  9. Linux/AIX上部署VNC Server
  10. GStreamer基础教程01 - Hello World
  11. python语言能做什么-Python是什么_python能做什么
  12. aspnet ajax 1.0中的according控件
  13. iOS安装CocoaPods详细过程
  14. java中this有什么作用_Java中this有什么用
  15. 从概念入手,了解AWS在Iaas、Paas,Saas层的服务
  16. 树莓派开发和车牌识别
  17. 使用 Packer 自动执行 VMware vsphere 模板
  18. 程序员为何很难与他人沟通
  19. 难度较大题型--LeetCode
  20. AD应用自定义BOM料单模板

热门文章

  1. python if条件思维导图_跟老齐学Python之从if开始语句的征程
  2. 编译linux内核分区,Ubuntu编译内核及grub的一些笔记
  3. 计算机扩招的学校,这些985/211学校今年继续扩招!又有一大波学校专业课有变!...
  4. Maven projects need to be imported反复弹出如何办?
  5. C#医疗挂号系统试题
  6. 日历记事本java代码_急需日历记事本JAVA源代码
  7. Nginx学习笔记---ngx_table_elt_t数据结构
  8. 《好未来编程题》求和
  9. win10+anaconda3安装tensorflow-gpu和pytorch-gpu版
  10. 机器学习实战(用Scikit-learn和TensorFlow进行机器学习)(五)