原题链接在这里: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. 使用scrapy框架做武林中文网的爬虫

    一.安装 首先scrapy的安装之前需要安装这个模块:wheel.lxml.Twisted.pywin32,最后在安装scrapy pip install wheel pip install lxml ...

  2. GoCN每日新闻(2019-09-23)

    1. 查看 Go 的代码优化过程http://xargin.com/go-compiler-opt 2. go 学习笔记之仅仅需要一个示例就能讲清楚什么闭包 https://segmentfault. ...

  3. 《PHP7底层设计与源码实现》学习笔记2——结构体对齐

    书里给了一段代码,假如有个结构体如下: struct test {     char a;     int b;     long c;     void* d;     int e;     cha ...

  4. C# yield checked,unchecked lock语句(C#学习笔记04)

    特殊语句 yield语句 yield用于终止迭代 只能使用在返回类型必须为 IEnumerable.IEnumerable<T>.IEnumerator 或 IEnumerator< ...

  5. String类的方法应用

    String类的几个方法的应用示例: using System;using System.Collections.Generic;using System.Linq;using System.Text ...

  6. "类"的讲稿

    -----------------------面向对象基础------------------------------------方法(函数) { (c#p10为主,p27;javap96)+资料,讲 ...

  7. 【翻译】在TypeScript中,Extends和Implements一个抽象类有什么不同

    我们知道在TypeScript中一个类既可以被implement也可以被extends,有一些C#或java基础的同学可能会对此感到困惑,因为在上述两个面向对象的语言里面只有接口可以被implemen ...

  8. Matlab访问者模式

    访问者(Visitor)模式的定义:将作用于某种数据结构中的各元素的操作分离出来封装成独立的类,使其在不改变数据结构的前提下可以添加作用于这些元素的新的操作,为数据结构中的每个元素提供多种访问方式.它 ...

  9. uniapp之w-picker使用采坑

    1. uniapp之w-picker使用采坑 1.1. 前言 由于我是先在index页面集合了这个组件,发现该文件内容实在太多了,不好维护,所以打算把内容一个个抽成组件,在抽w-picker时,遇到了 ...

  10. linq自定义条件Lambda过滤方法

    Public Func<NoramalClass,bool>simpleComare<NormalClass>(string property,object value) { ...