LeetCode 301: Remove Invalid Parenthesis
2017-09-04 07:49
435 查看
Note:
It could be other chars in the String. Do not count as invalid parenthesis.
Second try:
1. Count j until i. not exceed i.
2. Return after remove char since it will duplicate.
It could be other chars in the String. Do not count as invalid parenthesis.
Second try:
1. Count j until i. not exceed i.
2. Return after remove char since it will duplicate.
class Solution { public List<String> removeInvalidParentheses(String s) { List<String> result = new ArrayList<>(); remove(s, result, 0, 0, new char[]{'(', ')'}); return result; } private void remove(String input, List<String> result, int lastI, int lastJ, char[] pattern) { for (int count = 0, i = lastI; i < input.length(); i++) { if (input.charAt(i) == pattern[0]) { count++; } if (input.charAt(i) == pattern[1]) { count--; } if (count >= 0) { continue; } for (int j = lastJ; j <= i; j++) { if (input.charAt(j) == pattern[1] && (j == lastJ || input.charAt(j - 1) != pattern[1])) { remove(input.substring(0, j) + input.substring(j + 1), result, i, j, pattern); } } return; } String reversed = new StringBuilder(input).reverse().toString(); if (pattern[0] == '(') { remove(reversed, result, 0, 0, new char[]{')', '('}); } else { result.add(reversed); } } }
相关文章推荐
- LeetCode 301 Remove Invalid Parentheses (BFS + 剪枝)
- leetcode:bfs: Remove Invalid Parentheses(301)
- leetcode 301 : Remove Invalid Parentheses
- Leetcode 301 Remove Invalid Parentheses
- [leetcode] Remove Invalid Parentheses
- Facebook面试题 Remove unclosed parenthesis
- [LeetCode][JavaScript]Remove Invalid Parentheses
- Remove Invalid Parentheses -- Leetcode
- LeetCode Remove Invalid Parentheses
- leetcode:Remove Invalid Parentheses
- leetcode Remove Invalid Parentheses
- 【LeetCode】Generate Parenthesis - Medium
- 【Leetcode】Remove Invalid Parentheses
- Remove Invalid Parentheses - LeetCode
- Hard 301题 Remove Invalid Parentheses
- Leetcode: Generate Parenthesis
- LeetCode:Generating Parenthesis
- LeetCode Remove Invalid Parenthese
- LeetCode-Remove Invalid Parentheses
- leetcode - Remove Invalid Parentheses