博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode刷题:301. Remove Invalid Parentheses (DFS算法设计和BFS算法设计)
阅读量:4040 次
发布时间:2019-05-24

本文共 3654 字,大约阅读时间需要 12 分钟。

LeetCode刷题:301. Remove Invalid Parentheses

原题链接:

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.

Note: The input string may contain letters other than the parentheses ( and ).

Example 1:

Input: "()())()"

Output: ["()()()", "(())()"]
Example 2:

Input: "(a)())()"

Output: ["(a)()()", "(a())()"]
Example 3:

Input: ")("

Output: [""]

删除最小数量的无效括号,使得输入的字符串有效,返回所有可能的结果。

说明: 输入可能包含了除 ( 和 ) 以外的字符。


算法设计

DFS算法设计

/*DFS*/	public List
removeInvalidParentheses(String s) { List
ans = new ArrayList<>(); if (s == null) return ans; int removeLeft = 0, removeRight = 0; for (char ch : s.toCharArray()) { if (ch == '(') removeLeft++; if (ch == ')') { if (removeLeft > 0) removeLeft--; else removeRight++; } } DFS(s, 0, removeLeft, removeRight, ans); return ans; //O(string length) + O() } private void DFS(String s, int lastRemoveIndex, int removeLeft, int removeRight, List
ans) { if (removeLeft == 0 && removeRight == 0) { //O(N) if (isValid(s)) ans.add(s); return; } int idx = lastRemoveIndex; if (removeRight > 0) { while (idx < s.length()) { if (s.charAt(idx) == ')') { DFS(s.substring(0, idx) + s.substring(idx + 1, s.length()), idx, removeLeft, removeRight - 1, ans); while (idx < s.length() && s.charAt(idx) == ')') idx++; } else { idx++; } } return; } if (removeLeft > 0) { while (idx < s.length()) { if (s.charAt(idx) == '(') { DFS(s.substring(0, idx) + s.substring(idx + 1, s.length()), idx, removeLeft - 1, removeRight, ans); while (idx < s.length() && s.charAt(idx) == '(') idx++; } else { idx++; } } } } private boolean isValid(String s) { if (s == null) return false; int cnt = 0; for (char ch : s.toCharArray()) { if (ch == '(') cnt++; if (ch == ')') cnt--; if (cnt < 0) return false; } return cnt == 0; }

 

BFS算法设计

/*	 * BFS	 * */	public List
removeInvalidParentheses(String s) { List
res = new ArrayList<>(); // sanity check if (s == null) return res; Set
visited = new HashSet<>(); Queue
queue = new LinkedList<>(); // initialize queue.add(s); visited.add(s); boolean found = false; while (!queue.isEmpty()) { s = queue.poll(); if (isValid(s)) { // found an answer, add to the result res.add(s); found = true; } if (found) continue; // generate all possible states for (int i = 0; i < s.length(); i++) { // we only try to remove left or right paren if (s.charAt(i) != '(' && s.charAt(i) != ')') continue; String t = s.substring(0, i) + s.substring(i + 1); if (!visited.contains(t)) { // for each state, if it's not visited, add it to the queue queue.add(t); visited.add(t); } } } return res; } // helper function checks if string s contains valid parantheses boolean isValid(String s) { int count = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '(') count++; if (c == ')' && count-- == 0) return false; } return count == 0; }

 

 

转载地址:http://vntdi.baihongyu.com/

你可能感兴趣的文章
”舍得“大法:把自己的优点当缺点倒出去
查看>>
[今日关注]鼓吹“互联网泡沫,到底为了什么”
查看>>
[互联网学习]如何提高网站的GooglePR值
查看>>
[关注大学生]求职不可不知——怎样的大学生不受欢迎
查看>>
[关注大学生]读“贫困大学生的自白”
查看>>
[互联网关注]李开复教大学生回答如何学好编程
查看>>
[关注大学生]李开复给中国计算机系大学生的7点建议
查看>>
[关注大学生]大学毕业生择业:是当"鸡头"还是"凤尾"?
查看>>
[茶余饭后]10大毕业生必听得歌曲
查看>>
gdb调试命令的三种调试方式和简单命令介绍
查看>>
C++程序员的几种境界
查看>>
VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法
查看>>
VUE-Vue.js之$refs,父组件访问、修改子组件中 的数据
查看>>
Vue-子组件改变父级组件的信息
查看>>
Python自动化之pytest常用插件
查看>>
Python自动化之pytest框架使用详解
查看>>
【正则表达式】以个人的理解帮助大家认识正则表达式
查看>>
性能调优之iostat命令详解
查看>>
性能调优之iftop命令详解
查看>>
非关系型数据库(nosql)介绍
查看>>