<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>island Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/island/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/island/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Aug 2021 23:18:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>island Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/island/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1905. Count Sub Islands</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-1905-count-sub-islands/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-1905-count-sub-islands/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Aug 2021 22:42:04 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[cc]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[island]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8587</guid>

					<description><![CDATA[<p>You are given two&#160;m x n&#160;binary matrices&#160;grid1&#160;and&#160;grid2&#160;containing only&#160;0&#8216;s (representing water) and&#160;1&#8216;s (representing land). An&#160;island&#160;is a group of&#160;1&#8216;s connected&#160;4-directionally&#160;(horizontal or vertical). Any cells outside of the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1905-count-sub-islands/">花花酱 LeetCode 1905. Count Sub Islands</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given two&nbsp;<code>m x n</code>&nbsp;binary matrices&nbsp;<code>grid1</code>&nbsp;and&nbsp;<code>grid2</code>&nbsp;containing only&nbsp;<code>0</code>&#8216;s (representing water) and&nbsp;<code>1</code>&#8216;s (representing land). An&nbsp;<strong>island</strong>&nbsp;is a group of&nbsp;<code>1</code>&#8216;s connected&nbsp;<strong>4-directionally</strong>&nbsp;(horizontal or vertical). Any cells outside of the grid are considered water cells.</p>



<p>An island in&nbsp;<code>grid2</code>&nbsp;is considered a&nbsp;<strong>sub-island&nbsp;</strong>if there is an island in&nbsp;<code>grid1</code>&nbsp;that contains&nbsp;<strong>all</strong>&nbsp;the cells that make up&nbsp;<strong>this</strong>&nbsp;island in&nbsp;<code>grid2</code>.</p>



<p>Return the&nbsp;<em><strong>number</strong>&nbsp;of islands in&nbsp;</em><code>grid2</code>&nbsp;<em>that are considered&nbsp;<strong>sub-islands</strong></em>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/06/10/test1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2021/06/03/testcasex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
<strong>Output:</strong> 2 
<strong>Explanation: </strong>In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>m == grid1.length == grid2.length</code></li><li><code>n == grid1[i].length == grid2[i].length</code></li><li><code>1 &lt;= m, n &lt;= 500</code></li><li><code>grid1[i][j]</code>&nbsp;and&nbsp;<code>grid2[i][j]</code>&nbsp;are either&nbsp;<code>0</code>&nbsp;or&nbsp;<code>1</code>.</li></ul>



<h2><strong>Solution: Coloring</strong></h2>



<p>Give each island in grid1 a different color. Whiling using the same method to find island and coloring it in grid2, we also check whether the same cell in grid1 always has the same color.</p>



<p>Time complexity: O(mn)<br>Space complexity: O(1) modify in place or O(mn)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int countSubIslands(vector&lt;vector&lt;int&gt;&gt;&amp; grid1, vector&lt;vector&lt;int&gt;&gt;&amp; grid2) {
    const int m = grid1.size();
    const int n = grid1[0].size();
    int valid = 1;
    
    function&lt;void(int, int, int, int)&gt; color = [&amp;](int i, int j, int c, int p) {
      if (i &lt; 0 || i &gt;= m || j &lt; 0 || j &gt;= n) return;
      auto&amp; gc = p ? grid2 : grid1;
      auto&amp; go = p ? grid1 : grid2;
      if (gc[i][j] != 1) return;
      gc[i][j] = c;
      if (p &amp;&amp; go[i][j] != c) valid = 0;
      color(i + 1, j, c, p);
      color(i - 1, j, c, p);
      color(i, j + 1, c, p);
      color(i, j - 1, c, p);
    };
        
    for (int i = 0, c = 2; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        if (grid1[i][j] == 1) color(i, j, c++, 0);
    
    int ans = 0;
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j)
        if (grid2[i][j] == 1 &amp;&amp; grid1[i][j]) {
          valid = 1;
          color(i, j, grid1[i][j], 1);
          ans += valid;
        }    
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-1905-count-sub-islands/">花花酱 LeetCode 1905. Count Sub Islands</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-1905-count-sub-islands/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1254. Number of Closed Islands</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Nov 2019 06:38:30 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[connected components]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[island]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(n*m)]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5817</guid>

					<description><![CDATA[<p>Given a 2D&#160;grid&#160;consists of&#160;0s&#160;(land)&#160;and&#160;1s&#160;(water).&#160; An&#160;island&#160;is a maximal 4-directionally connected group of&#160;0s&#160;and a&#160;closed island&#160;is an island&#160;totally&#160;(all left, top, right, bottom) surrounded by&#160;1s. Return the number of&#160;closed&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/">花花酱 LeetCode 1254. Number of Closed Islands</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1252 1253 1254 1255 Weekly Contest 162  - 刷题找工作" width="500" height="375" src="https://www.youtube.com/embed/1XMpzhFUvco?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given a 2D&nbsp;<code>grid</code>&nbsp;consists of&nbsp;<code>0s</code>&nbsp;(land)&nbsp;and&nbsp;<code>1s</code>&nbsp;(water).&nbsp; An&nbsp;<em>island</em>&nbsp;is a maximal 4-directionally connected group of&nbsp;<code>0s</code>&nbsp;and a&nbsp;<em>closed island</em>&nbsp;is an island&nbsp;<strong>totally</strong>&nbsp;(all left, top, right, bottom) surrounded by&nbsp;<code>1s.</code></p>



<p>Return the number of&nbsp;<em>closed islands</em>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/10/31/sample_3_1610.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
Islands in gray are closed because they are completely surrounded by water (group of 1s).</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/10/31/sample_4_1610.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
<strong>Output:</strong> 1
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[1,1,1,1,1,1,1],
&nbsp;              [1,0,0,0,0,0,1],
&nbsp;              [1,0,1,1,1,0,1],
&nbsp;              [1,0,1,0,1,0,1],
&nbsp;              [1,0,1,1,1,0,1],
&nbsp;              [1,0,0,0,0,0,1],
               [1,1,1,1,1,1,1]]
<strong>Output:</strong> 2
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= grid.length, grid[0].length &lt;= 100</code></li><li><code>0 &lt;= grid[i][j] &lt;=1</code></li></ul>



<h2><strong>Solution: DFS/Backtracking</strong></h2>



<p>For each connected component, if it can reach the boundary then it&#8217;s not a closed island.</p>



<p>Time complexity: O(n*m)<br>Space complexity: O(n*m)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int closedIsland(vector&lt;vector&lt;int&gt;&gt;&amp; grid) {    
    const int n = grid.size();
    const int m = grid[0].size();    
    function&lt;int(int, int)&gt; dfs = [&amp;](int x, int y) {
      if (x &lt; 0 || y &lt; 0 || x &gt;= m || y &gt;= n) return 0;      
      if (grid[y][x]++) return 1;
      return dfs(x + 1, y) &amp; dfs(x - 1, y) &amp; dfs(x, y + 1) &amp; dfs(x, y - 1);
    };
    
    int ans = 0;
    for (int i = 0; i &lt; n; ++i)
      for (int j = 0; j &lt; m; ++j)
        if (!grid[i][j]) ans += dfs(j, i);      
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/">花花酱 LeetCode 1254. Number of Closed Islands</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 947. Most Stones Removed with Same Row or Column</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-947-most-stones-removed-with-same-row-or-column/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-947-most-stones-removed-with-same-row-or-column/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 30 Nov 2018 05:06:23 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[connected components]]></category>
		<category><![CDATA[island]]></category>
		<category><![CDATA[union find]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4379</guid>

					<description><![CDATA[<p>Problem On a 2D plane, we place stones at some integer coordinate points.&#160; Each coordinate point may have at most one stone. Now, a&#160;move&#160;consists of&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-947-most-stones-removed-with-same-row-or-column/">花花酱 LeetCode 947. Most Stones Removed with Same Row or Column</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>On a 2D plane, we place stones at some integer coordinate points.&nbsp; Each coordinate point may have at most one stone.</p>
<p>Now, a&nbsp;<em>move</em>&nbsp;consists of removing a stone&nbsp;that shares a column or row with another stone on the grid.</p>
<p>What is the largest possible number of moves we can make?</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>stones = <span id="example-input-1-2">[[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]</span>
<strong>Output: </strong>5
</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>stones = <span id="example-input-2-2">[[0,0],[0,2],[1,1],[2,0],[2,2]]</span>
<strong>Output: </strong>3
</pre>
<p><strong>Example 3:</strong></p>
<pre class="crayon:false"><strong>Input: </strong>stones = <span id="example-input-3-2">[[0,0]]</span>
<strong>Output: </strong>0
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= stones.length &lt;= 1000</code></li>
<li><code>0 &lt;= stones[i][j] &lt; 10000</code></li>
</ol>
<p><script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-fb+5w+4e-db+86" data-ad-client="ca-pub-2404451723245401" data-ad-slot="2162692788"></ins><br />
<script><br />
     (adsbygoogle = window.adsbygoogle || []).push({});<br />
</script></p>
<h1><strong>Solution 2: Union Find</strong></h1>
<p>Find all connected components (islands)</p>
<p>Ans = # of stones &#8211; # of islands</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua, running time: 16 ms
class Solution {
public:
  int removeStones(vector&lt;vector&lt;int&gt;&gt;&amp; stones) {
    const int kSize = 10000;
    DSU dsu(kSize * 2);
    for (const auto&amp; stone : stones)
      dsu.Union(stone[0], stone[1] + kSize);
    unordered_set&lt;int&gt; seen;
    for (const auto&amp; stone : stones)
      seen.insert(dsu.Find(stone[0]));
    return stones.size() - seen.size();
  }
private:
  class DSU {
  public:
    DSU(int n): p_(n) {
      for (int i = 0 ; i &lt; n; ++i)
        p_[i] = i;              
    }
    
    void Union(int i, int j) {
      p_[Find(i)] = Find(j);      
    }
    
    int Find(int i) {
      if (p_[i] != i) p_[i] = Find(p_[i]);
      return p_[i];
    }    
  private:
    vector&lt;int&gt; p_;    
  };
};</pre><p></div></div></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/searching/leetcode-200-number-of-islands/">花花酱 LeetCode 200. Number of Islands</a></li>
</ul>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-947-most-stones-removed-with-same-row-or-column/">花花酱 LeetCode 947. Most Stones Removed with Same Row or Column</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-947-most-stones-removed-with-same-row-or-column/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 200. Number of Islands</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-200-number-of-islands/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-200-number-of-islands/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 21 Sep 2017 02:23:43 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[connected components]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[island]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=373</guid>

					<description><![CDATA[<p>Problem: 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&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-200-number-of-islands/">花花酱 LeetCode 200. Number of Islands</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/XSmgFKe-XYU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>Given a 2d grid map of <code>'1'</code>s (land) and <code>'0'</code>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.</p>
<p><i><b>Example 1:</b></i></p><pre class="crayon-plain-tag">11110
11010
11000
00000</pre><p>Answer: 1</p>
<p><i><b>Example 2:</b></i></p><pre class="crayon-plain-tag">11000
11000
00100
00011</pre><p></p>
<h1><strong>Idea: DFS</strong></h1>
<p>Use DFS to find a connected component (an island) and mark all the nodes to 0.</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/200-ep65-1.png"><img class="alignnone size-full wp-image-377" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/200-ep65-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/200-ep65-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/200-ep65-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/200-ep65-1-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/200-ep65-1-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p>Time complexity: O(mn)</p>
<p>Space complexity: O(mn)</p>
<h1><strong>Solution</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Time complexity: O(mn)
// Running time: 6 ms
class Solution {
public:
    int numIslands(vector&lt;vector&lt;char&gt;&gt;&amp; grid) {
        if (grid.empty()) return 0;
        int m = grid.size();
        int n = grid[0].size();
        int ans = 0;
        for (int y = 0; y &lt; m; ++y)
            for (int x = 0; x &lt; n; ++x) {
                ans += grid[y][x] - '0';
                dfs(grid, x, y, m, n);
            }
        return ans;                
    }   
private:
    void dfs(vector&lt;vector&lt;char&gt;&gt;&amp; grid, int x, int y, int m, int n) {
        if (x &lt; 0 || y &lt; 0 || x &gt;= n || y &gt;= m || grid[y][x] == '0')
            return;
        grid[y][x] = '0';
        dfs(grid, x + 1, y, m, n);
        dfs(grid, x - 1, y, m, n);
        dfs(grid, x, y + 1, m, n);
        dfs(grid, x, y - 1, m, n);
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Time Complexity: O(mn)
// Running time: 13 ms
class Solution {
    public int numIslands(char[][] grid) {
        int m = grid.length;
        if (m == 0) return 0;
        int n = grid[0].length;
        
        int ans = 0;
        for (int y = 0; y &lt; m; ++y)
            for (int x = 0; x &lt; n; ++x)
                if (grid[y][x] == '1') {
                    ++ans;
                    dfs(grid, x, y, n, m);
                }
        
        return ans;
    }
    
    private void dfs(char[][] grid, int x, int y, int n, int m) {
        if (x &lt; 0 || y &lt; 0 || x &gt;= n || y &gt;= m || grid[y][x] == '0')
            return;
        grid[y][x] = '0';
        dfs(grid, x + 1, y, n, m);
        dfs(grid, x - 1, y, n, m);
        dfs(grid, x, y + 1, n, m);
        dfs(grid, x, y - 1, n, m);
    }
}</pre><p></div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Time Complexity: O(mn)
Running Time: 102 ms
"""
class Solution(object):
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        m = len(grid)
        if m == 0: return 0
        n = len(grid[0])
        
        ans = 0
        for y in xrange(m):
            for x in xrange(n):
                if grid[y][x] == '1':
                    ans += 1
                    self.__dfs(grid, x, y, n, m)
        return ans
    
    def __dfs(self, grid, x, y, n, m):
        if x &lt; 0 or y &lt; 0 or x &gt;=n or y &gt;= m or grid[y][x] == '0':
            return
        grid[y][x] = '0'
        self.__dfs(grid, x + 1, y, n, m)
        self.__dfs(grid, x - 1, y, n, m)
        self.__dfs(grid, x, y + 1, n, m)
        self.__dfs(grid, x, y - 1, n, m)</pre><p></div></div></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/graph/leetcode-695-max-area-of-island/">[解题报告] LeetCode 695. Max Area of Island</a></li>
<li><a href="http://zxi.mytechroad.com/blog/graph/leetcode-547-friend-circles/">[解题报告] LeetCode 547. Friend Circles</a></li>
<li><a href="https://zxi.mytechroad.com/blog/graph/leetcode-827-making-a-large-island/">花花酱 LeetCode 827. Making A Large Island</a></li>
<li><a href="https://zxi.mytechroad.com/blog/graph/leetcode-934-shortest-bridge/">花花酱 LeetCode 934. Shortest Bridge</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-200-number-of-islands/">花花酱 LeetCode 200. Number of Islands</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/searching/leetcode-200-number-of-islands/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
