Ching 9690121403 feat(init project): add all existing files
add all existing files

Signed-off-by: Ching <loooching@gmail.com>
2022-02-02 19:04:18 +08:00

122 lines
7.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-number-of-islands · MarkDown</title><meta name="description" content="leetcode-number-of-islands - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-number-of-islands</h1><div class="post-meta"><div class="post-time">2020年4月21日</div></div><div class="post-content"><h3 id="200-__u5C9B_u5C7F_u6570_u91CF"><a href="#200-__u5C9B_u5C7F_u6570_u91CF" class="headerlink" title="200. 岛屿数量"></a>200. 岛屿数量</h3><p><a href="https://leetcode-cn.com/problems/number-of-islands/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>这种矩阵题现在第一反应就是用<a href="(https://zh.wikipedia.org/zh-cn/%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2)">广度优先搜索</a>类似之前算和0之间的距离那题。遍历矩阵遇到 1 就将 1 改成 0然后广度优先搜索找出 1 相邻的所有 1这就是一个岛屿以此类推。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> collections</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">numIslands</span><span class="params">(self, grid)</span> -&gt; int:</span></span><br><span class="line"> rows = len(grid)</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> rows:</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span></span><br><span class="line"> cols = len(grid[<span class="number">0</span>])</span><br><span class="line"> islands = <span class="number">0</span></span><br><span class="line"> <span class="keyword">for</span> r <span class="keyword">in</span> range(rows):</span><br><span class="line"> <span class="keyword">for</span> l <span class="keyword">in</span> range(cols):</span><br><span class="line"> <span class="keyword">if</span> grid[r][l] == <span class="string">'1'</span>:</span><br><span class="line"> islands += <span class="number">1</span></span><br><span class="line"> grid[r][l] = <span class="string">'0'</span></span><br><span class="line"> neighbors = collections.deque([(r, l)])</span><br><span class="line"> <span class="keyword">while</span> neighbors:</span><br><span class="line"> x, y = neighbors.popleft()</span><br><span class="line"> <span class="keyword">for</span> x_, y_ <span class="keyword">in</span> [[x<span class="number">-1</span>, y], [x+<span class="number">1</span>, y], [x, y<span class="number">-1</span>], [x, y+<span class="number">1</span>]]:</span><br><span class="line"> <span class="keyword">if</span> <span class="number">0</span>&lt;=x_&lt;rows <span class="keyword">and</span> <span class="number">0</span>&lt;=y_&lt;cols <span class="keyword">and</span> grid[x_][y_] == <span class="string">'1'</span>:</span><br><span class="line"> neighbors.append([x_, y_])</span><br><span class="line"> grid[x_][y_] = <span class="string">'0'</span></span><br><span class="line"> <span class="keyword">return</span> islands</span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/04/16/leetcode-string-to-integer-atoi/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>