给一组括号,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);
} }

最新文章

  1. js中文乱码怎么解决【转】
  2. C# 必看书籍
  3. Cheatsheet: 2015 06.01 ~ 06.30
  4. 自己动手搞定支付宝手机网站支付接口 FOR ECShop
  5. zabbix再爆高危SQL注入漏洞,可获系统权限
  6. Dynamic Programming - Part1
  7. LeetCode: Sqrt
  8. WdatePicker 控制选择范围
  9. ASP.NET通用权限验证组件实现
  10. OC中另外的一个常用技术:通知(Notification)
  11. C# Best Practices - Creating Good Properties
  12. 动态规划3-------poj1050
  13. SQL清除所有数据库日志脚本
  14. ArcGIS API for JavaScript 4.2学习笔记[15] 弹窗内容的格式与自定义格式
  15. Gradle 1.12用户指南翻译——第三十九章. IDEA 插件
  16. 在MFC中通过访问IP地址下载文件到本地
  17. chmod命令-权限
  18. 微软刚发布的区块链去中心化身份识别系统DID
  19. CentOS 7 安装telnet服务
  20. Angular2 File Upload

热门文章

  1. + CategoryInfo : NotSpecified: (:) [], PSSecurityException + FullyQualifiedErrorId : RuntimeException
  2. Excel 转为 MySQL 语句
  3. 如何写一个Js上传图片插件。
  4. .Net Core WebAPI 搭建
  5. django——模型层之多表操作
  6. 探索JavaScript中Null和Undefined的深渊
  7. 02-Python入门学习-变量
  8. Lesnoe Ozero 2016. BSUIR Open 2016 Finals
  9. 那些年我们跳过的 IE坑
  10. 安装python--环境配置