给一组括号,remove最少的括号使得它valid

从左从右各scan一次

 package fb;

 public class removeParen {

     public static String fix(String str) {
StringBuffer res = new StringBuffer(str);
int l = 0, r = 0;
int i = 0;
while (i < res.length()) {
if (res.charAt(i) == '(') l++;
else {
if (l <= r) {
res.deleteCharAt(i);
i--;
}
else {
r++;
}
}
i++;
} l = 0;
r = 0;
i = res.length()-1;
while (i >= 0) {
if (res.charAt(i) == ')') r++;
else {
if (l >= r) {
res.deleteCharAt(i);
}
else {
l++;
}
}
i--;
} return res.toString();
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String res = fix(")((())(");
System.out.println(res);
} }

FB面经 Prepare: Make Parentheses valid的更多相关文章

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

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  2. LeetCode 921. 使括号有效的最少添加(Minimum Add to Make Parentheses Valid) 48

    921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...

  3. 72. Generate Parentheses && Valid Parentheses

    Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

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

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  5. [leetcode-921-Minimum Add to Make Parentheses Valid]

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  6. 921. Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  7. 【LeetCode】921. Minimum Add to Make Parentheses Valid 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  8. FB面经 Prepare: All Palindromic Substrings

    Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...

  9. FB面经 Prepare: Task Schedule

    tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...

随机推荐

  1. A tuple is defined as a function

    In James Munkres "Topology", the concept for a tuple, which can be \(m\)-tuple, \(\omega\) ...

  2. 模块度Q

    模块度:也称模块化度量值,是目前常用的一种衡量网络社区结构强度的方法. 常用语衡量一个社区划分结果的优劣:一个理想化的社区划分应该对应着社区内部节点间相似度尽可能的高,同时社区外部节点间的相异度尽可能 ...

  3. cmake教程

    1 教程 cmake界的hello world[2] 进阶的入门教程参考[3] 2 引用 [1] cmake官网 [2] 在 linux 下使用 CMake 构建应用程序 [3] Valgrind官网

  4. 20172328 2018-2019《Java软件结构与数据结构》第五周学习总结

    20172328 2018-2019<Java软件结构与数据结构>第五周学习总结 概述 Generalization 本周学习了第九章:排序与查找,主要包括线性查找和二分查找算法和几种排序 ...

  5. C 小白的 thrift 环境搭建

    公司有个通讯 是用的 thrift ,thrift 是个什么都东西,可以类比 webservice 吧,比 webservice 高效些,不管是啥,搞他! 先在 mac 上搞本地开发环境 网上一搜 貌 ...

  6. CSS(四)

    css元素溢出 当子元素的尺寸超过父元素的尺寸时,需要设置父元素显示溢出的子元素的方式,设置的方法是通过overflow属性来设置. overflow的设置项: 1.visible 默认值.内容不会被 ...

  7. Web 录音

    所需文件下载地址 链接:https://pan.baidu.com/s/1Dzbv8gPUZJ3T8Fe02hOJvg 提取码:zbgt py文件 from flask import Flask, j ...

  8. css 定位布局

    文档流: 文档流,是指盒子按照html标签编写的顺序依次从上到下,从左到右排列.块元素占一行,行内元素在一行之内从左到在排列,先写的先排列,后写的排在后面,每个盒子都占据自己的位置. 关于定位: 可以 ...

  9. MFC版链表实现稀疏多项式相加减

    链表实现多项式运算(加减)MFC可视化版 题目 设计一个一元稀疏多项式简单计算器. 基本要求 (1)输入并建立两个多项式: (2)多项式a与b相加,建立和多项式c: (3)多项式a与b相减,建立差多项 ...

  10. (62)Wangdao.com第十天_JavaScript 变量的作用域

    在 js 中有两种作用域:全局作用域,局部作用域. 全局作用域 直接写在 <script> 标签中的变量和方法. 在网页打开时创建,在网页关闭时销毁. 全局作用域有一个全局对象 windo ...