leetcode301
class Solution {
public List<String> removeInvalidParentheses(String s) {
List<String> ans = new ArrayList<>();
remove(s, ans, 0, 0, new char[]{'(', ')'});
return ans;
}
public void remove(String s, List<String> ans, int last_i, int last_j, char[] par) {
for (int stack = 0, i = last_i; i < s.length(); ++i) {
if (s.charAt(i) == par[0]) stack++;
if (s.charAt(i) == par[1]) stack--;
if (stack >= 0) continue;
for (int j = last_j; j <= i; ++j)
if (s.charAt(j) == par[1] && (j == last_j || s.charAt(j - 1) != par[1]))
remove(s.substring(0, j) + s.substring(j + 1, s.length()), ans, i, j, par);
return;
}
String reversed = new StringBuilder(s).reverse().toString();
if (par[0] == '(') // finished left to right
remove(reversed, ans, 0, 0, new char[]{')', '('});
else // finished right to left
ans.add(reversed);
}
}
leetcode301的更多相关文章
- [Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- LeetCode301. Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
随机推荐
- python类属性和类方法(类的结构、实例属性、静态方法)
类属性和类方法 目标 类的结构 类属性和实例属性 类方法和静态方法 01. 类的结构 1.1 术语 —— 实例 使用面相对象开发,第 1 步 是设计 类 使用 类名() 创建对象,创建对象 的动作有两 ...
- JS查看IOS手机的版本号
微信弹portal连接wifi的开发过程中,遇到了有些版本的ios系统在弹portal的浏览器(以下称小浏览器)中无法进行alert(),weixin://等等操作,只能使用window.locati ...
- BULK语句 将TXT数据塞入数据库表格
SET @iSQL=N'BULK INSERT [TEST].[dbo].[TEST_Interim]'+' FROM '+quotename(@fullFileName,'''')+' WITH ( ...
- Linux搭建SVN环境
1.安装SVN yun install -y subversion root权限安装 安装成功 验证 svnserve --version 创建SVN版本库 cd /home/kuma/下载 mkdi ...
- 编译安装和apt安装Nginx1.14.0
安装依赖 yum -y install gcc gcc-c++yum -y install zlib zlib-devel openssl openssl-devel pcre-devel 在Ubun ...
- Oracle的导入和导出
导出命令: EXP 用户名/密码@数据库名 BUFFER=64000 file=G:\dmp\full1.dmp OWNER=用户名 导入命令: IMP 用户名/密码@数据库名 BUFFER=64 ...
- IIS 集成模式 导致 AjaxPro 无法正常运行
web.config 配置如下: system.web/httphandlers <httpHandlers> <add verb="POST,GET" path ...
- PythonStudy——字典的操作 Dictionary operation
dic = {'a': 1, 'b': 2} print(dic) # 增: 字典名[key] = 值 => key已存在就是修改值,不存在就是新增值 dic['c'] = 3 print(di ...
- nginx添加一个站点
server { listen ; server_name demo.abc.com ; root /Users/pa200318/demo.cp.com/trunk; index index.php ...
- Django学习笔记之数据库-数据库与模型
MySQL数据库 在网站开发中,数据库是网站的重要组成部分.只有提供数据库,数据才能够动态的展示,而不是在网页中显示一个静态的页面.数据库有很多,比如有SQL Server.Oracle.Postgr ...