zoukankan      html  css  js  c++  java
  • LeetCode 752. Open the Lock

    原题链接在这里:https://leetcode.com/problems/open-the-lock/

    题目:

    You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.

    The lock initially starts at '0000', a string representing the state of the 4 wheels.

    You are given a list of deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.

    Given a target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.

    Example 1:

    Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
    Output: 6
    Explanation:
    A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
    Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
    because the wheels of the lock become stuck after the display becomes the dead end "0102".

    Example 2:

    Input: deadends = ["8888"], target = "0009"
    Output: 1
    Explanation:
    We can turn the last wheel in reverse to move from "0000" -> "0009".

    Example 3:

    Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
    Output: -1
    Explanation:
    We can't reach the target without getting stuck.

    Example 4:

    Input: deadends = ["0000"], target = "8888"
    Output: -1

    Note:

    1. The length of deadends will be in the range [1, 500].
    2. target will not be in the list deadends.
    3. Every string in deadends and the string target will be a string of 4 digits from the 10,000 possibilities '0000' to '9999'.

    题解:

    If the deadends include begin or target string, then it could never reach target, return -1.

    Otherwise, use BFS to iterate from "0000".

    Every time, we poll and get the current. For each char in current, plus or minus 1, have the new candidate, if the caidiate is not visited or not in the deadend, add it to the queue.

    Time Complexity: O(V+E). V is node count. E is edge count.

    Space: O(V).

    AC Java:

     1 class Solution {
     2     public int openLock(String[] deadends, String target) {
     3         HashSet<String> hs = new HashSet<>(Arrays.asList(deadends));
     4         if(hs.contains("0000") || hs.contains(target)){
     5             return -1;
     6         }
     7         
     8         LinkedList<Node> que = new LinkedList<>();
     9         HashSet<String> visited = new HashSet<>();
    10         visited.add("0000");
    11         que.add(new Node("0000", 0));
    12         while(!que.isEmpty()){
    13             Node cur = que.poll();
    14             if(cur.s.equals(target)){
    15                 return cur.num;
    16             }
    17             
    18             String s = cur.s;
    19             for(int i = 0; i<s.length(); i++){
    20                 char c = s.charAt(i);
    21                 String can1 = s.substring(0, i) + (c-'0'+1)%10 + s.substring(i+1);
    22                 if(!hs.contains(can1) && !visited.contains(can1)){
    23                     visited.add(can1);
    24                     que.add(new Node(can1, cur.num+1));
    25                 }
    26                 
    27                 String can2 = s.substring(0, i) + (c-'0'+10-1)%10 + s.substring(i+1);
    28                 if(!hs.contains(can2) && !visited.contains(can2)){
    29                     visited.add(can2);
    30                     que.add(new Node(can2, cur.num+1));
    31                 }
    32             }
    33         }
    34         
    35         return -1;
    36     }
    37 }
    38 
    39 class Node{
    40     String s;
    41     int num;
    42     public Node(String s, int num){
    43         this.s = s;
    44         this.num = num;
    45     }
    46 }

    Could use bidirectional BFS.

    Time Complexity: O(V+E).

    Space: O(V).

    AC Java:

     1 class Solution {
     2     public int openLock(String[] deadends, String target) {
     3         HashSet<String> deadSet = new HashSet<>(Arrays.asList(deadends));
     4         if(deadSet.contains("0000") || deadSet.contains(target)){
     5             return -1;
     6         }
     7         
     8         int level = 0;
     9         HashSet<String> beginSet = new HashSet<>();
    10         HashSet<String> endSet = new HashSet<>();
    11         beginSet.add("0000");
    12         endSet.add(target);
    13         while(!beginSet.isEmpty() && !endSet.isEmpty()){
    14             if(beginSet.size() > endSet.size()){
    15                 HashSet<String> temp = beginSet;
    16                 beginSet = endSet;
    17                 endSet = temp;
    18             }
    19             
    20             HashSet<String> nextSet = new HashSet<>();
    21             for(String s : beginSet){
    22                 if(endSet.contains(s)){
    23                     return level;
    24                 }
    25                 
    26                 for(int i = 0; i<s.length(); i++){
    27                     char c = s.charAt(i);
    28                     String can1 = s.substring(0, i) + (c-'0'+1)%10 + s.substring(i+1);
    29                     String can2 = s.substring(0, i) + (c-'0'+10-1)%10 + s.substring(i+1);
    30                     if(!deadSet.contains(can1)){
    31                         nextSet.add(can1);
    32                     }
    33                     
    34                     if(!deadSet.contains(can2)){
    35                         nextSet.add(can2);
    36                     }
    37                 }
    38             }
    39             
    40             level++;
    41             beginSet = nextSet;
    42         }
    43         
    44         return -1;
    45     }
    46 }

    跟上Word Ladder II.

  • 相关阅读:
    作业:利用正则表达式知识, 编辑一个简单的表达式计算器
    正则表达式:内置re模块
    HTML5笔记
    安卓开发-intent在Activity之间数据传递
    NumPy-矩阵部分
    Jupyter notebook入门
    Anaconda入门教程
    在python2里面使用python3的print
    判断浏览器是不是支持html5
    python爬虫-html解析器beautifulsoup
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11934377.html
Copyright © 2011-2022 走看看