Skip to content

Commit

Permalink
Site updated: 2024-03-08 18:47:00
Browse files Browse the repository at this point in the history
  • Loading branch information
oyoanan committed Mar 8, 2024
1 parent 4b13065 commit 1fc68b6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 57 deletions.
11 changes: 3 additions & 8 deletions 2024/03/02/LeetCode01/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://an-hexo-blog.oss-cn-beijing.aliyuncs.com/img/202403021502005.jpeg">
<meta property="article:published_time" content="2024-03-02T06:53:55.000Z">
<meta property="article:modified_time" content="2024-03-06T01:50:19.832Z">
<meta property="article:modified_time" content="2024-03-08T10:40:27.529Z">
<meta property="article:author" content="An">
<meta property="article:tag" content="Python">
<meta property="article:tag" content="题库">
Expand Down Expand Up @@ -225,7 +225,7 @@
<span class="post-meta mr-2">
<i class="iconfont icon-chart"></i>

622
625

</span>

Expand Down Expand Up @@ -291,19 +291,14 @@ <h1 id="1-两数之和"><a href="#1-两数之和" class="headerlink" title="1.
<p>你可以按任意顺序返回答案。</p>
<p><strong>示例 1:</strong></p>
<figure class="highlight inform7"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs inform7">输入:nums = <span class="hljs-comment">[2,7,11,15]</span>, target = 9<br>输出:<span class="hljs-comment">[0,1]</span><br>解释:因为 nums<span class="hljs-comment">[0]</span> + nums<span class="hljs-comment">[1]</span> == 9 ,返回 <span class="hljs-comment">[0, 1]</span><br></code></pre></td></tr></table></figure>

<p><strong>示例 2:</strong></p>
<figure class="highlight inform7"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs inform7">输入:nums = <span class="hljs-comment">[3,2,4]</span>, target = 6<br>输出:<span class="hljs-comment">[1,2]</span><br></code></pre></td></tr></table></figure>

<p><strong>示例 3:</strong></p>
<figure class="highlight inform7"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs inform7">输入:nums = <span class="hljs-comment">[3,3]</span>, target = 6<br>输出:<span class="hljs-comment">[0,1]</span><br></code></pre></td></tr></table></figure>

<h2 id="题解"><a href="#题解" class="headerlink" title="题解"></a>题解</h2><h3 id="(1)暴力枚举"><a href="#(1)暴力枚举" class="headerlink" title="(1)暴力枚举"></a>(1)暴力枚举</h3><p>枚举数组中的每一个数<code>x</code>,寻找数组中是否存在<code>target-x</code></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></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Solution</span>:<br> <span class="hljs-keyword">def</span> <span class="hljs-title function_">twoSum</span>(<span class="hljs-params">self,nums,target</span>):<br> n = <span class="hljs-built_in">len</span>(nums)<br> <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(n):<br> <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(i+<span class="hljs-number">1</span>,n):<br> <span class="hljs-keyword">if</span> nums[i] + nums[j] == target:<br> <span class="hljs-keyword">return</span> [i,j]<br> <span class="hljs-keyword">return</span> []<br></code></pre></td></tr></table></figure>

<h3 id="(2)哈希表"><a href="#(2)哈希表" class="headerlink" title="(2)哈希表"></a>(2)哈希表</h3><p>对于数组中的每一个数<code>x</code>,首先查询哈希表中是否存在<code>target-x</code>,如果存在则返回结果,不存在就将<code>x</code>插入到哈希表中,保证不会让<code>x</code>和自己匹配。</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></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Solution</span>:<br> <span class="hljs-keyword">def</span> <span class="hljs-title function_">twoSum</span>(<span class="hljs-params">self,nums,target</span>):<br> hashtable = <span class="hljs-built_in">dict</span>()<br> <span class="hljs-keyword">for</span> i,num <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(nums):<br> num2 = target - num <br> <span class="hljs-built_in">print</span>(num,num2)<br> <span class="hljs-keyword">if</span> num2 <span class="hljs-keyword">in</span> hashtable:<br> <span class="hljs-keyword">return</span> [i,hashtable[num2]]<br> hashtable[num] = i<br> <span class="hljs-built_in">print</span>(hashtable)<br> <span class="hljs-keyword">return</span> [] <br></code></pre></td></tr></table></figure>

<h2 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h2><ul>
<li><p><strong>是否会报超出索引异常</strong></p>
<blockquote>
Expand All @@ -312,7 +307,7 @@ <h2 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a
</li>
<li><p><strong>时间与空间复杂度</strong></p>
<blockquote>
<p>1、使用暴力枚举,时间复杂度为<img src="https://an-hexo-blog.oss-cn-beijing.aliyuncs.com/img/202403060950412.png" srcset="/img/loading.gif" lazyload alt="image-20240306095011380">,因为需要使用两层循环来检查数组中每对不同的元素,看它们的和是否等于 <code>target</code>;空间复杂度为O(1),因为除了输入和输出之外,只需要有限的额外空间。</p>
<p>1、使用暴力枚举,时间复杂度为O($n^2$),因为需要使用两层循环来检查数组中每对不同的元素,看它们的和是否等于 <code>target</code>;空间复杂度为O(1),因为除了输入和输出之外,只需要有限的额外空间。</p>
<p>2、使用哈希表,时间复杂度为O(n),因为只需要遍历数组一次,对于每个元素,可以在 O(1) 的时间内通过哈希表检查 <code>target - num</code> 是否存在;空间复杂度为O(n),因为最坏的情况下,可能需要将数组中的所有元素都存储在哈希表中。</p>
</blockquote>
</li>
Expand Down
19 changes: 8 additions & 11 deletions 2024/03/02/PAT01/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
<meta name="author" content="An">
<meta name="keywords" content="">

<meta name="description" content="1.A+B和C题目给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。 输入:输入第1行给出正整数T(&lt;&#x3D;10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。 输出:对每组测试用例,在一行中输出“Case #X: true”如果A+B&gt;C,否则输出“Case #X: false”,其中X是测试用例的编号(">
<meta name="description" content="1.A+B和C题目给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。 输入:输入第1行给出正整数T(&lt;&#x3D;10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。 输出:对每组测试用例,在一行中输出“Case #X: true”如果A+B&gt;C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)">
<meta property="og:type" content="article">
<meta property="og:title" content="PAT01:A+B和C">
<meta property="og:url" content="http://example.com/2024/03/02/PAT01/index.html">
<meta property="og:site_name" content="An&#96;s Blog">
<meta property="og:description" content="1.A+B和C题目给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。 输入:输入第1行给出正整数T(&lt;&#x3D;10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。 输出:对每组测试用例,在一行中输出“Case #X: true”如果A+B&gt;C,否则输出“Case #X: false”,其中X是测试用例的编号(">
<meta property="og:description" content="1.A+B和C题目给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。 输入:输入第1行给出正整数T(&lt;&#x3D;10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。 输出:对每组测试用例,在一行中输出“Case #X: true”如果A+B&gt;C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://an-hexo-blog.oss-cn-beijing.aliyuncs.com/img/202403021855958.jpeg">
<meta property="article:published_time" content="2024-03-02T10:48:36.000Z">
Expand Down Expand Up @@ -225,7 +225,7 @@
<span class="post-meta mr-2">
<i class="iconfont icon-chart"></i>

687
684

</span>

Expand Down Expand Up @@ -287,18 +287,14 @@ <h1 id="seo-header">PAT01:A+B和C</h1>
<div class="markdown-body">

<h1 id="1-A-B和C"><a href="#1-A-B和C" class="headerlink" title="1.A+B和C"></a>1.A+B和C</h1><h2 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h2><p>给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。</p>
<p><strong>输入:</strong>输入第1行给出正整数T(&lt;&#x3D;10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。</p>
<p><strong>输入:</strong>输入第1行给出正整数T(&lt;=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。</p>
<p><strong>输出:</strong>对每组测试用例,在一行中输出“Case #X: true”如果A+B&gt;C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。</p>
<p><strong>输入例子:</strong></p>
<figure class="highlight basic"><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></pre></td><td class="code"><pre><code class="hljs basic"><span class="hljs-number">4</span><br><span class="hljs-symbol">1 </span><span class="hljs-number">2</span> <span class="hljs-number">3</span><br><span class="hljs-symbol">2 </span><span class="hljs-number">3</span> <span class="hljs-number">4</span><br><span class="hljs-symbol">2147483647 </span><span class="hljs-number">0</span> <span class="hljs-number">2147483646</span><br><span class="hljs-symbol">0 </span>-<span class="hljs-number">2147483648</span> -<span class="hljs-number">2147483647</span><br></code></pre></td></tr></table></figure>

<p><strong>输出例子:</strong></p>
<figure class="highlight cal"><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></pre></td><td class="code"><pre><code class="hljs cal"><span class="hljs-keyword">Case</span> <span class="hljs-string">#1</span>: <span class="hljs-literal">false</span><br><span class="hljs-keyword">Case</span> <span class="hljs-string">#2</span>: <span class="hljs-literal">true</span><br><span class="hljs-keyword">Case</span> <span class="hljs-string">#3</span>: <span class="hljs-literal">true</span><br><span class="hljs-keyword">Case</span> <span class="hljs-string">#4</span>: <span class="hljs-literal">false</span><br></code></pre></td></tr></table></figure>

<h2 id="题解"><a href="#题解" class="headerlink" title="题解"></a>题解</h2><h3 id="(1)我的解法"><a href="#(1)我的解法" class="headerlink" title="(1)我的解法"></a>(1)我的解法</h3><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></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">import</span> sys<br><br>index = <span class="hljs-number">0</span><br><span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> sys.stdin:<br> index +=<span class="hljs-number">1</span><br> a = line.split()<br> a = [<span class="hljs-built_in">int</span>(i) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> a]<br> <span class="hljs-keyword">if</span> index == <span class="hljs-number">1</span>:<br> <span class="hljs-keyword">pass</span><br> <span class="hljs-keyword">else</span>:<br> <span class="hljs-built_in">print</span>(<span class="hljs-string">f&#x27;Case #<span class="hljs-subst">&#123;index-<span class="hljs-number">1</span>&#125;</span>: <span class="hljs-subst">&#123;<span class="hljs-built_in">str</span>(a[<span class="hljs-number">0</span>]+a[<span class="hljs-number">1</span>]&gt;a[<span class="hljs-number">2</span>]).lower()&#125;</span>&#x27;</span>)<br></code></pre></td></tr></table></figure>

<h3 id="(2)其他解法"><a href="#(2)其他解法" class="headerlink" title="(2)其他解法"></a>(2)其他解法</h3><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></pre></td><td class="code"><pre><code class="hljs python">n = <span class="hljs-built_in">int</span>(<span class="hljs-built_in">input</span>())<br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(n):<br> a,b,c = <span class="hljs-built_in">map</span>(<span class="hljs-built_in">int</span>,<span class="hljs-built_in">input</span>().split())<br> <span class="hljs-built_in">print</span>(<span class="hljs-string">&#x27;Case #&#x27;</span>+<span class="hljs-built_in">str</span>((i+<span class="hljs-number">1</span>))+<span class="hljs-string">&#x27;: true&#x27;</span> <span class="hljs-keyword">if</span> a+b&gt;c <span class="hljs-keyword">else</span> <span class="hljs-string">&#x27;Case #&#x27;</span>+<span class="hljs-built_in">str</span>((i+<span class="hljs-number">1</span>))+<span class="hljs-string">&#x27;: false&#x27;</span>)<br></code></pre></td></tr></table></figure>

<h2 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h2><ul>
<li><code>sys.stdin</code></li>
</ul>
Expand All @@ -309,15 +305,16 @@ <h2 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a
<p>2、<code>sys.stdin.readline()</code>: 每次调用读取一行输入,包括行尾的换行符,如果已经到达EOF,则返回一个空字符串。</p>
<p>3、<code>sys.stdin.readlines()</code>: 读取所有剩余的输入行,并将它们作为字符串列表返回。</p>
<p><code>for line in sys.stdin:</code>循环会逐行读取标准输入直到EOF。这种方式特别适合处理不确定数量的输入行。</p>
<p>在命令行环境中,通常可以通过<code>Ctrl+D</code>(在Unix&#x2F;Linux&#x2F;macOS系统中)或<code>Ctrl+Z</code>然后回车(在Windows系统中)来发送EOF信号。此外,<code>sys.stdin</code>也常被用于从文件中读取输入,通过命令行重定向操作,例如<code>python script.py &lt; inputfile.txt</code>,这时<code>inputfile.txt</code>的内容会被作为标准输入传递给脚本。</p>
<p>在命令行环境中,通常可以通过<code>Ctrl+D</code>(在Unix/Linux/macOS系统中)或<code>Ctrl+Z</code>然后回车(在Windows系统中)来发送EOF信号。此外,<code>sys.stdin</code>也常被用于从文件中读取输入,通过命令行重定向操作,例如<code>python script.py &lt; inputfile.txt</code>,这时<code>inputfile.txt</code>的内容会被作为标准输入传递给脚本。</p>
</blockquote>
<ul>
<li><strong>三元运算符</strong></li>
</ul>
<li><p><strong>三元运算符</strong></p>
<blockquote>
<p><code>print(&quot;Case #&quot;+str(i+1)+&quot;: true&quot; if b+c&gt;d else &quot;Case #&quot;+str(i+1)+&quot;: false&quot;)</code>: </p>
<p>使用条件表达式(三元操作符)简化逻辑判断,而且字符串不能直接拼接整数,需要先对整数进行<code>str()</code></p>
</blockquote>
</li>
</ul>


</div>
Expand Down
Loading

0 comments on commit 1fc68b6

Please sign in to comment.