题目:

你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 。每个拨轮可以自由旋转:例如把 '9' 变为 '0''0'变为 '9' 。每次旋转都只能旋转一个拨轮的一位数字。

锁的初始数字为 '0000' ,一个代表四个拨轮的数字的字符串。

列表 deadends 包含了一组死亡数字,一旦拨轮的数字和列表里的任何一个元素相同,这个锁将会被永久锁定,无法再被旋转。

字符串 target 代表可以解锁的数字,你需要给出最小的旋转次数,如果无论如何不能解锁,返回 -1。

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.

示例 1:

输入:deadends = ["0201","0101","0102","1212","2002"], target = "0202"
输出:6
解释:
可能的移动序列为 "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202"。
注意 "0000" -> "0001" -> "0002" -> "0102" -> "0202" 这样的序列是不能解锁的,
因为当拨动到 "0102" 时这个锁就会被锁定。

示例 2:

输入: deadends = ["8888"], target = "0009"
输出:1
解释:
把最后一位反向旋转一次即可 "0000" -> "0009"。

示例 3:

输入: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
输出:-1
解释:
无法旋转到目标数字且不被锁定。

示例 4:

输入: deadends = ["0000"], target = "8888"
输出:-1

提示:

  1. 死亡列表 deadends 的长度范围为 [1, 500]
  2. 目标数字 target 不会在 deadends 之中。
  3. 每个 deadendstarget 中的字符串的数字会在 10,000 个可能的情况 '0000''9999' 中产生。

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

解题思路:

题目要求给出最小的旋转次数,应该就是用 BFS(广度优先搜索)解题了。初始字符串为 "0000" 那么第二步就是 "1000" "9000" "0100" "0900" "0010" "0090" "0001" "0009" 共八个字符串,也就是说每一步的字符串拨动密码扩展到下一步时可以得到八个新字符串。把它想象成图的形式:很明显相当于每个节点后有八个节点,用 BFS 每次走一个节点,直到达到目标节点,即是最短路径。

另外需要注意:每次到要判断节点是否为给出的死亡数字,并且把已遍历的节点也加入死亡数字以防止重复。这样只能将原数组形式的死亡数字转为哈希表以减少查找操作的复杂度。用队列暂存下一步需要遍历的节点。Java、python无法直接修改字符串里的字符.Java可先转换成 char 型数组,python可借助切片组装新字符串。

Java:

class Solution {
public int openLock(String[] deadends, String target) {
HashSet<String> dead_set = new HashSet<>(Arrays.asList(deadends));//死亡数字转为哈希表
if (dead_set.contains("0000")) return -1;//死亡数字如果含有初始节点,返回-1
Queue<String> queue = new LinkedList<>();//队列
queue.add("0000");//加入初始节点
int count = 0;//记录步数
while (!queue.isEmpty()) {//节点未访问完,队列内的节点不为空
int size = queue.size();//每一步节点数
while (size-- > 0) {
String tmp = queue.remove();//弹出头节点
if (target.equals(tmp)) return count;//如果与目标数相同,直接返回步数
char[] c = tmp.toCharArray();//转为数组
for (int j = 0; j < 4; j++) {//每次修改四位数字的一位
int i = c[j] - '0';//转为int型
c[j] = (char) ('0' + (i + 9) % 10);//数字-1。余数运算可防止节点为0、9时出现-1、10的情况
String s = new String(c);//得到新字符串
if (!dead_set.contains(s)) {//字符串不在死亡数字中时
queue.add(s);//添加到队列作为下一步需要遍历的节点
dead_set.add(s);//下一步必访问该节点,所以可先加入到死亡数字
}
c[j] = (char) ('0' + (i + 11) % 10);//数字+1
s = new String(c);
if (!dead_set.contains(s)) {
queue.add(s);
dead_set.add(s);
}
c[j] = (char) ('0' + i);
}
}
count++;
}
return -1;
}
}

Python:

class Solution:
def openLock(self, deadends: List[str], target: str) -> int:
#转成哈希表
dead_set = set(deadends)
if '0000' in dead_set: return -1
que = collections.deque(['0000'])
count = 0
while que:
for x in range(len(que)):
#从左取出头节点
tmp = que.popleft()
if tmp == target: return count
for i in range(4):
#分别存修改字符的左半部分字符串,待修改字符(转成int),右半部分字符
left, mid, right = tmp[:i], int(tmp[i]), tmp[i + 1:]
#j数字加一、减一拨动操作
for x in [-1, 1]:
s = left + str((mid + x) % 10) + right#切片拼接字符
if not s in dead_set:
dead_set.add(s)
que.append(s)
count += 1
return -1

关注微.信.公.众号:爱写Bug,一起学习吖

LeetCode 752:打开转盘锁 Open the Lock的更多相关文章

  1. Java实现 LeetCode 752 打开转盘锁(暴力)

    752. 打开转盘锁 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋 ...

  2. leetcode 752. 打开转盘锁

    地址 https://leetcode-cn.com/problems/open-the-lock/ 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', ...

  3. Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock)

    Leetcode之广度优先搜索(BFS)专题-752. 打开转盘锁(Open the Lock) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  4. [Swift]LeetCode752. 打开转盘锁 | Open the Lock

    You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...

  5. [LeetCode] 0752. Open the Lock 打开转盘锁

    题目 You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', ' ...

  6. leetcode752. 打开转盘锁

    我们可以将 0000 到 9999 这 10000 状态看成图上的 10000 个节点,两个节点之间存在一条边,当且仅当这两个节点对应的状态只有 1 位不同,且不同的那位相差 1(包括 0 和 9 也 ...

  7. 第19/24周 锁升级(Lock Escalations)

    大家好,欢迎回到性能调优培训.上2个星期我们已经讨论了SQLServer里的悲观和乐观锁.今天我想谈下SQL Server里对于锁的一个特殊现象:所谓的锁升级(Lock Escalations).在我 ...

  8. ORACLE基础之oracle锁(oracle lock mode)详解

    ORACLE里锁有以下几种模式: 0:none 1:null 空 2:Row-S 行共享(RS):共享表锁,sub share  3:Row-X 行独占(RX):用于行的修改,sub exclusiv ...

  9. 同步锁Synchronized与Lock的区别?

    synchronized与Lock两者区别: 1:Lock是一个接口,而Synchronized是关键字. 2:Synchronized会自动释放锁,而Lock必须手动释放锁. 3:Lock可以让等待 ...

随机推荐

  1. vue 仿微信朋友圈9张图上传功能

    项目需求要求用户上传商品的时候可以一次性上传9张图,多余9张提示‘只能上传9张图’,并且每张图右上角有个删除按钮,图片也可以点击放大. 出来的效果图如下: 话不多说,上代码: <el-form- ...

  2. CAT 默认密码修改

    修改操作 1.按照如下路径,打开SessionManager类,cat-home目录下:com.dianping.cat.system.page.login.service.SessionManage ...

  3. python基础(34):线程(二)

    1. python线程 1.1 全局解释器锁GIL Python代码的执行由Python虚拟机(也叫解释器主循环)来控制.Python在设计之初就考虑到要在主循环中,同时只有一个线程在执行.虽然 Py ...

  4. centos 配置sentry+钉钉+邮件通知

    1.sentry官方推荐docker方式安装.使用docker-compose,最好是centos7 2.卸载旧版本 yum remove docker docker-common docker-se ...

  5. 最好用的koa2+mysql的RESTful API脚手架,mvc架构,支持node调试,pm2部署。

     #基于webpack构建的 Koa2 restful API 服务器脚手架    这是一个基于 Koa2 的轻量级 RESTful API Server 脚手架,支持 ES6, 支持使用TypeSc ...

  6. JS基础语法---数组案例---9个练习

    练习1:求数组中所有元素的和 var arr1 = [10, 20, 30, 40, 50]; var sum = 0; for (var i = 0; i < arr1.length; i++ ...

  7. 从HTML开始

    <html>:做网页,是一种超文本标记语言. 超文本:既有添加文本的能力,还可以添加图片,视频等多媒体元素. 标记:由标签组成.不同的标签有不同的效果.        开始标签,结束标签. ...

  8. 移动OA办公——Smobiler第一个开源应用解决方案,快来get吧

    产品简介 SmoONE是一款移动OA类的开源解决方案,通过Smobiler平台开发,包含了注册.登陆.用户信息等基本功能.集成了OA中使用场景较多的报销.请假.部门管理.成本中心等核心功能. 免费获取 ...

  9. [Go] 利用channel实现简单的工作池

    先启动固定数量的goroutine,每个goroutine都在从channel中获取数据,如果这个channel为空,就阻塞等待在那里channel中传递一个Car类型,这个类型主要负责具体做的任务也 ...

  10. K60时钟分析

    转载:https://blog.csdn.net/hcx25909/article/details/7164650 1.飞思卡尔K60时钟系统          飞思卡尔K60时钟系统如上图所示,可以 ...