Do you have a turntable with four circular dial lock. Each has a dial 10 digits: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
. Each dial can be rotated freely: for example the '9'
turns '0'
,'0'
turns '9'
. Each rotation can only rotate the dial of a digit.
Initial number of locks '0000'
, a number of the dial string represents four.
List deadends
contains a set number of deaths, the same number and once any element in the list click wheel, the lock will be permanently locked and can no longer be rotated.
String target
representatives can unlock digital, you need to give the minimum number of rotations, if any case can not unlock, returns -1.
Example 1:
输入:deadends = ["0201","0101","0102","1212","2002"], target = "0202"
输出:6
解释:
可能的移动序列为 "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202"。
注意 "0000" -> "0001" -> "0002" -> "0102" -> "0202" 这样的序列是不能解锁的,
因为当拨动到 "0102" 时这个锁就会被锁定。
Example 2:
输入: deadends = ["8888"], target = "0009"
输出:1
解释:
把最后一位反向旋转一次即可 "0000" -> "0009"。
Example 3:
输入: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
输出:-1
解释:
无法旋转到目标数字且不被锁定。
Example 4:
输入: deadends = ["0000"], target = "8888"
输出:-1
prompt:
- Death list
deadends
length is[1, 500]
. - Target figures
target
will not bedeadends
in. - Each
deadends
andtarget
number strings in 10,000 will possible'0000'
to'9999'
produce.