Skip to content

Commit

Permalink
Site updated: 2024-06-18 18:09:15
Browse files Browse the repository at this point in the history
  • Loading branch information
zade23 committed Jun 18, 2024
1 parent d46d67c commit 0c72267
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 132 deletions.
23 changes: 16 additions & 7 deletions 2023/07/06/Python条件判断的优先级/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@


<title>Python条件判断的优先级 | ANdRoid&#39;s BLOG</title>
<meta name="description" content="背景做LeetCode的时候经常能够发现很多基本功不扎实的问题。这一次是在做一道简单题时候忽视的条件判断优先级的问题 题目连接:https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;count-the-number-of-vowel-strings-in-range 代码部分 问题代码12345678910class Solution: def vowelStrings(self,">
<meta name="description" content="背景 代码部分 问题代码 正确代码 具体分析 背景做LeetCode的时候经常能够发现很多基本功不扎实的问题。这一次是在做一道简单题时候忽视的条件判断优先级的问题 题目连接:https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;count-the-number-of-vowel-strings-in-range 代码部分 问题代码12345678910class Solution:">
<meta property="og:type" content="article">
<meta property="og:title" content="Python条件判断的优先级">
<meta property="og:url" content="https://zade23.github.io/2023/07/06/Python%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E7%9A%84%E4%BC%98%E5%85%88%E7%BA%A7/index.html">
<meta property="og:site_name" content="ANdRoid&#39;s BLOG">
<meta property="og:description" content="背景做LeetCode的时候经常能够发现很多基本功不扎实的问题。这一次是在做一道简单题时候忽视的条件判断优先级的问题 题目连接:https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;count-the-number-of-vowel-strings-in-range 代码部分 问题代码12345678910class Solution: def vowelStrings(self,">
<meta property="og:description" content="背景 代码部分 问题代码 正确代码 具体分析 背景做LeetCode的时候经常能够发现很多基本功不扎实的问题。这一次是在做一道简单题时候忽视的条件判断优先级的问题 题目连接:https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;count-the-number-of-vowel-strings-in-range 代码部分 问题代码12345678910class Solution:">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-07-06T12:53:13.000Z">
<meta property="article:modified_time" content="2024-03-21T06:42:48.368Z">
<meta property="article:modified_time" content="2024-06-18T10:01:11.723Z">
<meta property="article:author" content="Android">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
Expand Down Expand Up @@ -366,17 +366,26 @@ <h1 class="article-title" itemprop="name">
<span class="post-comment"><i class="icon icon-comment"></i> <a href="/2023/07/06/Python%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E7%9A%84%E4%BC%98%E5%85%88%E7%BA%A7/#comments" class="article-comment-link">评论</a></span>


<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 788(字)</span>
<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 806(字)</span>


<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 3(分)</span>
<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 4(分)</span>


</div>
</div>
<div class="article-entry marked-body" itemprop="articleBody">

<h2 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h2><p>做LeetCode的时候经常能够发现很多基本功不扎实的问题。<br>这一次是在做一道简单题时候忽视的<strong>条件判断</strong>优先级的问题</p>
<ul>
<li><a href="#%E8%83%8C%E6%99%AF">背景</a></li>
<li><a href="#%E4%BB%A3%E7%A0%81%E9%83%A8%E5%88%86">代码部分</a><ul>
<li><a href="#%E9%97%AE%E9%A2%98%E4%BB%A3%E7%A0%81">问题代码</a></li>
<li><a href="#%E6%AD%A3%E7%A1%AE%E4%BB%A3%E7%A0%81">正确代码</a></li>
</ul>
</li>
<li><a href="#%E5%85%B7%E4%BD%93%E5%88%86%E6%9E%90">具体分析</a></li>
</ul>
<h2 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h2><p>做LeetCode的时候经常能够发现很多基本功不扎实的问题。<br>这一次是在做一道简单题时候忽视的<strong>条件判断</strong>优先级的问题</p>
<blockquote>
<p>题目连接:<a target="_blank" rel="noopener" href="https://leetcode.cn/problems/count-the-number-of-vowel-strings-in-range">https://leetcode.cn/problems/count-the-number-of-vowel-strings-in-range</a></p>
</blockquote>
Expand All @@ -390,7 +399,7 @@ <h2 id="具体分析"><a href="#具体分析" class="headerlink" title="具体
<p>在第二段错误代码中,条件判断部分存在问题。以下是有问题的代码片段:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> words[i][<span class="number">0</span>] == <span class="string">&#x27;a&#x27;</span> <span class="keyword">or</span> words[i][<span class="number">0</span>] == <span class="string">&#x27;e&#x27;</span> <span class="keyword">or</span> words[i][<span class="number">0</span>] == <span class="string">&#x27;i&#x27;</span> <span class="keyword">or</span> words[i][<span class="number">0</span>] == <span class="string">&#x27;o&#x27;</span> <span class="keyword">or</span> words[i][<span class="number">0</span>] == <span class="string">&#x27;u&#x27;</span> <span class="keyword">and</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;a&#x27;</span> <span class="keyword">or</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;e&#x27;</span> <span class="keyword">or</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;i&#x27;</span> <span class="keyword">or</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;o&#x27;</span> <span class="keyword">or</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;u&#x27;</span>:</span><br><span class="line"> res += <span class="number">1</span></span><br></pre></td></tr></table></figure>

<p>问题出在逻辑运算符的优先级上。在Python中,<code>and</code> 运算符的优先级高于 <code>or</code> 运算符,因此该条件判断实际上被解释为:</p>
<p>问题出在逻辑运算符的优先级上。<strong>在Python中,<code>and</code> 运算符的优先级高于 <code>or</code> 运算符</strong>,因此该条件判断实际上被解释为:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> words[i][<span class="number">0</span>] == <span class="string">&#x27;a&#x27;</span> <span class="keyword">or</span> words[i][<span class="number">0</span>] == <span class="string">&#x27;e&#x27;</span> <span class="keyword">or</span> words[i][<span class="number">0</span>] == <span class="string">&#x27;i&#x27;</span> <span class="keyword">or</span> words[i][<span class="number">0</span>] == <span class="string">&#x27;o&#x27;</span> <span class="keyword">or</span> (words[i][<span class="number">0</span>] == <span class="string">&#x27;u&#x27;</span> <span class="keyword">and</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;a&#x27;</span>) <span class="keyword">or</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;e&#x27;</span> <span class="keyword">or</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;i&#x27;</span> <span class="keyword">or</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;o&#x27;</span> <span class="keyword">or</span> words[i][-<span class="number">1</span>] == <span class="string">&#x27;u&#x27;</span>:</span><br><span class="line"> res += <span class="number">1</span></span><br></pre></td></tr></table></figure>

<p>由于 <code>or</code> 运算符的短路特性,只要 <code>words[i][0]</code> 的首字母为元音字母之一,整个条件判断就会被认为是True,导致res增加。而第二个部分 <code>words[i][-1] == &#39;a&#39;</code><code>words[i][-1] == &#39;e&#39;</code>等都是单独的条件,不会影响整个条件判断的结果。</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@


<title>完备区间判断下的二分查找万能模板 | ANdRoid&#39;s BLOG</title>
<meta name="description" content="场景单调数列&#x2F;数组 一定可以用二分;但如果非单调的情况,也有可能存在能够使用二分的情况 二分模板整数二分C++123456789101112131415161718192021222324252627#include &lt;iostream&gt;&#x2F;&#x2F; 整数二分的模板bool check(int x) &amp;#123;&#x2F;*......*&#x2F;&amp;#125; &#x2F;&#x2F; 检查x是否满足某种性质&#x2F;*">
<meta name="description" content="场景 二分模板 整数二分 C++ Python 浮点数二分 场景单调数列&#x2F;数组 一定可以用二分;但如果非单调的情况,也有可能存在能够使用二分的情况 二分模板整数二分C++123456789101112131415161718192021222324252627#include &lt;iostream&gt;&#x2F;&#x2F; 整数二分的模板bool check(int x) &amp;#123">
<meta property="og:type" content="article">
<meta property="og:title" content="完备区间判断下的二分查找万能模板">
<meta property="og:url" content="https://zade23.github.io/2023/07/07/%E5%AE%8C%E5%A4%87%E5%8C%BA%E9%97%B4%E5%88%A4%E6%96%AD%E4%B8%8B%E7%9A%84%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E4%B8%87%E8%83%BD%E6%A8%A1%E6%9D%BF/index.html">
<meta property="og:site_name" content="ANdRoid&#39;s BLOG">
<meta property="og:description" content="场景单调数列&#x2F;数组 一定可以用二分;但如果非单调的情况,也有可能存在能够使用二分的情况 二分模板整数二分C++123456789101112131415161718192021222324252627#include &lt;iostream&gt;&#x2F;&#x2F; 整数二分的模板bool check(int x) &amp;#123;&#x2F;*......*&#x2F;&amp;#125; &#x2F;&#x2F; 检查x是否满足某种性质&#x2F;*">
<meta property="og:description" content="场景 二分模板 整数二分 C++ Python 浮点数二分 场景单调数列&#x2F;数组 一定可以用二分;但如果非单调的情况,也有可能存在能够使用二分的情况 二分模板整数二分C++123456789101112131415161718192021222324252627#include &lt;iostream&gt;&#x2F;&#x2F; 整数二分的模板bool check(int x) &amp;#123">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-07-07T06:57:15.000Z">
<meta property="article:modified_time" content="2024-03-21T06:42:59.254Z">
<meta property="article:modified_time" content="2024-06-18T10:03:01.022Z">
<meta property="article:author" content="Android">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="C++">
Expand Down Expand Up @@ -367,7 +367,7 @@ <h1 class="article-title" itemprop="name">
<span class="post-comment"><i class="icon icon-comment"></i> <a href="/2023/07/07/%E5%AE%8C%E5%A4%87%E5%8C%BA%E9%97%B4%E5%88%A4%E6%96%AD%E4%B8%8B%E7%9A%84%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E4%B8%87%E8%83%BD%E6%A8%A1%E6%9D%BF/#comments" class="article-comment-link">评论</a></span>


<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 366(字)</span>
<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 383(字)</span>


<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 1(分)</span>
Expand All @@ -377,7 +377,19 @@ <h1 class="article-title" itemprop="name">
</div>
<div class="article-entry marked-body" itemprop="articleBody">

<h2 id="场景"><a href="#场景" class="headerlink" title="场景"></a>场景</h2><p>单调数列&#x2F;数组 一定可以用二分;但如果非单调的情况,也有可能存在能够使用二分的情况</p>
<ul>
<li><a href="#%E5%9C%BA%E6%99%AF">场景</a></li>
<li><a href="#%E4%BA%8C%E5%88%86%E6%A8%A1%E6%9D%BF">二分模板</a><ul>
<li><a href="#%E6%95%B4%E6%95%B0%E4%BA%8C%E5%88%86">整数二分</a><ul>
<li><a href="#c">C++</a></li>
<li><a href="#python">Python</a></li>
</ul>
</li>
<li><a href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E4%BA%8C%E5%88%86">浮点数二分</a></li>
</ul>
</li>
</ul>
<h2 id="场景"><a href="#场景" class="headerlink" title="场景"></a>场景</h2><p>单调数列&#x2F;数组 一定可以用二分;但如果非单调的情况,也有可能存在能够使用二分的情况</p>
<hr>
<h2 id="二分模板"><a href="#二分模板" class="headerlink" title="二分模板"></a>二分模板</h2><h3 id="整数二分"><a href="#整数二分" class="headerlink" title="整数二分"></a>整数二分</h3><h4 id="C"><a href="#C" class="headerlink" title="C++"></a>C++</h4><figure class="highlight c++"><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><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 整数二分的模板</span></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">check</span><span class="params">(<span class="type">int</span> x)</span> </span>&#123;<span class="comment">/*......*/</span>&#125; <span class="comment">// 检查x是否满足某种性质</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> 目前发现整数二分的所有情况都可以被下面的两个板子涵盖</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 2.1.1 区间被划分成 [l, mid] 和 [mid + 1, r]</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">bsearch_1</span><span class="params">(<span class="type">int</span> l, <span class="type">int</span> r)</span> </span>&#123;</span><br><span class="line"> <span class="keyword">while</span> (l &lt; r) &#123;</span><br><span class="line"> <span class="type">int</span> mid = l + r &gt;&gt; <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">check</span>(mid)) r = mid;</span><br><span class="line"> <span class="keyword">else</span> l = mid + <span class="number">1</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> l</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2.1.2 区间被划分成 [l, mid - 1] 和 [mid, r]</span></span><br><span class="line"><span class="type">int</span> <span class="built_in">bsearch_2</span>(<span class="type">int</span> l, <span class="type">int</span> r) &#123;</span><br><span class="line"> <span class="keyword">while</span> (l &lt; r) &#123;</span><br><span class="line"> <span class="type">int</span> mid = l + r + <span class="number">1</span> &gt;&gt; <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">chedk</span>(mid)) l = mid;</span><br><span class="line"> <span class="keyword">else</span> r = mid - <span class="number">1</span>;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

Expand Down
Loading

0 comments on commit 0c72267

Please sign in to comment.