• Remove Invalid Parentheses 解答


    Question

    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 ).

    Examples:

    "()())()" -> ["()()()", "(())()"]
    "(a)())()" -> ["(a)()()", "(a())()"]
    ")(" -> [""]

    Solution

    看到parenthese的问题,第一反应是用栈。这题要求minimum number,所以想到用BFS遍历解空间树。

    思路为:

    层次依次为删除0个元素,1个元素,2个元素。。。

    层次遍历所有的可能。如果有一种可能是valid,那么不再遍历下面的层。

     1 public class Solution {
     2     public List<String> removeInvalidParentheses(String s) {
     3         Set<String> visited = new HashSet<String>();
     4         List<String> result = new ArrayList<String>();
     5         List<String> current = new ArrayList<String>();
     6         List<String> next;
     7         current.add(s);
     8         boolean reached = false;
     9         // BFS
    10         while (!current.isEmpty()) {
    11             next = new ArrayList<String>();
    12             for (String prev : current) {
    13                 visited.add(prev);
    14                 // If valid
    15                 if (isValid(prev)) {
    16                     reached = true;
    17                     result.add(prev);
    18                 }
    19                 
    20                 // If not reached, then delete
    21                 if (!reached) {
    22                     for (int i = 0; i < prev.length(); i++) {
    23                         char tmp = prev.charAt(i);
    24                         if (tmp != '(' && tmp != ')') {
    25                             continue;
    26                         }
    27                         String newStr = prev.substring(0, i) + prev.substring(i + 1);
    28                         if (!visited.contains(newStr)) {
    29                             next.add(newStr);
    30                             visited.add(newStr);
    31                         }
    32                     }
    33                 }
    34             }
    35             if (reached) {
    36                 break;
    37             }
    38             current = next;
    39         }
    40         return result;
    41     }
    42     
    43     private boolean isValid(String s) {
    44         Deque<Integer> stack = new LinkedList<Integer>();
    45         for (int i = 0; i < s.length(); i++) {
    46             char cur = s.charAt(i);
    47             if (cur != '(' && cur != ')') {
    48                 continue;
    49             }
    50             if (cur == '(') {
    51                 stack.push(i);
    52             }
    53             if (cur == ')') {
    54                 if (stack.isEmpty()) {
    55                     return false;
    56                 } else {
    57                     stack.pop();
    58                 }
    59             }
    60         }
    61         return stack.isEmpty();
    62     }
    63 }

    事实上,我们可以不用栈,用一个count来检测是否是valid parenthese.

     1 private boolean isValid(String s) {
     2     int count = 0;
     3     for (int i = 0; i < s.length(); i++) {
     4         char cur = s.charAt(i);
     5         if (cur != '(' && cur != ')') {
     6             continue;
     7         }
     8         if (cur == '(') {
     9             count++;
    10         }
    11         if (cur == ')') {
    12             count--;
    13             if (count < 0) {
    14                 return false;
    15             }
    16         }
    17     }
    18     return count == 0;
    19 }
  • 相关阅读:
    Jquery操作select
    Session在类库中的使用
    从一个表取数据更新另一个表的信息
    判断数据库表中是否存在某个字段
    .net项目中上传的图片或者文件太大 无法上传
    计算机中丢失 msvcr110.dlll
    Ubuntu搭建FTP server
    Linux常用命令集
    系统清理篇
    ubuntu安装 ssh server
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4944260.html
Copyright © 2020-2023  润新知