FB面经 Prepare: Make Parentheses valid
2024-10-16 08:59:46
给一组括号,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);
} }
最新文章
- js中文乱码怎么解决【转】
- C# 必看书籍
- Cheatsheet: 2015 06.01 ~ 06.30
- 自己动手搞定支付宝手机网站支付接口 FOR ECShop
- zabbix再爆高危SQL注入漏洞,可获系统权限
- Dynamic Programming - Part1
- LeetCode: Sqrt
- WdatePicker 控制选择范围
- ASP.NET通用权限验证组件实现
- OC中另外的一个常用技术:通知(Notification)
- C# Best Practices - Creating Good Properties
- 动态规划3-------poj1050
- SQL清除所有数据库日志脚本
- ArcGIS API for JavaScript 4.2学习笔记[15] 弹窗内容的格式与自定义格式
- Gradle 1.12用户指南翻译——第三十九章. IDEA 插件
- 在MFC中通过访问IP地址下载文件到本地
- chmod命令-权限
- 微软刚发布的区块链去中心化身份识别系统DID
- CentOS 7 安装telnet服务
- Angular2 File Upload
热门文章
- + CategoryInfo : NotSpecified: (:) [], PSSecurityException + FullyQualifiedErrorId : RuntimeException
- Excel 转为 MySQL 语句
- 如何写一个Js上传图片插件。
- .Net Core WebAPI 搭建
- django——模型层之多表操作
- 探索JavaScript中Null和Undefined的深渊
- 02-Python入门学习-变量
- Lesnoe Ozero 2016. BSUIR Open 2016 Finals
- 那些年我们跳过的 IE坑
- 安装python--环境配置