【问题标题】:Open the Lock - BFS Application打开锁 - BFS 应用程序
【发布时间】:2020-08-17 00:18:34
【问题描述】:

我刚刚学习了BFS算法,我在这里尝试应用BFS算法解决leetcode问题Open the Lock

我的算法适用于某些用例,而对其他用例输出错误的答案。谁能帮我理解我错过了什么?

提前致谢

    class Solution {
    Queue<String> queue = new LinkedList<String>();
    HashSet<String> deads = new HashSet<String>();
    public int openLock(String[] deadends, String target) {
        for(int i  = 0; i < deadends.length; i++){
            deads.add(deadends[i]);
        }
        if(deads.contains("0000"))return -1;
        int level  = bfs("0000", target);
        return level;
    
    }
public int bfs(String start, String target){
        int level  = 0;
        queue.add(start); // add the start to the queue
        deads.add(start);
        while(!queue.isEmpty()){
            int groupSize = queue.size();
            while(groupSize >0){
                String current = queue.poll();
                if(current.equals(target)) return level;
                for(int i  = 0; i < current.length(); i++){
                    char c = current.charAt(i);
                    char temp  = c;
                    if( c == '9'){
                        c = '0';
                        temp  = c;
                    }else{
                        c++;
                    }
                    String upString = current.substring(0, i) + c + current.substring(i + 1);
                    if(!deads.contains(upString)){
                        queue.add(upString);
                        deads.add(upString);
                    }
                    c = temp;
                    if( c == '0'){
                        c = '9';
                    }
                    else{
                        c--;
                    }
                    String downString = current.substring(0, i) + c + current.substring(i + 1);
                    if(!deads.contains(downString)){
                        queue.add(downString);
                        deads.add(downString);
                    }
                }
                groupSize = groupSize - 1;
            }
            level = level + 1;
        }
        return -1;
    }
}

【问题讨论】:

    标签: java algorithm queue breadth-first-search


    【解决方案1】:

    不太确定您的算法可能有什么问题,在我看来还不错,可能是需要修复的小问题。

    几乎相同的方法,除了我们将使用三个 Set 来解决 Java 中的问题:

    public final class Solution {
        public static final int openLock(
            final String[] deadends,
            final String target
        ) {
            Set<String> startSet = new HashSet<>();
            Set<String> endSet = new HashSet<>();
            Set<String> deadSet = new HashSet<>(Arrays.asList(deadends));
            startSet.add("0000");
            endSet.add(target);
            int minTurns = 0;
            Set<String> tempSet;
    
            while (!startSet.isEmpty() && !endSet.isEmpty()) {
                if (startSet.size() > endSet.size()) {
                    tempSet = startSet;
                    startSet = endSet;
                    endSet = tempSet;
                }
    
                tempSet = new HashSet<>();
    
                for (String start : startSet) {
                    if (endSet.contains(start)) {
                        return minTurns;
                    }
    
                    if (deadSet.contains(start)) {
                        continue;
                    }
    
                    deadSet.add(start);
                    StringBuilder startSB = new StringBuilder(start);
    
                    for (int i = 0; i < 4; i++) {
                        final char character = startSB.charAt(i);
                        String s1 = startSB.substring(0, i) + (character == '9' ? 0 : character - '0' + 1) + startSB.substring(i + 1);
                        String s2 = startSB.substring(0, i) + (character == '0' ? 9 : character - '0' - 1) + startSB.substring(i + 1);
    
                        if (!deadSet.contains(s1)) {
                            tempSet.add(s1);
                        }
    
                        if (!deadSet.contains(s2)) {
                            tempSet.add(s2);
                        }
                    }
                }
    
                minTurns++;
                startSet = tempSet;
            }
    
            return -1;
        }
    }
    

    好的!这是 LeetCode 的 BFS 解决方案,您可以根据它来解决:

    class Solution {
        public int openLock(String[] deadends, String target) {
            Set<String> dead = new HashSet();
            for (String d: deadends) dead.add(d);
    
            Queue<String> queue = new LinkedList();
            queue.offer("0000");
            queue.offer(null);
    
            Set<String> seen = new HashSet();
            seen.add("0000");
    
            int depth = 0;
            while (!queue.isEmpty()) {
                String node = queue.poll();
                if (node == null) {
                    depth++;
                    if (queue.peek() != null)
                        queue.offer(null);
                } else if (node.equals(target)) {
                    return depth;
                } else if (!dead.contains(node)) {
                    for (int i = 0; i < 4; ++i) {
                        for (int d = -1; d <= 1; d += 2) {
                            int y = ((node.charAt(i) - '0') + d + 10) % 10;
                            String nei = node.substring(0, i) + ("" + y) + node.substring(i+1);
                            if (!seen.contains(nei)) {
                                seen.add(nei);
                                queue.offer(nei);
                            }
                        }
                    }
                }
            }
            return -1;
        }
    }
    
    

    他们似乎为这个问题添加了新的测试用例。


    参考文献

    • 有关更多详细信息,请参阅Discussion Board,您可以在其中找到大量解释清楚且公认的解决方案,其中包含各种languages,包括低复杂度算法和渐近runtime/memory 分析@ 987654325@, 2.

    【讨论】:

    【解决方案2】:
     class Solution {
        Queue<String> queue = new LinkedList<String>();
        HashSet<String> deads = new HashSet<String>();
        public int openLock(String[] deadends, String target) {
            for(int i  = 0; i < deadends.length; i++){
                deads.add(deadends[i]);
            }
            if(deads.contains("0000"))return -1;
            int level  = bfs("0000", target);
            return level;
        
        }
    public int bfs(String start, String target){
            int level  = 0;
            queue.add(start); // add the start to the queue
            deads.add(start);
            while(!queue.isEmpty()){
                int groupSize = queue.size();
                while(groupSize >0){
                    String current = queue.poll();
                    if(current.equals(target)) return level;
                    for(int i  = 0; i < current.length(); i++){
                        char c = current.charAt(i);
                        char temp  = c;
                        if( c == '9'){
                            c = '0';
                            temp  = c; // THIS LINE IS THE ISSUE. REMOVING IT HELPED!!!!
                        }else{
                            c++;
                        }
                        String upString = current.substring(0, i) + c + current.substring(i + 1);
                        if(!deads.contains(upString)){
                            queue.add(upString);
                            deads.add(upString);
                        }
                        c = temp;
                        if( c == '0'){
                            c = '9';
                        }
                        else{
                            c--;
                        }
                        String downString = current.substring(0, i) + c + current.substring(i + 1);
                        if(!deads.contains(downString)){
                            queue.add(downString);
                            deads.add(downString);
                        }
                    }
                    groupSize = groupSize - 1;
                }
                level = level + 1;
            }
            return -1;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      相关资源
      最近更新 更多