Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/main/java/leetcode/editor/cn/all.json
This commit is contained in:
huangge1199@hotmail.com 2021-06-28 23:43:51 +08:00
commit 485f342ca9
5 changed files with 344 additions and 1 deletions

View File

@ -0,0 +1,105 @@
//给你一个数组 routes 表示一系列公交线路其中每个 routes[i] 表示一条公交线路 i 辆公交车将会在上面循环行驶
//
//
// 例如路线 routes[0] = [1, 5, 7] 表示第 0 辆公交车会一直按序列 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1
//-> ... 这样的车站路线行驶
//
//
// 现在从 source 车站出发初始时不在公交车上要前往 target 车站 期间仅可乘坐公交车
//
// 求出 最少乘坐的公交车数量 如果不可能到达终点车站返回 -1
//
//
//
// 示例 1
//
//
//输入routes = [[1,2,7],[3,6,7]], source = 1, target = 6
//输出2
//解释最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6
//
//
// 示例 2
//
//
//输入routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
//输出-1
//
//
//
//
// 提示
//
//
// 1 <= routes.length <= 500.
// 1 <= routes[i].length <= 105
// routes[i] 中的所有值 互不相同
// sum(routes[i].length) <= 105
// 0 <= routes[i][j] < 106
// 0 <= source, target < 106
//
// Related Topics 广度优先搜索 数组 哈希表
// 👍 149 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//815:公交路线
public class BusRoutes {
public static void main(String[] args) {
//测试代码
Solution solution = new BusRoutes().new Solution();
System.out.println(solution.numBusesToDestination(new int[][]{{1, 2, 7}, {3, 6, 7}}, 1, 6));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int numBusesToDestination(int[][] routes, int source, int target) {
Map<Integer, List<Integer>> site = new HashMap<>();
Map<Integer, List<Integer>> bus = new HashMap<>();
for (int i = 0; i < routes.length; i++) {
List<Integer> busList = new ArrayList<>();
for (int j = 0; j < routes[i].length; j++) {
List<Integer> list = site.getOrDefault(routes[i][j], new ArrayList<>());
list.add(i);
site.put(routes[i][j], list);
busList.add(routes[i][j]);
}
bus.put(i, busList);
}
int count = minCount(site, bus, source, target, 0, new ArrayList<>());
return count == Integer.MAX_VALUE ? -1 : count;
}
private int minCount(Map<Integer, List<Integer>> map, Map<Integer, List<Integer>> bus, int source, int target, int count, List<Integer> use) {
if (source == target) {
return count;
}
if (map.get(source).size() == 1 && use.contains(map.get(source).get(0))) {
return Integer.MAX_VALUE;
}
int min = Integer.MAX_VALUE;
for (int busIndex : map.get(source)) {
if (use.contains(busIndex)) {
continue;
}
for (int stata : bus.get(busIndex)) {
if (stata == source) {
continue;
}
use.add(busIndex);
min = Math.min(min, minCount(map, bus, stata, target, count + 1, use));
use.remove(use.size() - 1);
}
}
return min;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,40 @@
<p>给你一个数组 <code>routes</code> ,表示一系列公交线路,其中每个 <code>routes[i]</code> 表示一条公交线路,第 <code>i</code> 辆公交车将会在上面循环行驶。</p>
<ul>
<li>例如,路线 <code>routes[0] = [1, 5, 7]</code> 表示第 <code>0</code> 辆公交车会一直按序列 <code>1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...</code> 这样的车站路线行驶。</li>
</ul>
<p>现在从 <code>source</code> 车站出发(初始时不在公交车上),要前往 <code>target</code> 车站。 期间仅可乘坐公交车。</p>
<p>求出 <strong>最少乘坐的公交车数量</strong> 。如果不可能到达终点车站,返回 <code>-1</code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>routes = [[1,2,7],[3,6,7]], source = 1, target = 6
<strong>输出:</strong>2
<strong>解释:</strong>最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
<strong>输出:</strong>-1
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= routes.length <= 500</code>.</li>
<li><code>1 <= routes[i].length <= 10<sup>5</sup></code></li>
<li><code>routes[i]</code> 中的所有值 <strong>互不相同</strong></li>
<li><code>sum(routes[i].length) <= 10<sup>5</sup></code></li>
<li><code>0 <= routes[i][j] < 10<sup>6</sup></code></li>
<li><code>0 <= source, target < 10<sup>6</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>广度优先搜索</li><li>数组</li><li>哈希表</li></div></div>\n<div><li>👍 148</li><li>👎 0</li></div>

View File

@ -0,0 +1,141 @@
//你有一个带有四个圆形拨轮的转盘锁每个拨轮都有10个数字 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
// 每个拨轮可以自由旋转例如把 '9' 变为 '0''0' 变为 '9' 每次旋转都只能旋转一个拨轮的一位数字
//
// 锁的初始数字为 '0000' 一个代表四个拨轮的数字的字符串
//
// 列表 deadends 包含了一组死亡数字一旦拨轮的数字和列表里的任何一个元素相同这个锁将会被永久锁定无法再被旋转
//
// 字符串 target 代表可以解锁的数字你需要给出最小的旋转次数如果无论如何不能解锁返回 -1
//
//
//
// 示例 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"], targ
//et = "8888"
//输出-1
//解释
//无法旋转到目标数字且不被锁定
//
//
// 示例 4:
//
//
//输入: deadends = ["0000"], target = "8888"
//输出-1
//
//
//
//
// 提示
//
//
// 死亡列表 deadends 的长度范围为 [1, 500]
// 目标数字 target 不会在 deadends 之中
// 每个 deadends target 中的字符串的数字会在 10,000 个可能的情况 '0000' '9999' 中产生
//
// Related Topics 广度优先搜索 数组 哈希表 字符串
// 👍 283 👎 0
package leetcode.editor.cn;
import java.util.*;
//752:打开转盘锁
public class OpenTheLock {
public static void main(String[] args) {
//测试代码
Solution solution = new OpenTheLock().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int openLock(String[] deadends, String target) {
//记录死亡节点
List<String> set = new ArrayList<>();
for (int i = 0; i < deadends.length; i++) {
set.add(deadends[i]);
}
int step = 0;
HashSet<String> visited = new HashSet<>();
//广度所需要的队列
Queue<String> queue = new LinkedList<>();
queue.offer("0000");
visited.add("0000");
while (queue.size() != 0) {
int sz = queue.size();
//广度优先遍历
for (int i = 0; i < sz; i++) {
String cur = queue.poll();
if (set.contains(cur)) {
continue;
}
if (target.equals(cur)) {
return step;
}
//添加判断那八种可能
for (int j = 0; j < 4; j++) {
String up = plusOne(cur, j);
if (!visited.contains(up)) {
visited.add(up);
queue.offer(up);
}
String down = minusOne(cur, j);
if (!visited.contains(down)) {
visited.add(down);
queue.offer(down);
}
}
}
step++;
}
return -1;
}
//向上拨的方法
public String plusOne(String s, int j) {
char[] ch = s.toCharArray();
if (ch[j] == '9') {
ch[j] = '0';
} else {
ch[j] += 1;
}
return new String(ch);
}
//向下拨的方法
public String minusOne(String s, int j) {
char[] ch = s.toCharArray();
if (ch[j] == '0') {
ch[j] = '9';
} else {
ch[j] -= 1;
}
return new String(ch);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,56 @@
<p>你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字 <code>'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'</code> 。每个拨轮可以自由旋转:例如把 <code>'9'</code> 变为 <code>'0'</code><code>'0'</code> 变为 <code>'9'</code> 。每次旋转都只能旋转一个拨轮的一位数字。</p>
<p>锁的初始数字为 <code>'0000'</code> ,一个代表四个拨轮的数字的字符串。</p>
<p>列表 <code>deadends</code> 包含了一组死亡数字,一旦拨轮的数字和列表里的任何一个元素相同,这个锁将会被永久锁定,无法再被旋转。</p>
<p>字符串 <code>target</code> 代表可以解锁的数字,你需要给出最小的旋转次数,如果无论如何不能解锁,返回 -1。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>deadends = ["0201","0101","0102","1212","2002"], target = "0202"
<strong>输出:</strong>6
<strong>解释:</strong>
可能的移动序列为 "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202"。
注意 "0000" -> "0001" -> "0002" -> "0102" -> "0202" 这样的序列是不能解锁的,
因为当拨动到 "0102" 时这个锁就会被锁定。
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> deadends = ["8888"], target = "0009"
<strong>输出:</strong>1
<strong>解释:</strong>
把最后一位反向旋转一次即可 "0000" -> "0009"。
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>输入:</strong> deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
<strong>输出:</strong>-1
<strong>解释:
</strong>无法旋转到目标数字且不被锁定。
</pre>
<p><strong>示例 4:</strong></p>
<pre>
<strong>输入:</strong> deadends = ["0000"], target = "8888"
<strong>输出:</strong>-1
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ol>
<li>死亡列表 <code>deadends</code> 的长度范围为 <code>[1, 500]</code></li>
<li>目标数字 <code>target</code> 不会在 <code>deadends</code> 之中。</li>
<li>每个 <code>deadends</code><code>target</code> 中的字符串的数字会在 10,000 个可能的情况 <code>'0000'</code><code>'9999'</code> 中产生。</li>
</ol>
<div><div>Related Topics</div><div><li>广度优先搜索</li><li>数组</li><li>哈希表</li><li>字符串</li></div></div>\n<div><li>👍 279</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long