FB面经 Prepare: Make Parentheses valid
给一组括号,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的更多相关文章
- [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) 48
921. 使括号有效的最少添加 921. Minimum Add to Make Parentheses Valid 题目描述 给定一个由 '(' 和 ')' 括号组成的字符串 S,我们需要添加最少的 ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- [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-Minimum Add to Make Parentheses Valid]
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- 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. ...
- FB面经 Prepare: All Palindromic Substrings
Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...
- FB面经 Prepare: Task Schedule
tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...
随机推荐
- TopCoder SRM704 Div1 800 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM704-800.html 题解 考虑构造一个 $n = 20$ 的图. 先把所有 $i$ 都连向 $i-1$ ...
- impala
impala 1.impala是什么: impala是基于hive的大数据实时分析查询引擎,直接使用Hive的元数据库Metadata,意味着impala元数据都存储在Hive的metastore中. ...
- (三)ajax请求不同源之websocket跨域
WebSocket是一种通信协议,使用ws://(非加密)和wss://(加密)作为协议前缀.该协议不实行同源政策,只要服务器支持,就可以通过它进行跨源通信. 一.WebSocket目标 在一个单独的 ...
- HDU3072 Intelligence System
题目传送门 有个中文版的题面...和原题稍有不同 /* Description “这一切都是命运石之门的选择.” 试图研制时间机器的机关SERN截获了中二科学家伦太郎发往过去的一条短信,并由此得知了伦 ...
- 2017-11-8—自动控制原理在软硬件方面上的应用和体现
这个话题讲起来是非常大的,先贴一下百度百科的定义: 自动控制(automatic control)是指在没有人直接参与的情况下,利用外加的设备或装置,使机器.设备或生产过程的某个工作状态或参数自动地按 ...
- Linux 上安装JDK
JDK下载 下载 JDK Linux 版本(注意看自己安装 Linux 系统的位数,本人的是64位) 1.oracle 官网下载地址:jdk-7u80-linux-x64.gz(可能需要注册下载,嫌麻 ...
- hadoop解决Could not locate executable null\bin\winutils.exe in the Hadoop binaries.问题
先看下自己的JAVA_HOME里面有没有空格目录,如果有的话,先把JAVA_HOME换个没空格的位置. 在windows系统本地运行spark的wordcount程序,会出现一个异常,但不影响现有程序 ...
- JavaScript学习day1
JavaScript 特点: javascript 是一种脚本语言,它的解释器被称为javascript引擎,JavaScript被发明用于在HTML网页上使用,给HTML 网页增加动态功能 由于ja ...
- vue菜鸟从业记:公司项目里如何进行前后端接口联调
最近我的朋友王小闰进入一家新的公司,正好公司项目采用的是前后端分离架构,技术栈是王小闰非常熟悉的vue全家桶,后端用的是Java语言. 在前后端开发人员碰面之后,协商确定好了前端需要的数据接口(扯那么 ...
- git开发过程中的使用流程
001.创建仓库 002.新建项目 003.初始化仓库 这一步不需要做 git init : 文件夹中会多出一个隐藏的.git文件 004.克隆项目 git clone <项目地址> 0 ...