128 lines
7.5 KiB
HTML
128 lines
7.5 KiB
HTML
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-01-matrix · MarkDown</title><meta name="description" content="leetcode-01-matrix - 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-01-matrix</h1><div class="post-meta"><div class="post-time">2020年4月16日</div></div><div class="post-content"><h3 id="542-_01__u77E9_u9635"><a href="#542-_01__u77E9_u9635" class="headerlink" title="542. 01 矩阵"></a>542. 01 矩阵</h3><p><a href="https://leetcode-cn.com/problems/01-matrix/" target="_blank" rel="noopener">题目</a></p>
|
||
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
|
||
<p>想了两种思路</p>
|
||
<ol>
|
||
<li><p>0 位置的上下左右是 1, 上下左右中有跟 1 相邻的就是 2,以此类推,从 0 的坐标开始往上下左右四个方向扩散。如果我们把同意个距离的看作是一层,可以用一个队列依次存放每一层的坐标,直至每个坐标都被计算过。</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></pre></td><td class="code"><pre><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">updateMatrix</span><span class="params">(self, matrix: List[List[int]])</span> -> List[List[int]]:</span></span><br><span class="line"> m, n = len(matrix), len(matrix[<span class="number">0</span>])</span><br><span class="line"> dist = [[<span class="number">0</span>] * n <span class="keyword">for</span> _ <span class="keyword">in</span> range(m)]</span><br><span class="line"> zeroes_pos = [(i, j) <span class="keyword">for</span> i <span class="keyword">in</span> range(m) <span class="keyword">for</span> j <span class="keyword">in</span> range(n) <span class="keyword">if</span> matrix[i][j] == <span class="number">0</span>]</span><br><span class="line"> <span class="comment"># 将所有的 0 添加进初始队列中</span></span><br><span class="line"> q = collections.deque(zeroes_pos)</span><br><span class="line"> seen = set(zeroes_pos)</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 广度优先搜索</span></span><br><span class="line"> <span class="keyword">while</span> q:</span><br><span class="line"> i, j = q.popleft()</span><br><span class="line"> <span class="keyword">for</span> ni, nj <span class="keyword">in</span> [(i - <span class="number">1</span>, j), (i + <span class="number">1</span>, j), (i, j - <span class="number">1</span>), (i, j + <span class="number">1</span>)]:</span><br><span class="line"> <span class="keyword">if</span> <span class="number">0</span> <= ni < m <span class="keyword">and</span> <span class="number">0</span> <= nj < n <span class="keyword">and</span> (ni, nj) <span class="keyword">not</span> <span class="keyword">in</span> seen:</span><br><span class="line"> dist[ni][nj] = dist[i][j] + <span class="number">1</span></span><br><span class="line"> q.append((ni, nj))</span><br><span class="line"> seen.add((ni, nj))</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> dist</span><br></pre></td></tr></table></figure>
|
||
</li>
|
||
<li><p>从左上角开往右下角遍历矩阵,当前坐标的距离由左和上两个位置的值确定。遍历一遍后,再反过来从右下角开始往左上角遍历,当前坐标的距离根据右和下两个位置的值确定,比较这两次得出的值中较小的一个即为该点的距离。</p>
|
||
</li>
|
||
</ol>
|
||
</div></article></div></section><footer><div class="paginator"><a href="/2020/04/16/leetcode-merge-intervals/" class="prev">PRVE</a><a href="/2020/04/14/leetcode-add-two-numbers-ii/" 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> |