给一组括号,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. Android+openCV 动态人脸检测

    动态人脸检测前提是需要打开摄像头. 网上看了很多教程,我知道的有两种方式打开摄像头: JavaCameraView mCameraView = new JavaCameraView(this, -1) ...

  2. Pop Star 1.2.5

    原文链接https://www.cnblogs.com/zhouzhendong/p/Pop-Star.html 是VB写的. 年代久远,代码太丑,原码不公开. 下载链接 仅支持Windows,需要解 ...

  3. chattr和lsattr的基本用法

    lsattr filename  查看一个文件的属性 chattr filename  改变一个文件的属性 主要用途:实现文件的原有内容不允许改变,但可以增加新行,则需通过改变文件的属性来实现. ch ...

  4. JAVA基础复习与总结<二>构造方法_static关键字_final关键字

    构造方法详解 构造器也叫做构造方法(constructor),用于对象的初始化. class Person2 { String name; int age; public Person2(String ...

  5. fflush()函数:更新缓冲区

    fflush()的作用是用来刷新缓冲区: fflush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃:stdin是standard input的缩写,即标准输入,一般是指键盘:标准输入缓冲 ...

  6. Django路由机制

    Django的URL路由配置: Settings.py文件中的ROOT_URLCONF变量指定全局路由文件名称 ROOT_URLCONF = '工程名称.urls'   #默认对应工程目录下urls. ...

  7. python田忌赛马

    一,简介 田忌赛马的故事大家都知道我就不展开说了,田忌能用同全面被碾压的马赢了齐威王(公子),我觉得这是十分具有智慧的.但是,如果说这里的条件改为:1,田忌的马比齐威王同等次的马弱一点但是比齐威王下一 ...

  8. 潭州课堂25班:Ph201805201 tornado 项目 第七课 界面美化和静态文件处理(课堂笔记)

    tornado 相关说明 使用  Bootstrap 前端框架 可以在 bootCDN 这里找 jquery ,poppe.js 文件 美化项目 twittel 的开源项目, 在 static 目录下 ...

  9. priority_queue和sort应用

    #include"iostream" #include"String" #include"stdio.h" #include "s ...

  10. ICL2019E

    https://www.codechef.com/ICL2019/problems/ICL1906  两个整数,[0,1e5]操作1是让两个数同时减1(只有都大于0的时候才可以用)操作2可以让一个数乘 ...