原题链接在这里:https://leetcode.com/problems/shortest-bridge/

题目:

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.)

Now, we may change 0s to 1s so as to connect the two islands together to form 1 island.

Return the smallest number of 0s that must be flipped.  (It is guaranteed that the answer is at least 1.)

Example 1:

Input: [[0,1],[1,0]]
Output: 1

Example 2:

Input: [[0,1,0],[0,0,0],[0,0,1]]
Output: 2

Example 3:

Input: [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
Output: 1

Note:

  1. 1 <= A.length = A[0].length <= 100
  2. A[i][j] == 0 or A[i][j] == 1

题解:

Find an index pointing to 1. Starting from it, iterating its neighbors and put all the islands into first set.

Perform BFS for each index in the current set, search surroundings, it is never visited before, and it is 1, then it is a cell in the other island, return level.

Otherwise, it is water, add it to nextSet.

Time Complexity: O(m*n). m = A.length. n = A[0].length.

Space: O(m*n).

AC Java:

 class Solution {
int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public int shortestBridge(int[][] A) {
if(A == null || A.length == 0 || A[0].length == 0){
return -1;
} int m = A.length;
int n = A[0].length;
int r = 0;
int c = 0;
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(A[i][j] == 0){
continue;
} r = i;
c = j;
}
} boolean [][] visited = new boolean[m][n];
LinkedList<int []> que = new LinkedList<>();
HashSet<int []> beginSet = new HashSet<>();
visited[r][c] = true;
que.add(new int[]{r,c});
while(!que.isEmpty()){
int [] cur = que.poll();
beginSet.add(cur);
for(int [] dir : dirs){
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if(x<0 || x>=m || y<0 || y>=n || A[x][y]!= 1 || visited[x][y]){
continue;
} visited[x][y] = true;
que.add(new int[]{x, y});
}
} int level = 0; while(!beginSet.isEmpty()){
HashSet<int []> nextSet = new HashSet<>();
for(int [] node : beginSet){
for(int [] dir : dirs){
int x = node[0] + dir[0];
int y = node[1] + dir[1];
if(x<0 || x>=m || y<0 || y>=n || visited[x][y]){
continue;
} visited[x][y] = true;
if(A[x][y] == 1){
return level;
} nextSet.add(new int[]{x, y});
}
} level++;
beginSet = nextSet;
} return -1;
}
}

LeetCode 934. Shortest Bridge的更多相关文章

  1. [LeetCode] 934. Shortest Bridge 最短的桥梁

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...

  2. LC 934. Shortest Bridge

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...

  3. 【LeetCode】934. Shortest Bridge 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地 ...

  4. 【leetcode】934. Shortest Bridge

    题目如下: In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connecte ...

  5. Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)

    Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  6. [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  7. [LeetCode] 243. Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  9. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

随机推荐

  1. C++完全二叉树的权值

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> int ...

  2. C 编程环境搭建 Window 篇

    前言 - 简介 我们在写代码的过程中, 不可避免的重度依赖所处的开发环境. 本文重点带大家在 Window 搭建 C 简单控制台项目. 当作存档, 用于记录项目搭建各种重复操作.  在详细过程之前, ...

  3. Shell学习笔记之关于 >/dev/null 2>&1 详解

    shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 分解这个组合:“>/dev/null 2>&1” 为五 ...

  4. c#栈的用法

    栈是一种重要的线性结构,栈和队列是限定插入和删除只能在表的“端点”进行的线性表 –栈的元素必须“后进先出”. –栈的操作只能在这个线性表的表尾进行. –注:对于栈来说,这个表尾称为栈的栈顶(top), ...

  5. 【已解決】谷歌浏览器如何清理缓存(cookie)

    清除缓存快捷键 Ctrl+Shift+Delete

  6. 第三章 Maven构建 Java Spring Boot Web项目

    3.1   认识Srping Boot Spring Boot是一个框架,是一种全新的编程规范,它的产生简化了对框架的使用,简化了Spring众多的框架中大量的繁琐的配置文件,所以说Spring Bo ...

  7. javascript实现网页倒计时效果

    一.HTML代码如下: <div class="timer" id="timer"> <span style="color: bla ...

  8. Java 之 匿名对象

    一.匿名对象 创建对象时,只有创建对象的语句,却没有把对象地址赋值给某个变量. 虽然是创建对象的简化写法,但是应用场景非常有限. 匿名对象:没有变量名的对象. 语法格式: new 类名(参数列表): ...

  9. 为什么会有jQuery、Dojo、Ext、Prototype、YUI、Zepto这么多JS包?

    目前流行的JS框架很多Dojo .Scriptaculous .Prototype .yui-ext .Jquery .Mochikit.mootools .moo.fx 等.当然还有很多我都不熟悉的 ...

  10. VB参考

    Open 语句: 能够对文件输入/输出 (I/O). Open pathname For mode [Access access] [lock] As [#]filenumber [Len=recle ...