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 ...
随机推荐
- Imcash平台测评报告
ImCash是由全球知名量子基金(QuantumFund)与美国好事达保险公司 (ALL ) 联合投资美国区块链金融资本(BFC)打造全球首款量子基金数字资产服务平台 . ImCash作为全球首款量子 ...
- python 爬虫与数据可视化--爬虫基础知识
一.python中的模块 模块的安装:pip install 模块名 导入模块与函数:import requests . from pymongo import MongoClient json模块的 ...
- es6中的...三个点
...是es6中新添加的操作符,可以称为spread或rest 定义一个数组 let name=['小红','小明','小白']; 我们在控制台输出 console.log(name); 结果: ...
- CentOS6.5安装图形用户界面
CentOS 6.5 安装图形界面 安装的时候没有安装图像界面.安装步骤如下: 1.yum -y groupinstall Desktop 2.yum -y groupinstall "X ...
- python 3 实现文件下载的方法总结
新学的python,兴奋不已,于是网上各种资源各种爬,发现对于同样文件下载,各个下载方法的速度是不一样的(目前就知道两种方法下载#尴尬) 下面分别看下吧: 下载同样的图片,方法二的速度是方法一的二倍 ...
- 数字图像特征提取之HOG特征
1.灰度化:(以便可以使用sobel等算子计算梯度)2.gamma校正: (降低光照影响)3.求每个像素的梯度和方向: (利用任意一种梯度算子,例如:sobel,laplacian等,对该patch进 ...
- jupyter notebook 远程访问
https://www.youtube.com/watch?v=LpQl0yeZzCU 在服务器端执行: jupyter notebook --ip 服务器的Ip地址 --allow-root --n ...
- 创建phpinfo(PHP探针)查看自己服务器空间php详细信息
创建phpinfo(PHP探针)查看自己服务器空间php详细信息 <?phpphpinfo();?> 保存,然后更改文件名为phpinfo.php 放到你域名根目录,然后访问:http:/ ...
- 2018-2019-20175307实验一《Java开发环境的熟悉》实验报告
实验内容与结果 一.Java开发环境的熟悉-1 1.实验要求: 0 参考实验要求 1 建立"自己学号exp1"的目录 2 在"自己学号exp1"目录下建立src ...
- 什么是Hash?Hash有哪些特性?
Hash 把任意长度的输入通过散列算法变换成固定长度的输出 Hash的特性: 输入域无穷,输出域有限.例如:有无穷多个(在工程中可以具体到多少个,例如1000)输入参数经过hash函数映射后得到有限的 ...