- 浏览: 184685 次
- 性别:
- 来自: 济南
文章分类
最新评论
Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
这道题的意思给定一个二维数组,里面只包含两个数 0和1,0代表水,1代表陆地,在水平方向和竖直方向相连的1才能代表一块连通的陆地,我们从给出的example中可以看到斜方向上的1不代表相连的陆地。
与其说这是二维数组的题目不如说是图的题目,相当于在一个图中求解包含1的连通子图。我们可以采用深度优先搜索来解决,当我们搜索到1时就把能够和它相连的所有的1都变为‘X’(可以变为任意一个不为1的字符),这样我们只需要得到遍历中1的个数即可,代码如下:
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
这道题的意思给定一个二维数组,里面只包含两个数 0和1,0代表水,1代表陆地,在水平方向和竖直方向相连的1才能代表一块连通的陆地,我们从给出的example中可以看到斜方向上的1不代表相连的陆地。
与其说这是二维数组的题目不如说是图的题目,相当于在一个图中求解包含1的连通子图。我们可以采用深度优先搜索来解决,当我们搜索到1时就把能够和它相连的所有的1都变为‘X’(可以变为任意一个不为1的字符),这样我们只需要得到遍历中1的个数即可,代码如下:
public class Solution { public int numIslands(char[][] grid) { if(grid == null || grid.length == 0) return 0; int m = grid.length; int n = grid[0].length; int result = 0; for(int i = 0; i < m; i++) for(int j = 0; j < n; j++) { if(grid[i][j] != '1') continue; result ++; getResult(grid, i, j); } return result; } public void getResult(char[][] grid, int i, int j) { if(i < 0 || j < 0 || i >= grid.length || j >= grid[0].length) return; if(grid[i][j] == '1') { grid[i][j] = 'X'; getResult(grid, i - 1, j); getResult(grid, i + 1, j); getResult(grid, i, j - 1); getResult(grid, i, j + 1); } } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 269Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 388Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 378Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 482Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 666Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 472The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 432Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 582Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 904Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 933Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 690Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 855Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 788You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 721For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Number of Islands.java
java java_leetcode题解之Number of Islands II.java
java java_leetcode题解之Number of Closed Islands.java
java java_leetcode题解之Number of Big Islands.java
python python_leetcode题解之200_Number_of_Islands.py
- **Number of Islands**:这个问题要求计算二维数组(可以视为网格)中连通的“1”(代表陆地)组成的岛屿数量。 - **Valid Sudoku**:检查一个数独是否有效,即每一行、每一列以及每一个宫格内的数字是否都在1到...
The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...
Number of Islands(岛屿数量),这类问题通常与图的遍历算法有关;LRU Cache(最近最少使用缓存),这需要了解缓存淘汰算法并实现相关的数据结构。 综上所述,LeetCode上的题目涵盖了计算机科学中的众多重要知识点...
lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 1 SINGLE NUMBER 2 HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes ...of ...of ...of ...Number of Islands 18 M
Number of Islands **知识点:** - **问题描述:** - 计算在一个二维矩阵中,有多少个由 '1' (表示陆地) 组成的连通区域。 - **解决方案分析:** - **四向深度优先搜索(DFS):** - 从任意一个 '1' 开始进行...
Islands:DFS My Calendar II:小空间匹配 My Calendar I:同上 *732. My Calendar III:难,小数据量可以用线段匹配,大数据量要用LCT(但是这东西看不懂) Construct String from Binary Tree:中序遍历 Word Ladder...
number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Input:...
Number of Islands], BFS 2017.06.14 打卡[LeetCode 3. Longest Substring Without Repeating Characters], N/A 2017.06.15 打卡[LeetCode 407. Trapping Rain Water II], BFS/Priority queue 2017.06.19 打卡...
10. **图论**:包括深度优先搜索(DFS)和广度优先搜索(BFS),例如"岛屿数量"(Number of Islands)要求计算二维矩阵中的连通岛屿。 Python 作为一种强大且易学的语言,是解决这些问题的常用工具,其简洁的语法和...
例如,"岛屿数量"(Number of Islands)问题,找出二进制矩阵中的连通岛屿。 7. **动态规划(Dynamic Programming)**:动态规划是解决复杂问题的有效方法,通过将问题分解为子问题来求解。例如,"背包问题"...
这个问题通常出现在数据结构和算法的练习中,如LeetCode上的《Number of Islands》题目。 在描述中,我们了解到给定一个二维网格,其中包含‘1’(陆地)和‘0’(水),岛屿是由水平或垂直相邻的陆地形成的,且...
狼人问题leetcode 不同岛屿的数量 给定一个由 0 和 1 组成的非空 2D 阵列网格,岛是一组 1(代表陆地)以 ...个方向(水平或垂直)连接。您可以假设网格的所有四个边缘都被水包围。...当且仅当一个岛可以平移(而不是旋转...
题目 给定一个由 ‘1’(陆地)和 ...链接:https://leetcode-cn.com/problems/number-of-islands 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路 用宽度优先搜索思想,将一个根节点(取
and the Graduate School of the University of Oregon in partial fulfillment of the requirements for the degree of Master of Science March 2009 11 "Effectiveness of Extracting Water Surface Slopes from ...