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 ...
随机推荐
- 最短路,dijstra算法
#include<iostream> #include<stdio.h> #include<math.h> #include<vector> using ...
- Markup解析XML——文档,说明
链接:http://pan.baidu.com/s/1slMwEc9 密码:slz7 上面是网盘的地址,因为来源已经找不到了,在这里给这个作者说声谢谢. 轻量级的XML解析器使用比较简单,下载Mark ...
- 剑指Offer 50. 数组中重复的数字 (数组)
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- [转] C++ 和 python之间的互相调用
转载自:https://www.cnblogs.com/apexchu/p/5015961.html 一.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不 ...
- 2.常用adb命令的使用
使用电脑连接手机,查看手机的唯一编号,如果是模拟器,就是显示地址和端口号: adb devices 使用adb安装app应用: adb install apk路径和包名 -r 允许覆盖安装 -s 将a ...
- gitlab 同步小脚本
gitlab 是公司中的代码仓库,如何保证两台机器同步呢 公司中使用的是docker那么久使用docker进行演示了也方便以后的工作查找资料 附:脚本 #!/bin/bash docker stop ...
- Angular4.0引入第三方框架,eg: bootstrap、jquery
最近学习angular4.0,在练习是需要使用jquery和bootstrap.但是查阅了,大多数都是angular2的方法,或者是angular4.0的方法但是不准确.花了一些时间终于捣腾出来了,把 ...
- 转载:python list和set的性能比较+两者转换
两者性能比较(转自http://www.linuxidc.com/Linux/2012-07/66404.htm) 本来是知道在Python中使用Set是比较高效,但是没想到竟然有这么大的差距: ~$ ...
- PythonStudy——列表类型 List type
# 1.定义 ls = [3, 1, 2] # 语法糖 | 笑笑语法 print(ls) ls = list([3, 1, 2]) # 本质 print(ls) # 嵌套 ls = [3, 1, [3 ...
- Nginx自动安装脚本
添加一个install_nginx.sh脚本 版本一:(以下脚本为在线自动化安装) #!/bin/bash mkdir /soft cd /soft wget -c http://nginx.org/ ...