LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48
921. 使括号有效的最少添加
921. Minimum Add to Make Parentheses Valid
题目描述
给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的括号( '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效。
从形式上讲,只有满足下面几点之一,括号字符串才是有效的:
- 它是一个空字符串,或者
- 它可以被写成 AB (A 与 B 连接),其中 A 和 B 都是有效字符串,或者
- 它可以被写作 (A),其中 A 是有效字符串。
给定一个括号字符串,返回为使结果字符串有效而必须添加的最少括号数。
每日一算法2019/6/20Day 48LeetCode921. Minimum Add to Make Parentheses Valid
示例 1:
输出:1
示例 2:
输出:3
示例 3:
输出:0
示例 4:
输出:4
提示:
- S.length <= 1000。
- S 只包含 '(' 和 ')' 字符。
Java 实现
import java.util.ArrayDeque;
import java.util.Deque;
class Solution {
public int minAddToMakeValid(String S) {
Deque<Character> deque = new ArrayDeque<>();
int count = 0;
for (char c : S.toCharArray()) {
if (c == '(') {
deque.push(c);
} else if (deque.isEmpty()) {
count++;
} else {
deque.pop();
}
}
return deque.size() + count;
}
}
参考资料
- https://leetcode-cn.com/problems/minimum-add-to-make-parentheses-valid/
- https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48的更多相关文章
- [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- leetcode 921. 使括号有效的最少添加
问题描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的括号( '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效. 从形式上讲,只有满足下面几点之一,括号字符 ...
- leetcode 921. 使括号有效的最少添加(Python)
class Solution: def minAddToMakeValid(self, S): """ :type S: str :rtype: int "&q ...
- [LeedCode]921. 使括号有效的最少添加
题目描述: 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的括号( '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效. 从形式上讲,只有满足下面几点之一,括号字 ...
- [LeetCode] 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- leetcode921. 使括号有效的最少添加
题目描述: 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的括号( '(' 或是 ')',可以在任何位置),以使得到的括号字符串有效. 从形式上讲,只有满足下面几点之一,括号字 ...
- 【leetcode】921. Minimum Add to Make Parentheses Valid
题目如下: 解题思路:上周都在忙着参加CTF,没时间做题,今天来更新一下博客吧.括号问题在leetcode中出现了很多,本题的解题思路和以前的括号问题一样,使用栈.遍历Input,如果是'('直接入栈 ...
- 921. Minimum Add to Make Parentheses Valid
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
随机推荐
- BigDecimal保留小数
public class test1_format { public static void main(String[] args) { BigDecimal decimal = new BigDec ...
- matlab基础向9:动画
先定义坐标变量,确定范围,画出起始静态图,存进图形变量h.通过对坐标的变化,把新的坐标放进图形变量h,再实时刷新看起来就是动态的. 1.动态的sin曲线 X = -2*pi:0.1:2*pi; Y = ...
- MATLAB画图笔记
plot函数 plot(x,y)默认格式: 若x,y是向量,则它们必须具有相同的长度.函数将以x为横轴,绘制y. 若x,y都是矩阵,则它们必须具有相同的尺寸,plot函数将针对x的各列绘制y的每列.更 ...
- BZOJ 3561: DZY Loves Math VI 莫比乌斯反演+复杂度分析
推到了一个推不下去的形式,然后就不会了 ~ 看题解后傻了:我推的是对的,推不下去是因为不需要再推了. 复杂度看似很大,但其实是均摊 $O(n)$ 的,看来分析复杂度也是一个能力啊 ~ code: #i ...
- 单片机模块化程序: 单片机AT指令配置模块程序模板(非阻塞版)
拷贝这两个文件到自己的工程 测试1://单片机发送AT+RST\r\n 如果单片机串口接收到OK 或者ready 执行下一条 测试视频: https://qqqqqbucket.oss-cn-bei ...
- CSS注册页面案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WSL调用Windows下的ParaView对OpenFOAM进行后处理
OpenFOAM可以在windows下的Linux子系统(WSL)安装,但是很麻烦的一个问题是WSL没有图形化的界面,只有一个控制台窗口,在后处理的过程中我们就没有办法直接调用paraFoam命令启动 ...
- Python全栈工程师(Python3 所有基础内容 0-0)
转发:https://www.cnblogs.com/ParisGabriel/p/9388030.html statements 语句print 输出quit() 退出exit() 退出ct ...
- Spring不能直接@autowired注入Static变量
一.业务场景 spring框架应用中有些静态方法需要依赖被容器管理的类,就像这样: @Component public class Test { @Autowired private static U ...
- nodejs异常处理过程/获取nodejs异常类型/写一个eggjs异常处理中间件
前言 今天想写一下eggjs的自定义异常处理中间件,在写的时候遇到了问题,这个错误我捕获不到类型?? 处理过程,不喜欢看过程的朋友请直接看解决方法和总结 看一下是什么: 抛出的异常是检验失败异常Val ...