Skip to content

Commit

Permalink
Site updated: 2024-01-11 14:35:33
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaibineric committed Jan 11, 2024
1 parent ad8665e commit d05f5c1
Show file tree
Hide file tree
Showing 5 changed files with 700 additions and 129 deletions.
67 changes: 62 additions & 5 deletions 2024/01/01/leetcode/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
<meta name="author" content="Haibin Li">
<meta name="keywords" content="">

<meta name="description" content="数组 二分查找 哈希表 链表 二叉树 回溯算法 贪心算法 动态规划">
<meta name="description" content="语言细节 vector的长度: C++:nums.size() Python:len(nums) GO:len(nums) for循环: C++:条件小括号+循环体中括号 Python:冒号且不需要小括号包条件 GO:循环体中括号,条件按照C++写但是不需要小括号 数组 二分查找 题目描述 链接:https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;binary-search&#x2F;des">
<meta property="og:type" content="article">
<meta property="og:title" content="【算法题】LeetCode算法汇总">
<meta property="og:url" content="https://lihaibineric.github.io/2024/01/01/leetcode/index.html">
<meta property="og:site_name" content="LIHAIBIN&#39;S BLOG">
<meta property="og:description" content="数组 二分查找 哈希表 链表 二叉树 回溯算法 贪心算法 动态规划">
<meta property="og:description" content="语言细节 vector的长度: C++:nums.size() Python:len(nums) GO:len(nums) for循环: C++:条件小括号+循环体中括号 Python:冒号且不需要小括号包条件 GO:循环体中括号,条件按照C++写但是不需要小括号 数组 二分查找 题目描述 链接:https:&#x2F;&#x2F;leetcode.cn&#x2F;problems&#x2F;binary-search&#x2F;des">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/lihaibineric/picgo/raw/master/pic/image-20240111141316267.png">
<meta property="article:published_time" content="2024-01-01T13:15:17.000Z">
<meta property="article:modified_time" content="2024-01-01T13:37:45.348Z">
<meta property="article:modified_time" content="2024-01-11T06:34:59.251Z">
<meta property="article:author" content="Haibin Li">
<meta property="article:tag" content="算法题">
<meta property="article:tag" content="leetcode">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://gitee.com/lihaibineric/picgo/raw/master/pic/image-20240111141316267.png">



Expand Down Expand Up @@ -209,7 +211,7 @@
<span class="post-meta mr-2">
<i class="iconfont icon-chart"></i>

26 words
2.8k words

</span>

Expand All @@ -220,7 +222,7 @@



1 mins
24 mins

</span>

Expand Down Expand Up @@ -263,8 +265,63 @@ <h1 id="seo-header">【算法题】LeetCode算法汇总</h1>
<div class="markdown-body">

<meta name="referrer" content="no-referrer"/>
<h2 id="语言细节">语言细节</h2>
<p>vector的长度:</p>
<ul>
<li>C++:nums.size()</li>
<li>Python:len(nums)</li>
<li>GO:len(nums)</li>
</ul>
<p>for循环:</p>
<ul>
<li>C++:条件小括号+循环体中括号</li>
<li>Python:冒号且不需要小括号包条件</li>
<li>GO:循环体中括号,条件按照C++写但是不需要小括号</li>
</ul>
<h1 id="数组">数组</h1>
<h3 id="二分查找">二分查找</h3>
<h4 id="题目描述">题目描述</h4>
<p>链接:https://leetcode.cn/problems/binary-search/description/</p>
<p>给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target
,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回
-1。</p>
<p><strong>示例 1:</strong></p>
<figure class="highlight makefile"><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 makefile"><span class="hljs-section">输入: nums = [-1,0,3,5,9,12], target = 9</span><br><span class="hljs-section">输出: 4</span><br><span class="hljs-section">解释: 9 出现在 nums 中并且下标为 4</span><br></code></pre></td></tr></table></figure>
<p><strong>示例 2:</strong></p>
<figure class="highlight makefile"><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 makefile"><span class="hljs-section">输入: nums = [-1,0,3,5,9,12], target = 2</span><br><span class="hljs-section">输出: -1</span><br><span class="hljs-section">解释: 2 不存在 nums 中因此返回 -1</span><br></code></pre></td></tr></table></figure>
<h4 id="思路">思路</h4>
<p>题目表示的是有序数组,而且题目没有重复元素。在二分查找的过程中,保持不变量,就是在while寻找中每一次边界的处理都要坚持根据区间的定义来操作,这就是<strong>循环不变量</strong>规则</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></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_">search</span>(<span class="hljs-params">self, nums: <span class="hljs-type">List</span>[<span class="hljs-built_in">int</span>], target: <span class="hljs-built_in">int</span></span>) -&gt; <span class="hljs-built_in">int</span>:<br> left, right = <span class="hljs-number">0</span>, <span class="hljs-built_in">len</span>(nums) - <span class="hljs-number">1</span> <span class="hljs-comment"># 定义target在左闭右闭的区间里,[left, right]</span><br><br> <span class="hljs-keyword">while</span> left &lt;= right:<br> middle = left + (right - left) // <span class="hljs-number">2</span><br> <br> <span class="hljs-keyword">if</span> nums[middle] &gt; target:<br> right = middle - <span class="hljs-number">1</span> <span class="hljs-comment"># target在左区间,所以[left, middle - 1]</span><br> <span class="hljs-keyword">elif</span> nums[middle] &lt; target:<br> left = middle + <span class="hljs-number">1</span> <span class="hljs-comment"># target在右区间,所以[middle + 1, right]</span><br> <span class="hljs-keyword">else</span>:<br> <span class="hljs-keyword">return</span> middle <span class="hljs-comment"># 数组中找到目标值,直接返回下标</span><br> <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span> <span class="hljs-comment"># 未找到目标值</span><br></code></pre></td></tr></table></figure>
<p>注意这里给出的题解法:当
<code>left &lt;= right</code>的时候,以下的条件中全部都不取到等号
<code>nums[middle] &gt; target nums[middle] &lt; target</code></p>
<p>需要注意的是:<code>right=nums.size()-1</code></p>
<p>C++版本</p>
<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></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Solution</span> &#123;<br><span class="hljs-keyword">public</span>:<br> <span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">search</span><span class="hljs-params">(vector&lt;<span class="hljs-type">int</span>&gt;&amp; nums, <span class="hljs-type">int</span> target)</span> </span>&#123;<br> <span class="hljs-type">int</span> left=<span class="hljs-number">0</span>;<br> <span class="hljs-type">int</span> right=nums.<span class="hljs-built_in">size</span>()<span class="hljs-number">-1</span>;<br> <span class="hljs-keyword">while</span>(left&lt;=right)<br> &#123;<br> <span class="hljs-comment">// int middle = (left+right)/2; 这样写会溢出</span><br> <span class="hljs-type">int</span> middle = left + ((right - left) / <span class="hljs-number">2</span>);<br> <span class="hljs-keyword">if</span>(nums[middle]&gt;target)<br> &#123;<br> right = middle<span class="hljs-number">-1</span>;<br> &#125;<br> <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(nums[middle]&lt;target)<br> &#123;<br> left = middle+<span class="hljs-number">1</span>;<br> &#125;<br> <span class="hljs-keyword">else</span>&#123;<br> <span class="hljs-keyword">return</span> middle;<br> &#125;<br> &#125;<br> <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;<br> &#125;<br>&#125;;<br></code></pre></td></tr></table></figure>
<p>Go版本</p>
<figure class="highlight go"><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></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">search</span><span class="hljs-params">(nums []<span class="hljs-type">int</span>, target <span class="hljs-type">int</span>)</span></span> <span class="hljs-type">int</span> &#123;<br> right:=<span class="hljs-built_in">len</span>(nums)<span class="hljs-number">-1</span><br> left:=<span class="hljs-number">0</span><br> <span class="hljs-keyword">for</span> left&lt;=right&#123;<br> middle:= left+(right-left)/<span class="hljs-number">2</span><br> <span class="hljs-keyword">if</span> nums[middle]&lt;target&#123;<br> left = middle+<span class="hljs-number">1</span><br> &#125;<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> nums[middle]&gt;target&#123;<br> right = middle<span class="hljs-number">-1</span><br> &#125;<span class="hljs-keyword">else</span>&#123;<br> <span class="hljs-keyword">return</span> middle<br> &#125;<br> &#125;<br> <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span><br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="移除元素">移除元素</h3>
<p>https://leetcode.cn/problems/remove-element/description/</p>
<h4 id="题目描述-1">题目描述</h4>
<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">[3,2,2,3]</span>, val = 3<br>输出:2, nums = <span class="hljs-comment">[2,2]</span><br>解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = <span class="hljs-comment">[2,2,3,3]</span> 或 nums = <span class="hljs-comment">[2,2,0,0]</span>,也会被视作正确答案。<br></code></pre></td></tr></table></figure>
<p><strong>示例 2:</strong></p>
<figure class="highlight dns"><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 dns">输入:nums = [<span class="hljs-number">0,1,2,2</span>,<span class="hljs-number">3,0,4,2</span>], val = <span class="hljs-number">2</span><br>输出:<span class="hljs-number">5</span>, nums = [<span class="hljs-number">0,1,3,0</span>,<span class="hljs-number">4</span>]<br>解释:函数应该返回新的长度 <span class="hljs-number">5</span>, 并且 nums 中的前五个元素为 <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。<br></code></pre></td></tr></table></figure>
<h4 id="思路-1">思路</h4>
<p>双指针法(快慢指针法):
<strong>通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。</strong></p>
<p>定义快慢指针</p>
<ul>
<li>快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组</li>
<li>慢指针:指向更新 新数组下标的位置</li>
</ul>
<p><img src="https://gitee.com/lihaibineric/picgo/raw/master/pic/image-20240111141316267.png" srcset="/img/loading.gif" lazyload alt="双指针题解" style="zoom:67%;" /></p>
<p>C++版本</p>
<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></pre></td><td class="code"><pre><code class="hljs C++"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Solution</span> &#123;<br><span class="hljs-keyword">public</span>:<br> <span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">removeElement</span><span class="hljs-params">(vector&lt;<span class="hljs-type">int</span>&gt;&amp; nums, <span class="hljs-type">int</span> val)</span> </span>&#123;<br> <span class="hljs-type">int</span> slowindex=<span class="hljs-number">0</span>;<br> <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> fastindex = <span class="hljs-number">0</span>; fastindex&lt;nums.<span class="hljs-built_in">size</span>();fastindex++)<br> &#123;<br> <span class="hljs-keyword">if</span>(val!=nums[fastindex])&#123;<br> nums[slowindex] = nums[fastindex];<br> slowindex++;<br> &#125;<br> &#125;<br> <span class="hljs-keyword">return</span> slowindex;<br> &#125;<br>&#125;;<br></code></pre></td></tr></table></figure>
<p>python版本</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></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Solution</span>(<span class="hljs-title class_ inherited__">object</span>):<br> <span class="hljs-keyword">def</span> <span class="hljs-title function_">removeElement</span>(<span class="hljs-params">self, nums, val</span>):<br> slowindex=<span class="hljs-number">0</span><br> fastindex=<span class="hljs-number">0</span><br> <span class="hljs-keyword">while</span> fastindex&lt;<span class="hljs-built_in">len</span>(nums):<br> <span class="hljs-keyword">if</span> val!=nums[fastindex]:<br> nums[slowindex]=nums[fastindex]<br> slowindex = slowindex+<span class="hljs-number">1</span><br> fastindex+=<span class="hljs-number">1</span><br> <span class="hljs-keyword">return</span> slowindex <br></code></pre></td></tr></table></figure>
<p>GO版本:</p>
<figure class="highlight go"><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></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">removeElement</span><span class="hljs-params">(nums []<span class="hljs-type">int</span>, val <span class="hljs-type">int</span>)</span></span> <span class="hljs-type">int</span> &#123;<br> slow:=<span class="hljs-number">0</span><br> <span class="hljs-keyword">for</span> i:=<span class="hljs-number">0</span>;i&lt;<span class="hljs-built_in">len</span>(nums);i++&#123;<br> <span class="hljs-keyword">if</span> nums[i]!=val&#123;<br> nums[slow]=nums[i]<br> slow++<br> &#125;<br> &#125;<br> <span class="hljs-keyword">return</span> slow<br>&#125;<br></code></pre></td></tr></table></figure>
<h1 id="哈希表">哈希表</h1>
<h1 id="链表">链表</h1>
<h1 id="二叉树">二叉树</h1>
Expand Down
Loading

0 comments on commit d05f5c1

Please sign in to comment.