mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 11:21:42 +08:00
add leetcode problem-cn part3
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<p>有两种特殊字符:</p>
|
||||
|
||||
<ul>
|
||||
<li>第一种字符可以用一比特 <code>0</code> 表示</li>
|
||||
<li>第二种字符可以用两比特(<code>10</code> 或 <code>11</code>)表示</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个以 <code>0</code> 结尾的二进制数组 <code>bits</code> ,如果最后一个字符必须是一个一比特字符,则返回 <code>true</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> bits = [1, 0, 0]
|
||||
<strong>输出:</strong> true
|
||||
<strong>解释:</strong> 唯一的解码方式是将其解析为一个两比特字符和一个一比特字符。
|
||||
所以最后一个字符是一比特字符。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>bits = [1,1,1,0]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>唯一的解码方式是将其解析为两比特字符和两比特字符。
|
||||
所以最后一个字符不是一比特字符。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= bits.length <= 1000</code></li>
|
||||
<li><code>bits[i]</code> 为 <code>0</code> 或 <code>1</code></li>
|
||||
</ul>
|
50
算法题(国内版)/problem (Chinese)/24 点游戏 [24-game].html
Normal file
50
算法题(国内版)/problem (Chinese)/24 点游戏 [24-game].html
Normal file
@@ -0,0 +1,50 @@
|
||||
<p>给定一个长度为4的整数数组 <code>cards</code> 。你有 <code>4</code> 张卡片,每张卡片上都包含一个范围在 <code>[1,9]</code> 的数字。您应该使用运算符 <code>['+', '-', '*', '/']</code> 和括号 <code>'('</code> 和 <code>')'</code> 将这些卡片上的数字排列成数学表达式,以获得值24。</p>
|
||||
|
||||
<p>你须遵守以下规则:</p>
|
||||
|
||||
<ul>
|
||||
<li>除法运算符 <code>'/'</code> 表示实数除法,而不是整数除法。
|
||||
|
||||
<ul>
|
||||
<li>例如, <code>4 /(1 - 2 / 3)= 4 /(1 / 3)= 12</code> 。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>每个运算都在两个数字之间。特别是,不能使用 <code>“-”</code> 作为一元运算符。
|
||||
<ul>
|
||||
<li>例如,如果 <code>cards =[1,1,1,1]</code> ,则表达式 <code>“-1 -1 -1 -1”</code> 是 <strong>不允许</strong> 的。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>你不能把数字串在一起
|
||||
<ul>
|
||||
<li>例如,如果 <code>cards =[1,2,1,2]</code> ,则表达式 <code>“12 + 12”</code> 无效。</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>如果可以得到这样的表达式,其计算结果为 <code>24</code> ,则返回 <code>true </code>,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> cards = [4, 1, 8, 7]
|
||||
<strong>输出:</strong> true
|
||||
<strong>解释:</strong> (8-4) * (7-1) = 24
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> cards = [1, 2, 1, 2]
|
||||
<strong>输出:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>cards.length == 4</code></li>
|
||||
<li><code>1 <= cards[i] <= 9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>给定一个长度为 <code>n</code> 的字符串 <code>s</code> ,其中 <code>s[i]</code> 是:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>“D”</code> 意味着减少,或者</li>
|
||||
<li><code>“I”</code> 意味着增加</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>有效排列</strong> 是对有 <code>n + 1</code> 个在 <code>[0, n]</code> 范围内的整数的一个排列 <code>perm</code> ,使得对所有的 <code>i</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>s[i] == 'D'</code>,那么 <code>perm[i] > perm[i+1]</code>,以及;</li>
|
||||
<li>如果 <code>s[i] == 'I'</code>,那么 <code>perm[i] < perm[i+1]</code>。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回 <em><strong>有效排列 </strong> </em><code>perm</code><em>的数量 </em>。因为答案可能很大,所以请<strong>返回你的答案对</strong> <code>10<sup>9</sup> + 7</code><strong> 取余</strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "DID"
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>
|
||||
(0, 1, 2, 3) 的五个有效排列是:
|
||||
(1, 0, 3, 2)
|
||||
(2, 0, 3, 1)
|
||||
(2, 1, 3, 0)
|
||||
(3, 0, 2, 1)
|
||||
(3, 1, 2, 0)
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> s = "D"
|
||||
<strong>输出:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == s.length</code></li>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>s[i]</code> 不是 <code>'I'</code> 就是 <code>'D'</code></li>
|
||||
</ul>
|
56
算法题(国内版)/problem (Chinese)/Dota2 参议院 [dota2-senate].html
Normal file
56
算法题(国内版)/problem (Chinese)/Dota2 参议院 [dota2-senate].html
Normal file
@@ -0,0 +1,56 @@
|
||||
<p>Dota2 的世界里有两个阵营:<code>Radiant</code>(天辉)和 <code>Dire</code>(夜魇)</p>
|
||||
|
||||
<p>Dota2 参议院由来自两派的参议员组成。现在参议院希望对一个 Dota2 游戏里的改变作出决定。他们以一个基于轮为过程的投票进行。在每一轮中,每一位参议员都可以行使两项权利中的<code><strong>一</strong></code>项:</p>
|
||||
|
||||
<ol>
|
||||
<li>
|
||||
<p><code>禁止一名参议员的权利</code>:</p>
|
||||
|
||||
<p>参议员可以让另一位参议员在这一轮和随后的几轮中丧失<strong>所有的权利</strong>。</p>
|
||||
</li>
|
||||
<li>
|
||||
<p><code>宣布胜利</code>:</p>
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<p> 如果参议员发现有权利投票的参议员都是<strong>同一个阵营的</strong>,他可以宣布胜利并决定在游戏中的有关变化。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>给定一个字符串代表每个参议员的阵营。字母 “R” 和 “D” 分别代表了 <code>Radiant</code>(天辉)和 <code>Dire</code>(夜魇)。然后,如果有 <code>n</code> 个参议员,给定字符串的大小将是 <code>n</code>。</p>
|
||||
|
||||
<p>以轮为基础的过程从给定顺序的第一个参议员开始到最后一个参议员结束。这一过程将持续到投票结束。所有失去权利的参议员将在过程中被跳过。</p>
|
||||
|
||||
<p>假设每一位参议员都足够聪明,会为自己的政党做出最好的策略,你需要预测哪一方最终会宣布胜利并在 Dota2 游戏中决定改变。输出应该是 <code>Radiant</code> 或 <code>Dire</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>"RD"
|
||||
<strong>输出:</strong>"Radiant"
|
||||
<strong>解释:</strong><code>第一个参议员来自 Radiant 阵营并且他可以使用第一项权利让第二个参议员失去权力,因此第二个参议员将被跳过因为他没有任何权利。然后在第二轮的时候,第一个参议员可以宣布胜利,因为他是唯一一个有投票权的人</code>
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>"RDD"
|
||||
<strong>输出:</strong>"Dire"
|
||||
<strong>解释:</strong>
|
||||
第一轮中,第一个<code>来自 Radiant 阵营的</code>参议员可以使用第一项权利禁止第二个参议员的权利
|
||||
第二个<code>来自 Dire 阵营的</code>参议员会被跳过因为他的权利被禁止
|
||||
第三个<code>来自 Dire 阵营的</code>参议员可以使用他的第一项权利禁止第一个参议员的权利
|
||||
因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>给定字符串的长度在 <code>[1, 10,000]</code> 之间.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,36 @@
|
||||
<p>给定一个正整数数组 <code>nums</code>和一个整数 <font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">k</span></span></font></font> ,返回 <font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">num</span></span></font></font> 中 「<strong>好子数组」</strong><em> </em>的数目。</p>
|
||||
|
||||
<p>如果 <code>nums</code> 的某个子数组中不同整数的个数恰好为 <code>k</code>,则称 <code>nums</code> 的这个连续、不一定不同的子数组为 <strong>「</strong><strong>好子数组 」</strong>。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>[1,2,3,1,2]</code> 中有 <code>3</code> 个不同的整数:<code>1</code>,<code>2</code>,以及 <code>3</code>。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>子数组</strong> 是数组的 <strong>连续</strong> 部分。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1,2,3], k = 2
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>恰好由 2 个不同整数组成的子数组:[1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1,3,4], k = 3
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>恰好由 3 个不同整数组成的子数组:[1,2,1,3], [2,1,3], [1,3,4].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i], k <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,按以下方法修改该数组:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择某个下标 <code>i</code> 并将 <code>nums[i]</code> 替换为 <code>-nums[i]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>重复这个过程恰好 <code>k</code> 次。可以多次选择同一个下标 <code>i</code> 。</p>
|
||||
|
||||
<p>以这种方式修改数组后,返回数组 <strong>可能的最大和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,2,3], k = 1
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>选择下标 1 ,nums 变为 [4,-2,3] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,-1,0,2], k = 3
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>选择下标 (1, 2, 2) ,nums 变为 [3,1,0,2] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,-3,-1,5,-4], k = 2
|
||||
<strong>输出:</strong>13
|
||||
<strong>解释:</strong>选择下标 (1, 4) ,nums 变为 [2,3,-1,5,4] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-100 <= nums[i] <= 100</code></li>
|
||||
<li><code>1 <= k <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>有 <code>n</code> 个城市通过一些航班连接。给你一个数组 <code>flights</code> ,其中 <code>flights[i] = [from<sub>i</sub>, to<sub>i</sub>, price<sub>i</sub>]</code> ,表示该航班都从城市 <code>from<sub>i</sub></code> 开始,以价格 <code>price<sub>i</sub></code> 抵达 <code>to<sub>i</sub></code>。</p>
|
||||
|
||||
<p>现在给定所有的城市和航班,以及出发城市 <code>src</code> 和目的地 <code>dst</code>,你的任务是找到出一条最多经过 <code>k</code> 站中转的路线,使得从 <code>src</code> 到 <code>dst</code> 的 <strong>价格最便宜</strong> ,并返回该价格。 如果不存在这样的路线,则输出 <code>-1</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
|
||||
src = 0, dst = 2, k = 1
|
||||
<strong>输出:</strong> 200
|
||||
<strong>解释:</strong>
|
||||
城市航班图如下
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png" style="height: 180px; width: 246px;" />
|
||||
|
||||
从城市 0 到城市 2 在 1 站中转以内的最便宜价格是 200,如图中红色所示。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
|
||||
src = 0, dst = 2, k = 0
|
||||
<strong>输出:</strong> 500
|
||||
<strong>解释:</strong>
|
||||
城市航班图如下
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/02/16/995.png" style="height: 180px; width: 246px;" />
|
||||
|
||||
从城市 0 到城市 2 在 0 站中转以内的最便宜价格是 500,如图中蓝色所示。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>0 <= flights.length <= (n * (n - 1) / 2)</code></li>
|
||||
<li><code>flights[i].length == 3</code></li>
|
||||
<li><code>0 <= from<sub>i</sub>, to<sub>i</sub> < n</code></li>
|
||||
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
|
||||
<li><code>1 <= price<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
<li>航班没有重复,且不存在自环</li>
|
||||
<li><code>0 <= src, dst, k < n</code></li>
|
||||
<li><code>src != dst</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>给定一个二进制数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p><strong>k位翻转</strong> 就是从 <code>nums</code> 中选择一个长度为 <code>k</code> 的 <strong>子数组</strong> ,同时把子数组中的每一个 <code>0</code> 都改成 <code>1</code> ,把子数组中的每一个 <code>1</code> 都改成 <code>0</code> 。</p>
|
||||
|
||||
<p>返回数组中不存在 <code>0</code> 所需的最小 <strong>k位翻转</strong> 次数。如果不可能,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>子数组</strong> 是数组的 <strong>连续</strong> 部分。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,1,0], K = 1
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>先翻转 A[0],然后翻转 A[2]。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,0], K = 2
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>无论我们怎样翻转大小为 2 的子数组,我们都不能使数组变为 [1,1,1]。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,0,0,1,0,1,1,0], K = 3
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
翻转 A[0],A[1],A[2]: A变成 [1,1,1,1,0,1,1,0]
|
||||
翻转 A[4],A[5],A[6]: A变成 [1,1,1,1,1,0,0,0]
|
||||
翻转 A[5],A[6],A[7]: A变成 [1,1,1,1,1,1,1,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>给出两个整数 <code>n</code> 和 <code>k</code>,找出所有包含从 <code>1</code> 到 <code>n</code> 的数字,且恰好拥有 <code>k</code> 个逆序对的不同的数组的个数。</p>
|
||||
|
||||
<p>逆序对的定义如下:对于数组的第<code>i</code>个和第 <code>j</code>个元素,如果满<code>i</code> < <code>j</code>且 <code>a[i]</code> > <code>a[j]</code>,则其为一个逆序对;否则不是。</p>
|
||||
|
||||
<p>由于答案可能很大,只需要返回 答案 mod 10<sup>9</sup> + 7 的值。</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> n = 3, k = 0
|
||||
<strong>输出:</strong> 1
|
||||
<strong>解释:</strong>
|
||||
只有数组 [1,2,3] 包含了从1到3的整数并且正好拥有 0 个逆序对。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> n = 3, k = 1
|
||||
<strong>输出:</strong> 2
|
||||
<strong>解释:</strong>
|
||||
数组 [1,3,2] 和 [2,1,3] 都有 1 个逆序对。
|
||||
</pre>
|
||||
|
||||
<p><strong>说明:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li> <code>n</code> 的范围是 [1, 1000] 并且 <code>k</code> 的范围是 [0, 1000]。</li>
|
||||
</ol>
|
@@ -0,0 +1,54 @@
|
||||
<p>给你一个类似 Lisp 语句的字符串表达式 <code>expression</code>,求出其计算结果。</p>
|
||||
|
||||
<p>表达式语法如下所示:</p>
|
||||
|
||||
<ul>
|
||||
<li>表达式可以为整数,<strong>let</strong> 表达式,<strong>add</strong> 表达式,<strong>mult</strong> 表达式,或赋值的变量。表达式的结果总是一个整数。</li>
|
||||
<li>(整数可以是正整数、负整数、0)</li>
|
||||
<li><strong>let</strong> 表达式采用 <code>"(let v<sub>1</sub> e<sub>1</sub> v<sub>2</sub> e<sub>2</sub> ... v<sub>n</sub> e<sub>n</sub> expr)"</code> 的形式,其中 <code>let</code> 总是以字符串 <code>"let"</code>来表示,接下来会跟随一对或多对交替的变量和表达式,也就是说,第一个变量 <code>v<sub>1</sub></code>被分配为表达式 <code>e<sub>1</sub></code> 的值,第二个变量 <code>v<sub>2</sub></code> 被分配为表达式 <code>e<sub>2</sub></code> 的值,<strong>依次类推</strong>;最终 <code>let</code> 表达式的值为 <code>expr</code>表达式的值。</li>
|
||||
<li><strong>add </strong>表达式表示为 <code>"(add e<sub>1</sub> e<sub>2</sub>)"</code> ,其中 <code>add</code> 总是以字符串 <code>"add"</code> 来表示,该表达式总是包含两个表达式 <code>e<sub>1</sub></code>、<code>e<sub>2</sub></code> ,最终结果是 <code>e<sub>1</sub></code> 表达式的值与 <code>e<sub>2</sub></code> 表达式的值之 <strong>和 </strong>。</li>
|
||||
<li><strong>mult</strong> 表达式表示为 <code>"(mult e<sub>1</sub> e<sub>2</sub>)"</code> ,其中 <code>mult</code> 总是以字符串 <code>"mult"</code> 表示,该表达式总是包含两个表达式 <code>e<sub>1</sub></code>、<code>e<sub>2</sub></code>,最终结果是 <code>e<sub>1</sub></code> 表达式的值与 <code>e<sub>2</sub></code> 表达式的值之<strong> 积 </strong>。</li>
|
||||
<li>在该题目中,变量名以小写字符开始,之后跟随 0 个或多个小写字符或数字。为了方便,<code>"add"</code> ,<code>"let"</code> ,<code>"mult"</code> 会被定义为 "关键字" ,不会用作变量名。</li>
|
||||
<li>最后,要说一下作用域的概念。计算变量名所对应的表达式时,在计算上下文中,首先检查最内层作用域(按括号计),然后按顺序依次检查外部作用域。测试用例中每一个表达式都是合法的。有关作用域的更多详细信息,请参阅示例。</li>
|
||||
</ul>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>expression = "(let x 2 (mult x (let x 3 y 4 (add x y))))"
|
||||
<strong>输出:</strong>14
|
||||
<strong>解释:</strong>
|
||||
计算表达式 (add x y), 在检查变量 x 值时,
|
||||
在变量的上下文中由最内层作用域依次向外检查。
|
||||
首先找到 x = 3, 所以此处的 x 值是 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>expression = "(let x 3 x 2 x)"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>let 语句中的赋值运算按顺序处理即可。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>expression = "(let x 1 y 2 x (add x y) (add x y))"
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>
|
||||
第一个 (add x y) 计算结果是 3,并且将此值赋给了 x 。
|
||||
第二个 (add x y) 计算结果是 3 + 2 = 5 。
|
||||
</pre>
|
||||
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= expression.length <= 2000</code></li>
|
||||
<li><code>exprssion</code> 中不含前导和尾随空格</li>
|
||||
<li><code>expressoin</code> 中的不同部分(token)之间用单个空格进行分隔</li>
|
||||
<li>答案和所有中间计算结果都符合 <strong>32-bit</strong> 整数范围</li>
|
||||
<li>测试用例中的表达式均为合法的且最终结果为整数</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>给定一个 n 叉树的根节点 <meta charset="UTF-8" /> <code>root</code> ,返回 <em>其节点值的<strong> 前序遍历</strong></em> 。</p>
|
||||
|
||||
<p>n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 <code>null</code> 分隔(请参见示例)。</p>
|
||||
|
||||
<p><br />
|
||||
<strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="height: 193px; width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,3,2,4,null,5,6]
|
||||
<strong>输出:</strong>[1,3,5,6,2,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="height: 272px; width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
<strong>输出:</strong>[1,2,3,6,7,11,14,4,8,12,5,9,13,10]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>节点总数在范围<meta charset="UTF-8" /> <code>[0, 10<sup>4</sup>]</code>内</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>n 叉树的高度小于或等于 <code>1000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>递归法很简单,你可以使用迭代法完成此题吗?</p>
|
@@ -0,0 +1,37 @@
|
||||
<p>给定一个 n 叉树的根节点<meta charset="UTF-8" /> <code>root</code> ,返回 <em>其节点值的<strong> 后序遍历</strong></em> 。</p>
|
||||
|
||||
<p>n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 <code>null</code> 分隔(请参见示例)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="height: 193px; width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,3,2,4,null,5,6]
|
||||
<strong>输出:</strong>[5,6,3,2,4,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="height: 269px; width: 296px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
<strong>输出:</strong>[2,6,14,11,7,3,12,8,4,13,9,10,5,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>节点总数在范围 <code>[0, 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>n 叉树的高度小于或等于 <code>1000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>递归法很简单,你可以使用迭代法完成此题吗?</p>
|
@@ -0,0 +1,32 @@
|
||||
<p>给定一个 N 叉树,返回其节点值的<em>层序遍历</em>。(即从左到右,逐层遍历)。</p>
|
||||
|
||||
<p>树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="width: 100%; max-width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,3,2,4,null,5,6]
|
||||
<strong>输出:</strong>[[1],[3,2,4],[5,6]]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="width: 296px; height: 241px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
<strong>输出:</strong>[[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树的高度不会超过 <code>1000</code></li>
|
||||
<li>树的节点总数在 <code>[0, 10^4]</code> 之间</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给定一个 N 叉树,找到其最大深度。</p>
|
||||
|
||||
<p class="MachineTrans-lang-zh-CN">最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。</p>
|
||||
|
||||
<p class="MachineTrans-lang-zh-CN">N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。</p>
|
||||
|
||||
<p class="MachineTrans-lang-zh-CN"> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="width: 100%; max-width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,3,2,4,null,5,6]
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="width: 296px; height: 241px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
<strong>输出:</strong>5
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树的深度不会超过 <code>1000</code> 。</li>
|
||||
<li>树的节点数目位于 <code>[0, 10<sup>4</sup>]</code> 之间。</li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>8 间牢房排成一排,每间牢房不是有人住就是空着。</p>
|
||||
|
||||
<p>每天,无论牢房是被占用或空置,都会根据以下规则进行更改:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果一间牢房的两个相邻的房间都被占用或都是空的,那么该牢房就会被占用。</li>
|
||||
<li>否则,它就会被空置。</li>
|
||||
</ul>
|
||||
|
||||
<p>(请注意,由于监狱中的牢房排成一行,所以行中的第一个和最后一个房间无法有两个相邻的房间。)</p>
|
||||
|
||||
<p>我们用以下方式描述监狱的当前状态:如果第 <code>i</code> 间牢房被占用,则 <code>cell[i]==1</code>,否则 <code>cell[i]==0</code>。</p>
|
||||
|
||||
<p>根据监狱的初始状态,在 <code>N</code> 天后返回监狱的状况(和上述 N 种变化)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>cells = [0,1,0,1,1,0,0,1], N = 7
|
||||
<strong>输出:</strong>[0,0,1,1,0,0,0,0]
|
||||
<strong>解释:
|
||||
</strong>下表概述了监狱每天的状况:
|
||||
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
|
||||
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
|
||||
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
|
||||
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
|
||||
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
|
||||
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
|
||||
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
|
||||
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>cells = [1,0,0,1,0,0,1,0], N = 1000000000
|
||||
<strong>输出:</strong>[0,0,1,1,1,1,1,0]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>cells.length == 8</code></li>
|
||||
<li><code>cells[i]</code> 的值为 <code>0</code> 或 <code>1</code> </li>
|
||||
<li><code>1 <= N <= 10^9</code></li>
|
||||
</ol>
|
45
算法题(国内版)/problem (Chinese)/RLE 迭代器 [rle-iterator].html
Normal file
45
算法题(国内版)/problem (Chinese)/RLE 迭代器 [rle-iterator].html
Normal file
@@ -0,0 +1,45 @@
|
||||
<p>我们可以使用游程编码(即 <strong>RLE </strong>)来编码一个整数序列。在偶数长度 <code>encoding</code> ( <strong>从 0 开始</strong> )的游程编码数组中,对于所有偶数 <code>i</code> ,<code>encoding[i]</code> 告诉我们非负整数 <code>encoding[i + 1]</code> 在序列中重复的次数。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,序列 <code>arr = [8,8,8,5,5]</code> 可以被编码为 <code>encoding =[3,8,2,5]</code> 。<code>encoding =[3,8,0,9,2,5]</code> 和 <code>encoding =[2,8,1,8,2,5]</code> 也是 <code>arr</code> 有效的 <strong>RLE</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给定一个游程长度的编码数组,设计一个迭代器来遍历它。</p>
|
||||
|
||||
<p>实现 <code>RLEIterator</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>RLEIterator(int[] encoded)</code> 用编码后的数组初始化对象。</li>
|
||||
<li><code>int next(int n)</code> 以这种方式耗尽后 <code>n</code> 个元素并返回最后一个耗尽的元素。如果没有剩余的元素要耗尽,则返回 <code>-1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:
|
||||
</strong>["RLEIterator","next","next","next","next"]
|
||||
[[[3,8,0,9,2,5]],[2],[1],[1],[2]]
|
||||
<strong>输出:
|
||||
</strong>[null,8,8,5,-1]
|
||||
<strong>解释:</strong>
|
||||
RLEIterator rLEIterator = new RLEIterator([3, 8, 0, 9, 2, 5]); // 这映射到序列 [8,8,8,5,5]。
|
||||
rLEIterator.next(2); // 耗去序列的 2 个项,返回 8。现在剩下的序列是 [8, 5, 5]。
|
||||
rLEIterator.next(1); // 耗去序列的 1 个项,返回 8。现在剩下的序列是 [5, 5]。
|
||||
rLEIterator.next(1); // 耗去序列的 1 个项,返回 5。现在剩下的序列是 [5]。
|
||||
rLEIterator.next(2); // 耗去序列的 2 个项,返回 -1。 这是由于第一个被耗去的项是 5,
|
||||
但第二个项并不存在。由于最后一个要耗去的项不存在,我们返回 -1。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= encoding.length <= 1000</code></li>
|
||||
<li><code>encoding.length</code> 为偶</li>
|
||||
<li><code>0 <= encoding[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
<li>每个测试用例调用<code>next </code>不高于 <code>1000</code> 次 </li>
|
||||
</ul>
|
41
算法题(国内版)/problem (Chinese)/Range 模块 [range-module].html
Normal file
41
算法题(国内版)/problem (Chinese)/Range 模块 [range-module].html
Normal file
@@ -0,0 +1,41 @@
|
||||
<p>Range模块是跟踪数字范围的模块。设计一个数据结构来跟踪表示为 <strong>半开区间</strong> 的范围并查询它们。</p>
|
||||
|
||||
<p><strong>半开区间</strong> <code>[left, right)</code> 表示所有 <code>left <= x < right</code> 的实数 <code>x</code> 。</p>
|
||||
|
||||
<p>实现 <code>RangeModule</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>RangeModule()</code> 初始化数据结构的对象。</li>
|
||||
<li><code>void addRange(int left, int right)</code> 添加 <strong>半开区间</strong> <code>[left, right)</code>,跟踪该区间中的每个实数。添加与当前跟踪的数字部分重叠的区间时,应当添加在区间 <code>[left, right)</code> 中尚未跟踪的任何数字到该区间中。</li>
|
||||
<li><code>boolean queryRange(int left, int right)</code> 只有在当前正在跟踪区间 <code>[left, right)</code> 中的每一个实数时,才返回 <code>true</code> ,否则返回 <code>false</code> 。</li>
|
||||
<li><code>void removeRange(int left, int right)</code> 停止跟踪 <strong>半开区间</strong> <code>[left, right)</code> 中当前正在跟踪的每个实数。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
["RangeModule", "addRange", "removeRange", "queryRange", "queryRange", "queryRange"]
|
||||
[[], [10, 20], [14, 16], [10, 14], [13, 15], [16, 17]]
|
||||
<strong>输出</strong>
|
||||
[null, null, null, true, false, true]
|
||||
|
||||
<strong>解释</strong>
|
||||
RangeModule rangeModule = new RangeModule();
|
||||
rangeModule.addRange(10, 20);
|
||||
rangeModule.removeRange(14, 16);
|
||||
rangeModule.queryRange(10, 14); 返回 true (区间 [10, 14) 中的每个数都正在被跟踪)
|
||||
rangeModule.queryRange(13, 15); 返回 false(未跟踪区间 [13, 15) 中像 14, 14.03, 14.17 这样的数字)
|
||||
rangeModule.queryRange(16, 17); 返回 true (尽管执行了删除操作,区间 [16, 17) 中的数字 16 仍然会被跟踪)
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left < right <= 10<sup>9</sup></code></li>
|
||||
<li>在单个测试用例中,对 <code>addRange</code> 、 <code>queryRange</code> 和 <code>removeRange</code> 的调用总数不超过 <code>10<sup>4</sup></code> 次</li>
|
||||
</ul>
|
36
算法题(国内版)/problem (Chinese)/一手顺子 [hand-of-straights].html
Normal file
36
算法题(国内版)/problem (Chinese)/一手顺子 [hand-of-straights].html
Normal file
@@ -0,0 +1,36 @@
|
||||
<p>Alice 手中有一把牌,她想要重新排列这些牌,分成若干组,使每一组的牌数都是 <code>groupSize</code> ,并且由 <code>groupSize</code> 张连续的牌组成。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>hand</code> 其中 <code>hand[i]</code> 是写在第 <code>i</code> 张牌,和一个整数 <code>groupSize</code> 。如果她可能重新排列这些牌,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>hand = [1,2,3,6,2,3,4,7,8], groupSize = 3
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>Alice 手中的牌可以被重新排列为 <code>[1,2,3],[2,3,4],[6,7,8]</code>。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>hand = [1,2,3,4,5], groupSize = 4
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>Alice 手中的牌无法被重新排列成几个大小为 4 的组。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= hand.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= hand[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= groupSize <= hand.length</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>此题目与 1296 重复:<a href="https://leetcode-cn.com/problems/divide-array-in-sets-of-k-consecutive-numbers/" target="_blank">https://leetcode-cn.com/problems/divide-array-in-sets-of-k-consecutive-numbers/</a></p>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你一个整型数组 <code>nums</code> ,在数组中找出由三个数组成的最大乘积,并输出这个乘积。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3]
|
||||
<strong>输出:</strong>6
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4]
|
||||
<strong>输出:</strong>24
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-1,-2,-3]
|
||||
<strong>输出:</strong>-6
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,找出三个长度为 <code>k</code> 、互不重叠、且全部数字和(<code>3 * k</code> 项)最大的子数组,并返回这三个子数组。</p>
|
||||
|
||||
<p>以下标的数组形式返回结果,数组中的每一项分别指示每个子数组的起始位置(下标从 <strong>0</strong> 开始)。如果有多个结果,返回字典序最小的一个。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1,2,6,7,5,1], k = 2
|
||||
<strong>输出:</strong>[0,3,5]
|
||||
<strong>解释:</strong>子数组 [1, 2], [2, 6], [7, 5] 对应的起始下标为 [0, 3, 5]。
|
||||
也可以取 [2, 1], 但是结果 [1, 3, 5] 在字典序上更大。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1,2,1,2,1,2,1], k = 2
|
||||
<strong>输出:</strong>[0,2,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] < 2<sup>16</sup></code></li>
|
||||
<li><code>1 <= k <= floor(nums.length / 3)</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>给定一个整数数组<meta charset="UTF-8" /> <code>arr</code> ,以及一个整数 <code>target</code> 作为目标值,返回满足 <code>i < j < k</code> 且<meta charset="UTF-8" /> <code>arr[i] + arr[j] + arr[k] == target</code> 的元组 <code>i, j, k</code> 的数量。</p>
|
||||
|
||||
<p>由于结果会非常大,请返回 <code>10<sup>9</sup> + 7</code> 的模。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,1,2,2,3,3,4,4,5,5], target = 8
|
||||
<strong>输出:</strong>20
|
||||
<strong>解释:</strong>
|
||||
按值枚举(arr[i], arr[j], arr[k]):
|
||||
(1, 2, 5) 出现 8 次;
|
||||
(1, 3, 4) 出现 8 次;
|
||||
(2, 2, 4) 出现 2 次;
|
||||
(2, 3, 3) 出现 2 次。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,1,2,2,2,2], target = 5
|
||||
<strong>输出:</strong>12
|
||||
<strong>解释:</strong>
|
||||
arr[i] = 1, arr[j] = arr[k] = 2 出现 12 次:
|
||||
我们从 [1,1] 中选择一个 1,有 2 种情况,
|
||||
从 [2,2,2,2] 中选出两个 2,有 6 种情况。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= arr.length <= 3000</code></li>
|
||||
<li><code>0 <= arr[i] <= 100</code></li>
|
||||
<li><code>0 <= target <= 300</code></li>
|
||||
</ul>
|
46
算法题(国内版)/problem (Chinese)/三等分 [three-equal-parts].html
Normal file
46
算法题(国内版)/problem (Chinese)/三等分 [three-equal-parts].html
Normal file
@@ -0,0 +1,46 @@
|
||||
<p>给定一个由 <code>0</code> 和 <code>1</code> 组成的数组<meta charset="UTF-8" /> <code>arr</code> ,将数组分成 <strong>3 个非空的部分</strong> ,使得所有这些部分表示相同的二进制值。</p>
|
||||
|
||||
<p>如果可以做到,请返回<strong>任何</strong> <code>[i, j]</code>,其中 <code>i+1 < j</code>,这样一来:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>arr[0], arr[1], ..., arr[i]</code> 为第一部分;</li>
|
||||
<li><code>arr[i + 1], arr[i + 2], ..., arr[j - 1]</code> 为第二部分;</li>
|
||||
<li><code>arr[j], arr[j + 1], ..., arr[arr.length - 1]</code> 为第三部分。</li>
|
||||
<li>这三个部分所表示的二进制值相等。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果无法做到,就返回 <code>[-1, -1]</code>。</p>
|
||||
|
||||
<p>注意,在考虑每个部分所表示的二进制时,应当将其看作一个整体。例如,<code>[1,1,0]</code> 表示十进制中的 <code>6</code>,而不会是 <code>3</code>。此外,前导零也是<strong>被允许</strong>的,所以 <code>[0,1,1]</code> 和 <code>[1,1]</code> 表示相同的值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,0,1,0,1]
|
||||
<strong>输出:</strong>[0,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,1,0,1,1]
|
||||
<strong>输出:</strong>[-1,-1]</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,1,0,0,1]
|
||||
<strong>输出:</strong>[0,2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
<meta charset="UTF-8" />
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= arr.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>arr[i]</code> 是 <code>0</code> 或 <code>1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>在<meta charset="UTF-8" /> <code>n x n</code> 的网格<meta charset="UTF-8" /> <code>grid</code> 中,我们放置了一些与 x,y,z 三轴对齐的<meta charset="UTF-8" /> <code>1 x 1 x 1</code> 立方体。</p>
|
||||
|
||||
<p>每个值 <code>v = grid[i][j]</code> 表示 <code>v</code> 个正方体叠放在单元格 <code>(i, j)</code> 上。</p>
|
||||
|
||||
<p>现在,我们查看这些立方体在 <code>xy</code> 、<code>yz</code> 和 <code>zx</code> 平面上的<em>投影</em>。</p>
|
||||
|
||||
<p><strong>投影</strong> 就像影子,将 <strong>三维</strong> 形体映射到一个 <strong>二维</strong> 平面上。从顶部、前面和侧面看立方体时,我们会看到“影子”。</p>
|
||||
|
||||
<p>返回 <em>所有三个投影的总面积</em> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/02/shadow.png" style="height: 214px; width: 800px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>[[1,2],[3,4]]
|
||||
<strong>输出:</strong>17
|
||||
<strong>解释:</strong>这里有该形体在三个轴对齐平面上的三个投影(“阴影部分”)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[2]]
|
||||
<strong>输出:</strong>5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>[[1,0],[0,2]]
|
||||
<strong>输出:</strong>8
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你一个 <code>n * n</code> 的网格 <code>grid</code> ,上面放置着一些 <code>1 x 1 x 1</code> 的正方体。每个值 <code>v = grid[i][j]</code> 表示 <code>v</code> 个正方体叠放在对应单元格 <code>(i, j)</code> 上。</p>
|
||||
|
||||
<p>放置好正方体后,任何直接相邻的正方体都会互相粘在一起,形成一些不规则的三维形体。</p>
|
||||
|
||||
<p>请你返回最终这些形体的总表面积。</p>
|
||||
|
||||
<p><strong>注意:</strong>每个形体的底面也需要计入表面积中。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid2.jpg" style="height: 80px; width: 80px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,2],[3,4]]
|
||||
<strong>输出:</strong>34
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid4.jpg" style="height: 100px; width: 100px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,1,1],[1,0,1],[1,1,1]]
|
||||
<strong>输出:</strong>32
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/08/tmp-grid5.jpg" style="height: 100px; width: 100px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[2,2,2],[2,1,2],[2,2,2]]
|
||||
<strong>输出:</strong>46
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>给定由一些正数(代表长度)组成的数组 <code>nums</code> ,返回 <em>由其中三个长度组成的、<strong>面积不为零</strong>的三角形的最大周长</em> 。如果不能形成任何面积不为零的三角形,返回 <code>0</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,1,2]
|
||||
<strong>输出:</strong>5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,1]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>给你一个正整数 <code>n</code> ,请你找出符合条件的最小整数,其由重新排列 <code>n</code><strong> </strong>中存在的每位数字组成,并且其值大于 <code>n</code> 。如果不存在这样的正整数,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>注意</strong> ,返回的整数应当是一个 <strong>32 位整数</strong> ,如果存在满足题意的答案,但不是 <strong>32 位整数</strong> ,同样返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 12
|
||||
<strong>输出:</strong>21
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 21
|
||||
<strong>输出:</strong>-1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>给你一个 <code>n x n</code> 的<strong> 方形 </strong>整数数组 <code>matrix</code> ,请你找出并返回通过 <code>matrix</code> 的<strong>下降路径</strong><em> </em>的<strong> </strong><strong>最小和</strong> 。</p>
|
||||
|
||||
<p><strong>下降路径</strong> 可以从第一行中的任何元素开始,并从每一行中选择一个元素。在下一行选择的元素和当前行所选元素最多相隔一列(即位于正下方或者沿对角线向左或者向右的第一个元素)。具体来说,位置 <code>(row, col)</code> 的下一个元素应当是 <code>(row + 1, col - 1)</code>、<code>(row + 1, col)</code> 或者 <code>(row + 1, col + 1)</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/11/03/failing1-grid.jpg" style="height: 500px; width: 499px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[2,1,3],[6,5,4],[7,8,9]]
|
||||
<strong>输出:</strong>13
|
||||
<strong>解释:</strong>如图所示,为和最小的两条下降路径
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/11/03/failing2-grid.jpg" style="height: 365px; width: 164px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[-19,57],[-40,-5]]
|
||||
<strong>输出:</strong>-59
|
||||
<strong>解释:</strong>如图所示,为和最小的下降路径
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == matrix.length == matrix[i].length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>-100 <= matrix[i][j] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给定一个字符串 <code>s</code>,计算 <code>s</code> 的 <strong>不同非空子序列</strong> 的个数。因为结果可能很大,所以返回答案需要对<strong> </strong><strong><code>10^9 + 7</code> 取余</strong> 。</p>
|
||||
|
||||
<p>字符串的 <strong>子序列</strong> 是经由原字符串删除一些(也可能不删除)字符但不改变剩余字符相对位置的一个新字符串。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>"ace"</code> 是 <code>"<em><strong>a</strong></em>b<em><strong>c</strong></em>d<em><strong>e</strong></em>"</code> 的一个子序列,但 <code>"aec"</code> 不是。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abc"
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>7 个不同的子序列分别是 "a", "b", "c", "ab", "ac", "bc", 以及 "abc"。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aba"
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>6 个不同的子序列分别是 "a", "b", "ab", "ba", "aa" 以及 "aba"。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aaa"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>3 个不同的子序列分别是 "a", "aa" 以及 "aaa"。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> 仅由小写英文字母组成</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
49
算法题(国内版)/problem (Chinese)/不同路径 III [unique-paths-iii].html
Normal file
49
算法题(国内版)/problem (Chinese)/不同路径 III [unique-paths-iii].html
Normal file
@@ -0,0 +1,49 @@
|
||||
<p>在二维网格 <code>grid</code> 上,有 4 种类型的方格:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1</code> 表示起始方格。且只有一个起始方格。</li>
|
||||
<li><code>2</code> 表示结束方格,且只有一个结束方格。</li>
|
||||
<li><code>0</code> 表示我们可以走过的空方格。</li>
|
||||
<li><code>-1</code> 表示我们无法跨越的障碍。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回在四个方向(上、下、左、右)上行走时,从起始方格到结束方格的不同路径的数目<strong>。</strong></p>
|
||||
|
||||
<p><strong>每一个无障碍方格都要通过一次,但是一条路径中不能重复通过同一个方格</strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>我们有以下两条路径:
|
||||
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
|
||||
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[[1,0,0,0],[0,0,0,0],[0,0,0,2]]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>我们有以下四条路径:
|
||||
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
|
||||
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
|
||||
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
|
||||
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[[0,1],[2,0]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>
|
||||
没有一条路能完全穿过每一个空的方格一次。
|
||||
请注意,起始和结束方格可以位于网格中的任意位置。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= grid.length * grid[0].length <= 20</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给定两个整数 <code>a</code> 和 <code>b</code> ,返回 <strong>任意</strong> 字符串 <code>s</code> ,要求满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>s</code> 的长度为 <code>a + b</code>,且正好包含<code><font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.600000381469727px"><span style="caret-color:#c7254e"><span style="background-color:#f9f2f4">a</span></span></span></font></font></code> 个 <code>'a'</code> 字母与 <code><font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.600000381469727px"><span style="caret-color:#c7254e"><span style="background-color:#f9f2f4">b</span></span></span></font></font></code> 个 <code>'b'</code> 字母;</li>
|
||||
<li>子串 <code>'aaa'</code> 没有出现在 <code>s</code> 中;</li>
|
||||
<li>子串 <code>'bbb'</code> 没有出现在 <code>s</code> 中。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>a = 1, b = 2
|
||||
<strong>输出:</strong>"abb"
|
||||
<strong>解释:</strong>"abb", "bab" 和 "bba" 都是正确答案。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>a = 4, b = 1
|
||||
<strong>输出:</strong>"aabaa"</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= a, b <= 100</code></li>
|
||||
<li>对于给定的 <code>a</code> 和 <code>b</code>,保证存在满足要求的 <code>s</code> </li>
|
||||
</ul>
|
||||
<span style="display:block"><span style="height:0px"><span style="position:absolute"></span></span></span>
|
@@ -0,0 +1,40 @@
|
||||
<p>给定一个正整数 <code>n</code> ,返回范围在 <code>[0, n]</code> 都非负整数中,其二进制表示不包含 <strong>连续的 1 </strong>的个数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> n = 5
|
||||
<strong>输出:</strong> 5
|
||||
<strong>解释:</strong>
|
||||
下面是带有相应二进制表示的非负整数<= 5:
|
||||
0 : 0
|
||||
1 : 1
|
||||
2 : 10
|
||||
3 : 11
|
||||
4 : 100
|
||||
5 : 101
|
||||
其中,只有整数3违反规则(有两个连续的1),其他5个满足规则。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> n = 1
|
||||
<strong>输出:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> n = 2
|
||||
<strong>输出:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。</p>
|
||||
|
||||
<p>你需要帮助他们用<strong>最少的索引和</strong>找出他们<strong>共同喜爱的餐厅</strong>。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设答案总是存在。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"],list2 = ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]
|
||||
<strong>输出:</strong> ["Shogun"]
|
||||
<strong>解释:</strong> 他们唯一共同喜爱的餐厅是“Shogun”。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"],list2 = ["KFC", "Shogun", "Burger King"]
|
||||
<strong>输出:</strong> ["Shogun"]
|
||||
<strong>解释:</strong> 他们共同喜爱且具有最小索引和的餐厅是“Shogun”,它有最小的索引和1(0+1)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= list1.length, list2.length <= 1000</code></li>
|
||||
<li><code>1 <= list1[i].length, list2[i].length <= 30</code> </li>
|
||||
<li><code>list1[i]</code> 和 <code>list2[i]</code> 由空格<meta charset="UTF-8" /> <code>' '</code> 和英文字母组成。</li>
|
||||
<li><code>list1</code> 的所有字符串都是 <strong>唯一</strong> 的。</li>
|
||||
<li><code>list2</code> 中的所有字符串都是 <strong>唯一</strong> 的。</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给定两个单词 <code>word1</code> 和<meta charset="UTF-8" /> <code>word2</code> ,返回使得<meta charset="UTF-8" /> <code>word1</code> 和 <meta charset="UTF-8" /> <code>word2</code><em> </em><strong>相同</strong>所需的<strong>最小步数</strong>。</p>
|
||||
|
||||
<p><strong>每步 </strong>可以删除任意一个字符串中的一个字符。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> word1 = "sea", word2 = "eat"
|
||||
<strong>输出:</strong> 2
|
||||
<strong>解释:</strong> 第一步将 "sea" 变为 "ea" ,第二步将 "eat "变为 "ea"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>word1 = "leetcode", word2 = "etco"
|
||||
<b>输出:</b>4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
<meta charset="UTF-8" />
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word1.length, word2.length <= 500</code></li>
|
||||
<li><code>word1</code> 和 <code>word2</code> 只包含小写英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给定两个字符串<code>s1</code> 和 <code>s2</code>,返回 <em>使两个字符串相等所需删除字符的 <strong>ASCII </strong>值的最小和 </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> s1 = "sea", s2 = "eat"
|
||||
<strong>输出:</strong> 231
|
||||
<strong>解释:</strong> 在 "sea" 中删除 "s" 并将 "s" 的值(115)加入总和。
|
||||
在 "eat" 中删除 "t" 并将 116 加入总和。
|
||||
结束时,两个字符串相等,115 + 116 = 231 就是符合条件的最小和。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> s1 = "delete", s2 = "leet"
|
||||
<strong>输出:</strong> 403
|
||||
<strong>解释:</strong> 在 "delete" 中删除 "dee" 字符串变成 "let",
|
||||
将 100[d]+101[e]+101[e] 加入总和。在 "leet" 中删除 "e" 将 101[e] 加入总和。
|
||||
结束时,两个字符串都等于 "let",结果即为 100+101+101+101 = 403 。
|
||||
如果改为将两个字符串转换为 "lee" 或 "eet",我们会得到 433 或 417 的结果,比答案更大。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= s1.length, s2.length <= 1000</code></li>
|
||||
<li><code>s1</code> 和 <code>s2</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>给出非负整数数组 <code>A</code> ,返回两个非重叠(连续)子数组中元素的最大和,子数组的长度分别为 <code>L</code> 和 <code>M</code>。(这里需要澄清的是,长为 L 的子数组可以出现在长为 M 的子数组之前或之后。)</p>
|
||||
|
||||
<p>从形式上看,返回最大的 <code>V</code>,而 <code>V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1])</code> 并满足下列条件之一:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < i + L - 1 < j < j + M - 1 < A.length</code>, <strong>或</strong></li>
|
||||
<li><code>0 <= j < j + M - 1 < i < i + L - 1 < A.length</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2
|
||||
<strong>输出:</strong>20
|
||||
<strong>解释:</strong>子数组的一种选择中,[9] 长度为 1,[6,5] 长度为 2。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2
|
||||
<strong>输出:</strong>29
|
||||
<strong>解释:</strong>子数组的一种选择中,[3,8,1] 长度为 3,[8,9] 长度为 2。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3
|
||||
<strong>输出:</strong>31
|
||||
<strong>解释:</strong>子数组的一种选择中,[5,6,0,9] 长度为 4,[0,3,8] 长度为 3。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>L >= 1</code></li>
|
||||
<li><code>M >= 1</code></li>
|
||||
<li><code>L + M <= A.length <= 1000</code></li>
|
||||
<li><code>0 <= A[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p><strong>句子</strong> 是一串由空格分隔的单词。每个 <strong>单词</strong><em> </em>仅由小写字母组成。</p>
|
||||
|
||||
<p>如果某个单词在其中一个句子中恰好出现一次,在另一个句子中却 <strong>没有出现</strong> ,那么这个单词就是 <strong>不常见的</strong><em> </em>。</p>
|
||||
|
||||
<p>给你两个 <strong>句子</strong> <code>s1</code> 和 <code>s2</code> ,返回所有 <strong>不常用单词</strong> 的列表。返回列表中单词可以按 <strong>任意顺序</strong> 组织。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s1 = "this apple is sweet", s2 = "this apple is sour"
|
||||
<strong>输出:</strong>["sweet","sour"]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s1 = "apple apple", s2 = "banana"
|
||||
<strong>输出:</strong>["banana"]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length <= 200</code></li>
|
||||
<li><code>s1</code> 和 <code>s2</code> 由小写英文字母和空格组成</li>
|
||||
<li><code>s1</code> 和 <code>s2</code> 都不含前导或尾随空格</li>
|
||||
<li><code>s1</code> 和 <code>s2</code> 中的所有单词间均由单个空格分隔</li>
|
||||
</ul>
|
44
算法题(国内版)/problem (Chinese)/两地调度 [two-city-scheduling].html
Normal file
44
算法题(国内版)/problem (Chinese)/两地调度 [two-city-scheduling].html
Normal file
@@ -0,0 +1,44 @@
|
||||
<p>公司计划面试 <code>2n</code> 人。给你一个数组 <code>costs</code> ,其中 <code>costs[i] = [aCost<sub>i</sub>, bCost<sub>i</sub>]</code> 。第 <code>i</code> 人飞往 <code>a</code> 市的费用为 <code>aCost<sub>i</sub></code> ,飞往 <code>b</code> 市的费用为 <code>bCost<sub>i</sub></code> 。</p>
|
||||
|
||||
<p>返回将每个人都飞到 <code>a</code> 、<code>b</code> 中某座城市的最低费用,要求每个城市都有 <code>n</code> 人抵达<strong>。</strong></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>costs = [[10,20],[30,200],[400,50],[30,20]]
|
||||
<strong>输出:</strong>110
|
||||
<strong>解释:</strong>
|
||||
第一个人去 a 市,费用为 10。
|
||||
第二个人去 a 市,费用为 30。
|
||||
第三个人去 b 市,费用为 50。
|
||||
第四个人去 b 市,费用为 20。
|
||||
|
||||
最低总费用为 10 + 30 + 50 + 20 = 110,每个城市都有一半的人在面试。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>costs = [[259,770],[448,54],[926,667],[184,139],[840,118],[577,469]]
|
||||
<strong>输出:</strong>1859
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>costs = [[515,563],[451,713],[537,709],[343,819],[855,779],[457,60],[650,359],[631,42]]
|
||||
<strong>输出:</strong>3086
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 * n == costs.length</code></li>
|
||||
<li><code>2 <= costs.length <= 100</code></li>
|
||||
<li><code>costs.length</code> 为偶数</li>
|
||||
<li><code>1 <= aCost<sub>i</sub>, bCost<sub>i</sub> <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>给定一个二叉搜索树 <code>root</code> 和一个目标结果 <code>k</code>,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 <code>true</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg" style="height: 229px; width: 400px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [5,3,6,2,4,null,7], k = 9
|
||||
<strong>输出:</strong> true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg" style="height: 229px; width: 400px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [5,3,6,2,4,null,7], k = 28
|
||||
<strong>输出:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树的节点个数的范围是 <code>[1, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li><code>root</code> 为二叉搜索树</li>
|
||||
<li><code>-10<sup>5</sup> <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>你被请来给一个要举办高尔夫比赛的树林砍树。树林由一个 <code>m x n</code> 的矩阵表示, 在这个矩阵中:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0</code> 表示障碍,无法触碰</li>
|
||||
<li><code>1</code> 表示地面,可以行走</li>
|
||||
<li><code>比 1 大的数</code> 表示有树的单元格,可以行走,数值表示树的高度</li>
|
||||
</ul>
|
||||
|
||||
<p>每一步,你都可以向上、下、左、右四个方向之一移动一个单位,如果你站的地方有一棵树,那么你可以决定是否要砍倒它。</p>
|
||||
|
||||
<p>你需要按照树的高度从低向高砍掉所有的树,每砍过一颗树,该单元格的值变为 <code>1</code>(即变为地面)。</p>
|
||||
|
||||
<p>你将从 <code>(0, 0)</code> 点开始工作,返回你砍完所有树需要走的最小步数。 如果你无法砍完所有的树,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p>可以保证的是,没有两棵树的高度是相同的,并且你至少需要砍倒一棵树。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/trees1.jpg" style="width: 242px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>forest = [[1,2,3],[0,0,4],[7,6,5]]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>沿着上面的路径,你可以用 6 步,按从最矮到最高的顺序砍掉这些树。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/26/trees2.jpg" style="width: 242px; height: 242px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>forest = [[1,2,3],[0,0,0],[7,6,5]]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>由于中间一行被障碍阻塞,无法访问最下面一行中的树。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>forest = [[2,3,4],[0,0,5],[8,7,6]]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>可以按与示例 1 相同的路径来砍掉所有的树。
|
||||
(0,0) 位置的树,可以直接砍去,不用算步数。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == forest.length</code></li>
|
||||
<li><code>n == forest[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 50</code></li>
|
||||
<li><code>0 <= forest[i][j] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>几乎每一个人都用 <a href="https://baike.baidu.com/item/%E4%B9%98%E6%B3%95%E8%A1%A8">乘法表</a>。但是你能在乘法表中快速找到第<code>k</code>小的数字吗?</p>
|
||||
|
||||
<p>给定高度<code>m</code> 、宽度<code>n</code> 的一张 <code>m * n</code>的乘法表,以及正整数<code>k</code>,你需要返回表中第<code>k</code> 小的数字。</p>
|
||||
|
||||
<p><strong>例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> m = 3, n = 3, k = 5
|
||||
<strong>输出:</strong> 3
|
||||
<strong>解释:</strong>
|
||||
乘法表:
|
||||
1 2 3
|
||||
2 4 6
|
||||
3 6 9
|
||||
|
||||
第5小的数字是 3 (1, 2, 2, 3, 3).
|
||||
</pre>
|
||||
|
||||
<p><strong>例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> m = 2, n = 3, k = 6
|
||||
<strong>输出:</strong> 6
|
||||
<strong>解释:</strong>
|
||||
乘法表:
|
||||
1 2 3
|
||||
2 4 6
|
||||
|
||||
第6小的数字是 6 (1, 2, 2, 3, 4, 6).
|
||||
</pre>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>m</code> 和 <code>n</code> 的范围在 [1, 30000] 之间。</li>
|
||||
<li><code>k</code> 的范围在 [1, m * n] 之间。</li>
|
||||
</ol>
|
@@ -0,0 +1,30 @@
|
||||
<p>给定一个正整数数组 <code>nums</code>和整数 <code>k</code> 。</p>
|
||||
|
||||
<p>请找出该数组内乘积小于 <code>k</code> 的连续的子数组的个数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [10,5,2,6], k = 100
|
||||
<strong>输出:</strong> 8
|
||||
<strong>解释:</strong> 8个乘积小于100的子数组分别为: [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6]。
|
||||
需要注意的是 [10,5,2] 并不是乘积小于100的子数组。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [1,2,3], k = 0
|
||||
<strong>输出:</strong> 0</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>0 <= k <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给定一个整数数组 <code>prices</code>,其中 <code>prices[i]</code>表示第 <code>i</code> 天的股票价格 ;整数 <code>fee</code> 代表了交易股票的手续费用。</p>
|
||||
|
||||
<p>你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。</p>
|
||||
|
||||
<p>返回获得利润的最大值。</p>
|
||||
|
||||
<p><strong>注意:</strong>这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>prices = [1, 3, 2, 8, 4, 9], fee = 2
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>能够达到的最大利润:
|
||||
在此处买入 prices[0] = 1
|
||||
在此处卖出 prices[3] = 8
|
||||
在此处买入 prices[4] = 4
|
||||
在此处卖出 prices[5] = 9
|
||||
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>prices = [1,3,7,5,10,3], fee = 3
|
||||
<strong>输出:</strong>6
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prices.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= prices[i] < 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= fee < 5 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>给定一个长度为偶数的整数数组 <code>arr</code>,只有对 <code>arr</code> 进行重组后可以满足 “对于每个 <code>0 <= i < len(arr) / 2</code>,都有 <code>arr[2 * i + 1] = 2 * arr[2 * i]</code>” 时,返回 <code>true</code>;否则,返回 <code>false</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [3,1,3,6]
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [2,1,2,6]
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [4,-2,2,-4]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>可以用 [-2,-4] 和 [2,4] 这两组组成 [-2,-4,2,4] 或是 [2,4,-2,-4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>arr.length</code> 是偶数</li>
|
||||
<li><code>-10<sup>5</sup> <= arr[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
26
算法题(国内版)/problem (Chinese)/二分查找 [binary-search].html
Normal file
26
算法题(国内版)/problem (Chinese)/二分查找 [binary-search].html
Normal file
@@ -0,0 +1,26 @@
|
||||
<p>给定一个 <code>n</code> 个元素有序的(升序)整型数组 <code>nums</code> 和一个目标值 <code>target</code> ,写一个函数搜索 <code>nums</code> 中的 <code>target</code>,如果目标值存在返回下标,否则返回 <code>-1</code>。</p>
|
||||
|
||||
<p><br>
|
||||
<strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> <code>nums</code> = [-1,0,3,5,9,12], <code>target</code> = 9
|
||||
<strong>输出:</strong> 4
|
||||
<strong>解释:</strong> 9 出现在 <code>nums</code> 中并且下标为 4
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> <code>nums</code> = [-1,0,3,5,9,12], <code>target</code> = 2
|
||||
<strong>输出:</strong> -1
|
||||
<strong>解释:</strong> 2 不存在 <code>nums</code> 中因此返回 -1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>你可以假设 <code>nums</code> 中的所有元素是不重复的。</li>
|
||||
<li><code>n</code> 将在 <code>[1, 10000]</code>之间。</li>
|
||||
<li><code>nums</code> 的每个元素都将在 <code>[-9999, 9999]</code>之间。</li>
|
||||
</ol>
|
@@ -0,0 +1,40 @@
|
||||
<p>给定二叉搜索树(BST)的根节点<meta charset="UTF-8" /> <code>root</code> 和要插入树中的值<meta charset="UTF-8" /> <code>value</code> ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 <strong>保证</strong> ,新值和原始二叉搜索树中的任意节点值都不同。</p>
|
||||
|
||||
<p><strong>注意</strong>,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 <strong>任意有效的结果</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/05/insertbst.jpg" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [4,2,7,1,3], val = 5
|
||||
<strong>输出:</strong>[4,2,7,1,3,5]
|
||||
<strong>解释:</strong>另一个满足题目要求可以通过的树是:
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/05/bst.jpg" />
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [40,20,60,10,30,50,70], val = 25
|
||||
<strong>输出:</strong>[40,20,60,10,30,50,70,null,null,25]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
|
||||
<strong>输出:</strong>[4,2,7,1,3,5]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中的节点数将在<meta charset="UTF-8" /> <code>[0, 10<sup>4</sup>]</code>的范围内。<meta charset="UTF-8" /></li>
|
||||
<li><code>-10<sup>8</sup> <= Node.val <= 10<sup>8</sup></code></li>
|
||||
<li>所有值 <meta charset="UTF-8" /><code>Node.val</code> 是 <strong>独一无二</strong> 的。</li>
|
||||
<li><code>-10<sup>8</sup> <= val <= 10<sup>8</sup></code></li>
|
||||
<li><strong>保证</strong> <code>val</code> 在原始BST中不存在。</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>给定二叉搜索树(BST)的根节点<meta charset="UTF-8" /> <code>root</code> 和一个整数值<meta charset="UTF-8" /> <code>val</code>。</p>
|
||||
|
||||
<p>你需要在 BST 中找到节点值等于 <code>val</code> 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回<meta charset="UTF-8" /> <code>null</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/01/12/tree1.jpg" style="height: 179px; width: 250px;" /><meta charset="UTF-8" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>root = [4,2,7,1,3], val = 2
|
||||
<b>输出:</b>[2,1,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/12/tree2.jpg" style="height: 179px; width: 250px;" />
|
||||
<pre>
|
||||
<b>输入:</b>root = [4,2,7,1,3], val = 5
|
||||
<b>输出:</b>[]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>数中节点数在 <code>[1, 5000]</code> 范围内</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>7</sup></code></li>
|
||||
<li><code>root</code> 是二叉搜索树</li>
|
||||
<li><code>1 <= val <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
28
算法题(国内版)/problem (Chinese)/二叉搜索树的范围和 [range-sum-of-bst].html
Normal file
28
算法题(国内版)/problem (Chinese)/二叉搜索树的范围和 [range-sum-of-bst].html
Normal file
@@ -0,0 +1,28 @@
|
||||
<p>给定二叉搜索树的根结点 <code>root</code>,返回值位于范围 <em><code>[low, high]</code></em> 之间的所有结点的值的和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg" style="width: 400px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [10,5,15,3,7,null,18], low = 7, high = 15
|
||||
<strong>输出:</strong>32
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg" style="width: 400px; height: 335px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
|
||||
<strong>输出:</strong>23
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数目在范围 <code>[1, 2 * 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
|
||||
<li>所有 <code>Node.val</code> <strong>互不相同</strong></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>给你一个二叉搜索树的根节点 <code>root</code> ,返回 <strong>树中任意两不同节点值之间的最小差值</strong> 。</p>
|
||||
|
||||
<p>差值是一个正数,其数值等于两值之差的绝对值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [4,2,6,1,3]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,0,48,null,null,12,49]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点的数目范围是 <code>[2, 100]</code></li>
|
||||
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>本题与 530:<a href="https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/">https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/</a> 相同</p>
|
||||
</div>
|
||||
</div>
|
@@ -0,0 +1,39 @@
|
||||
<p>给定一个二叉树(具有根结点 <code>root</code>), 一个目标结点 <code>target</code> ,和一个整数值 <code>k</code> 。</p>
|
||||
|
||||
<p>返回到目标结点 <code>target</code> 距离为 <code>k</code> 的所有结点的值的列表。 答案可以以 <strong>任何顺序</strong> 返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png" style="height: 429px; width: 500px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
|
||||
<strong>输出:</strong>[7,4,1]
|
||||
<strong>解释:</strong>所求结点为与目标结点(值为 5)距离为 2 的结点,值分别为 7,4,以及 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [1], target = 1, k = 3
|
||||
<strong>输出:</strong> []
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>节点数在 <code>[1, 500]</code> 范围内</li>
|
||||
<li><code>0 <= Node.val <= 500</code></li>
|
||||
<li><code>Node.val</code> 中所有值 <strong>不同</strong></li>
|
||||
<li>目标结点 <code>target</code> 是树上的结点。</li>
|
||||
<li><code>0 <= k <= 1000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,35 @@
|
||||
<p>给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 <code>2</code> 或 <code>0</code>。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。</p>
|
||||
|
||||
<p>更正式地说,即 <code>root.val = min(root.left.val, root.right.val)</code> 总成立。</p>
|
||||
|
||||
<p>给出这样的一个二叉树,你需要输出所有节点中的 <strong>第二小的值 </strong>。</p>
|
||||
|
||||
<p>如果第二小的值不存在的话,输出 -1 <strong>。</strong></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/smbt1.jpg" style="height: 210px; width: 300px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [2,2,5,null,null,5,7]
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>最小的值是 2 ,第二小的值是 5 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/smbt2.jpg" style="height: 113px; width: 200px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [2,2,2]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>最小的值是 2, 但是不存在第二小的值。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数目在范围 <code>[1, 25]</code> 内</li>
|
||||
<li><code>1 <= Node.val <= 2<sup>31</sup> - 1</code></li>
|
||||
<li>对于树中每个节点 <code>root.val == min(root.left.val, root.right.val)</code></li>
|
||||
</ul>
|
39
算法题(国内版)/problem (Chinese)/二叉树剪枝 [binary-tree-pruning].html
Normal file
39
算法题(国内版)/problem (Chinese)/二叉树剪枝 [binary-tree-pruning].html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>给你二叉树的根结点 <code>root</code> ,此外树的每个结点的值要么是 <code>0</code> ,要么是 <code>1</code> 。</p>
|
||||
|
||||
<p>返回移除了所有不包含 <code>1</code> 的子树的原二叉树。</p>
|
||||
|
||||
<p>节点 <code>node</code> 的子树为 <code>node</code> 本身加上所有 <code>node</code> 的后代。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png" style="width: 500px; height: 140px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,0,0,1]
|
||||
<strong>输出:</strong>[1,null,0,null,1]
|
||||
<strong>解释:</strong>
|
||||
只有红色节点满足条件“所有不包含 1 的子树”。 右图为返回的答案。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png" style="width: 500px; height: 115px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,0,1,0,0,0,1]
|
||||
<strong>输出:</strong>[1,null,1,null,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png" style="width: 500px; height: 134px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,1,0,1,1,0,1,0]
|
||||
<strong>输出:</strong>[1,1,0,1,1,null,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点的数目在范围 <code>[1, 200]</code> 内</li>
|
||||
<li><code>Node.val</code> 为 <code>0</code> 或 <code>1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,66 @@
|
||||
<p>给定一个二叉树,编写一个函数来获取这个树的最大宽度。树的宽度是所有层中的最大宽度。这个二叉树与<strong>满二叉树(full binary tree)</strong>结构相同,但一些节点为空。</p>
|
||||
|
||||
<p>每一层的宽度被定义为两个端点(该层最左和最右的非空节点,两端点间的<code>null</code>节点也计入长度)之间的长度。</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
|
||||
1
|
||||
/ \
|
||||
3 2
|
||||
/ \ \
|
||||
5 3 9
|
||||
|
||||
<strong>输出:</strong> 4
|
||||
<strong>解释:</strong> 最大值出现在树的第 3 层,宽度为 4 (5,3,null,9)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
|
||||
1
|
||||
/
|
||||
3
|
||||
/ \
|
||||
5 3
|
||||
|
||||
<strong>输出:</strong> 2
|
||||
<strong>解释:</strong> 最大值出现在树的第 3 层,宽度为 2 (5,3)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
|
||||
1
|
||||
/ \
|
||||
3 2
|
||||
/
|
||||
5
|
||||
|
||||
<strong>输出:</strong> 2
|
||||
<strong>解释:</strong> 最大值出现在树的第 2 层,宽度为 2 (3,2)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
|
||||
1
|
||||
/ \
|
||||
3 2
|
||||
/ \
|
||||
5 9
|
||||
/ \
|
||||
6 7
|
||||
<strong>输出:</strong> 8
|
||||
<strong>解释:</strong> 最大值出现在树的第 4 层,宽度为 8 (6,null,null,null,null,null,null,7)。
|
||||
</pre>
|
||||
|
||||
<p><strong>注意:</strong> 答案在32位有符号整数的表示范围内。</p>
|
50
算法题(国内版)/problem (Chinese)/二叉树的坡度 [binary-tree-tilt].html
Normal file
50
算法题(国内版)/problem (Chinese)/二叉树的坡度 [binary-tree-tilt].html
Normal file
@@ -0,0 +1,50 @@
|
||||
<p>给你一个二叉树的根节点 <code>root</code> ,计算并返回 <strong>整个树 </strong>的坡度 。</p>
|
||||
|
||||
<p>一个树的<strong> 节点的坡度 </strong>定义即为,该节点左子树的节点之和和右子树节点之和的 <strong>差的绝对值 </strong>。如果没有左子树的话,左子树的节点之和为 0 ;没有右子树的话也是一样。空结点的坡度是 0 。</p>
|
||||
|
||||
<p><strong>整个树</strong> 的坡度就是其所有节点的坡度之和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/20/tilt1.jpg" style="width: 712px; height: 182px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>
|
||||
节点 2 的坡度:|0-0| = 0(没有子节点)
|
||||
节点 3 的坡度:|0-0| = 0(没有子节点)
|
||||
节点 1 的坡度:|2-3| = 1(左子树就是左子节点,所以和是 2 ;右子树就是右子节点,所以和是 3 )
|
||||
坡度总和:0 + 0 + 1 = 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/20/tilt2.jpg" style="width: 800px; height: 203px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [4,2,9,3,5,null,7]
|
||||
<strong>输出:</strong>15
|
||||
<strong>解释:</strong>
|
||||
节点 3 的坡度:|0-0| = 0(没有子节点)
|
||||
节点 5 的坡度:|0-0| = 0(没有子节点)
|
||||
节点 7 的坡度:|0-0| = 0(没有子节点)
|
||||
节点 2 的坡度:|3-5| = 2(左子树就是左子节点,所以和是 3 ;右子树就是右子节点,所以和是 5 )
|
||||
节点 9 的坡度:|0-7| = 7(没有左子树,所以和是 0 ;右子树正好是右子节点,所以和是 7 )
|
||||
节点 4 的坡度:|(3+5+2)-(9+7)| = |10-16| = 6(左子树值为 3、5 和 2 ,和是 10 ;右子树值为 9 和 7 ,和是 16 )
|
||||
坡度总和:0 + 0 + 0 + 2 + 7 + 6 = 15
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/20/tilt3.jpg" style="width: 800px; height: 293px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [21,7,14,1,1,2,2,3,3]
|
||||
<strong>输出:</strong>9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数目的范围在 <code>[0, 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>-1000 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>给你二叉树的根结点 <code>root</code> ,请你设计算法计算二叉树的<em> </em><strong>垂序遍历</strong> 序列。</p>
|
||||
|
||||
<p>对位于 <code>(row, col)</code> 的每个结点而言,其左右子结点分别位于 <code>(row + 1, col - 1)</code> 和 <code>(row + 1, col + 1)</code> 。树的根结点位于 <code>(0, 0)</code> 。</p>
|
||||
|
||||
<p>二叉树的 <strong>垂序遍历</strong> 从最左边的列开始直到最右边的列结束,按列索引每一列上的所有结点,形成一个按出现位置从上到下排序的有序列表。如果同行同列上有多个结点,则按结点的值从小到大进行排序。</p>
|
||||
|
||||
<p>返回二叉树的 <strong>垂序遍历</strong> 序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree1.jpg" style="width: 431px; height: 304px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,9,20,null,null,15,7]
|
||||
<strong>输出:</strong>[[9],[3,15],[20],[7]]
|
||||
<strong>解释:</strong>
|
||||
列 -1 :只有结点 9 在此列中。
|
||||
列 0 :只有结点 3 和 15 在此列中,按从上到下顺序。
|
||||
列 1 :只有结点 20 在此列中。
|
||||
列 2 :只有结点 7 在此列中。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree2.jpg" style="width: 512px; height: 304px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,4,5,6,7]
|
||||
<strong>输出:</strong>[[4],[2],[1,5,6],[3],[7]]
|
||||
<strong>解释:</strong>
|
||||
列 -2 :只有结点 4 在此列中。
|
||||
列 -1 :只有结点 2 在此列中。
|
||||
列 0 :结点 1 、5 和 6 都在此列中。
|
||||
1 在上面,所以它出现在前面。
|
||||
5 和 6 位置都是 (2, 0) ,所以按值从小到大排序,5 在 6 的前面。
|
||||
列 1 :只有结点 3 在此列中。
|
||||
列 2 :只有结点 7 在此列中。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/29/vtree3.jpg" style="width: 512px; height: 304px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,4,6,5,7]
|
||||
<strong>输出:</strong>[[4],[2],[1,5,6],[3],[7]]
|
||||
<strong>解释:</strong>
|
||||
这个示例实际上与示例 2 完全相同,只是结点 5 和 6 在树中的位置发生了交换。
|
||||
因为 5 和 6 的位置仍然相同,所以答案保持不变,仍然按值从小到大排序。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中结点数目总数在范围 <code>[1, 1000]</code> 内</li>
|
||||
<li><code>0 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>在二叉树中,根节点位于深度 <code>0</code> 处,每个深度为 <code>k</code> 的节点的子节点位于深度 <code>k+1</code> 处。</p>
|
||||
|
||||
<p>如果二叉树的两个节点深度相同,但<strong> 父节点不同</strong> ,则它们是一对<em>堂兄弟节点</em>。</p>
|
||||
|
||||
<p>我们给出了具有唯一值的二叉树的根节点 <code>root</code> ,以及树中两个不同节点的值 <code>x</code> 和 <code>y</code> 。</p>
|
||||
|
||||
<p>只有与值 <code>x</code> 和 <code>y</code> 对应的节点是堂兄弟节点时,才返回 <code>true</code> 。否则,返回 <code>false</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:<br />
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/16/q1248-01.png" style="height: 160px; width: 180px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,4], x = 4, y = 3
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:<br />
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/16/q1248-02.png" style="height: 160px; width: 201px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,null,4,null,5], x = 5, y = 4
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/16/q1248-03.png" style="height: 160px; width: 156px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,null,4], x = 2, y = 3
|
||||
<strong>输出:</strong>false</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树的节点数介于 <code>2</code> 到 <code>100</code> 之间。</li>
|
||||
<li>每个节点的值都是唯一的、范围为 <code>1</code> 到 <code>100</code> 的整数。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,34 @@
|
||||
<p>给定一个二叉树的<meta charset="UTF-8" /> <code>root</code> ,确定它是否是一个 <em>完全二叉树</em> 。</p>
|
||||
|
||||
<p>在一个 <strong><a href="https://baike.baidu.com/item/完全二叉树/7773232?fr=aladdin" target="_blank">完全二叉树</a></strong> 中,除了最后一个关卡外,所有关卡都是完全被填满的,并且最后一个关卡中的所有节点都是尽可能靠左的。它可以包含<meta charset="UTF-8" /> <code>1</code> 到<meta charset="UTF-8" /> <code>2<sup>h</sup></code> 节点之间的最后一级 <code>h</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/15/complete-binary-tree-1.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,4,5,6]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>最后一层前的每一层都是满的(即,结点值为 {1} 和 {2,3} 的两层),且最后一层中的所有结点({4,5,6})都尽可能地向左。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/15/complete-binary-tree-2.png" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,4,5,null,7]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>值为 7 的结点没有尽可能靠向左侧。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树的结点数在范围 <meta charset="UTF-8" /> <code>[1, 100]</code> 内。</li>
|
||||
<li><code>1 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给定一个非空二叉树的根节点<meta charset="UTF-8" /> <code>root</code> , 以数组的形式返回每一层节点的平均值。与实际答案相差 <code>10<sup>-5</sup></code> 以内的答案可以被接受。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/avg1-tree.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,9,20,null,null,15,7]
|
||||
<strong>输出:</strong>[3.00000,14.50000,11.00000]
|
||||
<strong>解释:</strong>第 0 层的平均值为 3,第 1 层的平均值为 14.5,第 2 层的平均值为 11 。
|
||||
因此返回 [3, 14.5, 11] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/avg2-tree.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,9,20,15,7]
|
||||
<strong>输出:</strong>[3.00000,14.50000,11.00000]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<p><meta charset="UTF-8" /></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数量在 <code>[1, 10<sup>4</sup>]</code> 范围内</li>
|
||||
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>给你两个整数 <code>left</code> 和 <code>right</code> ,在闭区间 <code>[left, right]</code> 范围内,统计并返回 <strong>计算置位位数为质数</strong> 的整数个数。</p>
|
||||
|
||||
<p><strong>计算置位位数</strong> 就是二进制表示中 <code>1</code> 的个数。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如, <code>21</code> 的二进制表示 <code>10101</code> 有 <code>3</code> 个计算置位。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>left = 6, right = 10
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
6 -> 110 (2 个计算置位,2 是质数)
|
||||
7 -> 111 (3 个计算置位,3 是质数)
|
||||
9 -> 1001 (2 个计算置位,2 是质数)
|
||||
10-> 1010 (2 个计算置位,2 是质数)
|
||||
共计 4 个计算置位为质数的数字。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>left = 10, right = 15
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>
|
||||
10 -> 1010 (2 个计算置位, 2 是质数)
|
||||
11 -> 1011 (3 个计算置位, 3 是质数)
|
||||
12 -> 1100 (2 个计算置位, 2 是质数)
|
||||
13 -> 1101 (3 个计算置位, 3 是质数)
|
||||
14 -> 1110 (3 个计算置位, 3 是质数)
|
||||
15 -> 1111 (4 个计算置位, 4 不是质数)
|
||||
共计 5 个计算置位为质数的数字。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= left <= right <= 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= right - left <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
45
算法题(国内版)/problem (Chinese)/二进制间距 [binary-gap].html
Normal file
45
算法题(国内版)/problem (Chinese)/二进制间距 [binary-gap].html
Normal file
@@ -0,0 +1,45 @@
|
||||
<p>给定一个正整数 <code>n</code>,找到并返回 <code>n</code> 的二进制表示中两个 <strong>相邻</strong> 1 之间的<strong> 最长距离 </strong>。如果不存在两个相邻的 1,返回 <code>0</code> 。</p>
|
||||
|
||||
<p>如果只有 <code>0</code> 将两个 <code>1</code> 分隔开(可能不存在 <code>0</code> ),则认为这两个 1 彼此 <strong>相邻</strong> 。两个 <code>1</code> 之间的距离是它们的二进制表示中位置的绝对差。例如,<code>"1001"</code> 中的两个 <code>1</code> 的距离为 3 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 22
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>22 的二进制是 "10110" 。
|
||||
在 22 的二进制表示中,有三个 1,组成两对相邻的 1 。
|
||||
第一对相邻的 1 中,两个 1 之间的距离为 2 。
|
||||
第二对相邻的 1 中,两个 1 之间的距离为 1 。
|
||||
答案取两个距离之中最大的,也就是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 8
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>8 的二进制是 "1000" 。
|
||||
在 8 的二进制表示中没有相邻的两个 1,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>5 的二进制是 "101" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给定一个正整数,检查它的二进制表示是否总是 0、1 交替出现:换句话说,就是二进制表示中相邻两位的数字永不相同。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>5 的二进制表示是:101
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 7
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>7 的二进制表示是:111.</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 11
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>11 的二进制表示是:1011.</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
40
算法题(国内版)/problem (Chinese)/亲密字符串 [buddy-strings].html
Normal file
40
算法题(国内版)/problem (Chinese)/亲密字符串 [buddy-strings].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>给你两个字符串 <code>s</code> 和 <code>goal</code> ,只要我们可以通过交换 <code>s</code> 中的两个字母得到与 <code>goal</code> 相等的结果,就返回 <code>true</code> ;否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p>交换字母的定义是:取两个下标 <code>i</code> 和 <code>j</code> (下标从 <code>0</code> 开始)且满足 <code>i != j</code> ,接着交换 <code>s[i]</code> 和 <code>s[j]</code> 处的字符。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,在 <code>"abcd"</code> 中交换下标 <code>0</code> 和下标 <code>2</code> 的元素可以生成 <code>"cbad"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "ab", goal = "ba"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>你可以交换 s[0] = 'a' 和 s[1] = 'b' 生成 "ba",此时 s 和 goal 相等。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "ab", goal = "ab"
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>你只能交换 s[0] = 'a' 和 s[1] = 'b' 生成 "ba",此时 s 和 goal 不相等。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aa", goal = "aa"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>你可以交换 s[0] = 'a' 和 s[1] = 'a' 生成 "aa",此时 s 和 goal 相等。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length, goal.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> 和 <code>goal</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你一个字符串 <code>s</code> ,根据下述规则反转字符串:</p>
|
||||
|
||||
<ul>
|
||||
<li>所有非英文字母保留在原有位置。</li>
|
||||
<li>所有英文字母(小写或大写)位置反转。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回反转后的 <code>s</code><em> 。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "ab-cd"
|
||||
<strong>输出:</strong>"dc-ba"
|
||||
</pre>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "a-bC-dEf-ghIj"
|
||||
<strong>输出:</strong>"j-Ih-gfE-dCba"
|
||||
</pre>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "Test1ng-Leet=code-Q!"
|
||||
<strong>输出:</strong>"Qedo1ct-eeLg=ntse-T!"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> 仅由 ASCII 值在范围 <code>[33, 122]</code> 的字符组成</li>
|
||||
<li><code>s</code> 不含 <code>'\"'</code> 或 <code>'\\'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>我们从二叉树的根节点 <code>root</code> 开始进行深度优先搜索。</p>
|
||||
|
||||
<p>在遍历中的每个节点处,我们输出 <code>D</code> 条短划线(其中 <code>D</code> 是该节点的深度),然后输出该节点的值。(<em>如果节点的深度为 <code>D</code>,则其直接子节点的深度为 <code>D + 1</code>。根节点的深度为 <code>0</code>)。</em></p>
|
||||
|
||||
<p>如果节点只有一个子节点,那么保证该子节点为左子节点。</p>
|
||||
|
||||
<p>给出遍历输出 <code>S</code>,还原树并返回其根节点 <code>root</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/04/12/recover-a-tree-from-preorder-traversal.png" style="height: 200px; width: 320px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>"1-2--3--4-5--6--7"
|
||||
<strong>输出:</strong>[1,2,5,3,4,6,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/04/12/screen-shot-2019-04-10-at-114101-pm.png" style="height: 250px; width: 256px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>"1-2--3---4-5--6---7"
|
||||
<strong>输出:</strong>[1,2,5,3,null,6,null,4,null,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/04/12/screen-shot-2019-04-10-at-114955-pm.png" style="height: 250px; width: 276px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>"1-401--349---90--88"
|
||||
<strong>输出:</strong>[1,401,null,349,88,90]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>原始树中的节点数介于 <code>1</code> 和 <code>1000</code> 之间。</li>
|
||||
<li>每个节点的值介于 <code>1</code> 和 <code>10 ^ 9</code> 之间。</li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>给定一颗根结点为 <code>root</code> 的二叉树,树中的每一个结点都有一个 <code>[0, 25]</code> 范围内的值,分别代表字母 <code>'a'</code> 到 <code>'z'</code>。</p>
|
||||
|
||||
<p>返回 <em><strong>按字典序最小</strong> 的字符串,该字符串从这棵树的一个叶结点开始,到根结点结束</em>。</p>
|
||||
|
||||
<blockquote>
|
||||
<p>注<strong>:</strong>字符串中任何较短的前缀在 <strong>字典序上</strong> 都是 <strong>较小</strong> 的:</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,在字典序上 <code>"ab"</code> 比 <code>"aba"</code> 要小。叶结点是指没有子结点的结点。 </li>
|
||||
</ul>
|
||||
</blockquote>
|
||||
|
||||
<p>节点的叶节点是没有子节点的节点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/02/02/tree1.png" style="height: 358px; width: 534px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [0,1,2,3,4,3,4]
|
||||
<strong>输出:</strong>"dba"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2019/01/30/tree2.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [25,1,3,1,3,0,2]
|
||||
<strong>输出:</strong>"adz"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2019/02/01/tree3.png" style="height: 513px; width: 490px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [2,2,1,null,1,0,null,0]
|
||||
<strong>输出:</strong>"abc"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>给定树的结点数在 <code>[1, 8500]</code> 范围内</li>
|
||||
<li><code>0 <= Node.val <= 25</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>给出一棵二叉树,其上每个结点的值都是 <code>0</code> 或 <code>1</code> 。每一条从根到叶的路径都代表一个从最高有效位开始的二进制数。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,如果路径为 <code>0 -> 1 -> 1 -> 0 -> 1</code>,那么它表示二进制数 <code>01101</code>,也就是 <code>13</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>对树上的每一片叶子,我们都要找出从根到该叶子的路径所表示的数字。</p>
|
||||
|
||||
<p>返回这些数字之和。题目数据保证答案是一个 <strong>32 位 </strong>整数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/04/sum-of-root-to-leaf-binary-numbers.png" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,0,1,0,1,0,1]
|
||||
<strong>输出:</strong>22
|
||||
<strong>解释:</strong>(100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [0]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中的节点数在 <code>[1, 1000]</code> 范围内</li>
|
||||
<li><code>Node.val</code> 仅为 <code>0</code> 或 <code>1</code> </li>
|
||||
</ul>
|
51
算法题(国内版)/problem (Chinese)/令牌放置 [bag-of-tokens].html
Normal file
51
算法题(国内版)/problem (Chinese)/令牌放置 [bag-of-tokens].html
Normal file
@@ -0,0 +1,51 @@
|
||||
<p>你的初始 <strong>能量</strong> 为 <code>P</code>,初始 <strong>分数</strong> 为 <code>0</code>,只有一包令牌 <code>tokens</code> 。其中 <code>tokens[i]</code> 是第 <code>i</code> 个令牌的值(下标从 0 开始)。</p>
|
||||
|
||||
<p>令牌可能的两种使用方法如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果你至少有 <code>token[i]</code> 点 <strong>能量</strong> ,可以将令牌 <code>i</code> 置为正面朝上,失去 <code>token[i]</code> 点 <strong>能量</strong> ,并得到 <code>1</code> <strong>分</strong> 。</li>
|
||||
<li>如果我们至少有 <code>1</code> <strong>分 </strong>,可以将令牌 <code>i</code> 置为反面朝上,获得 <code>token[i]</code> 点 <strong>能量</strong> ,并失去 <code>1</code> <strong>分</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>每个令牌 <strong>最多</strong> 只能使用一次,使用 <strong>顺序不限</strong> ,<strong>不需</strong> 使用所有令牌。</p>
|
||||
|
||||
<p>在使用任意数量的令牌后,返回我们可以得到的最大 <strong>分数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>tokens = [100], P = 50
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>无法使用唯一的令牌,因为能量和分数都太少了。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>tokens = [100,200], P = 150
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>令牌 0 正面朝上,能量变为 50,分数变为 1 。不必使用令牌 1 ,因为你无法使用它来提高分数。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>tokens = [100,200,300,400], P = 200
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>按下面顺序使用令牌可以得到 2 分:
|
||||
1. 令牌 0 正面朝上,能量变为 100 ,分数变为 1
|
||||
2. 令牌 3 正面朝下,能量变为 500 ,分数变为 0
|
||||
3. 令牌 1 正面朝上,能量变为 300 ,分数变为 1
|
||||
4. 令牌 2 正面朝上,能量变为 0 ,分数变为 2</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= tokens.length <= 1000</code></li>
|
||||
<li><code>0 <= tokens[i], P < 10<sup>4</sup></code></li>
|
||||
</ul>
|
47
算法题(国内版)/problem (Chinese)/任务调度器 [task-scheduler].html
Normal file
47
算法题(国内版)/problem (Chinese)/任务调度器 [task-scheduler].html
Normal file
@@ -0,0 +1,47 @@
|
||||
<p>给你一个用字符数组 <code>tasks</code> 表示的 CPU 需要执行的任务列表。其中每个字母表示一种不同种类的任务。任务可以以任意顺序执行,并且每个任务都可以在 1 个单位时间内执行完。在任何一个单位时间,CPU 可以完成一个任务,或者处于待命状态。</p>
|
||||
|
||||
<p>然而,两个<strong> 相同种类</strong> 的任务之间必须有长度为整数<strong> </strong><code>n</code><strong> </strong>的冷却时间,因此至少有连续 <code>n</code> 个单位时间内 CPU 在执行不同的任务,或者在待命状态。</p>
|
||||
|
||||
<p>你需要计算完成所有任务所需要的<strong> 最短时间</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>tasks = ["A","A","A","B","B","B"], n = 2
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>A -> B -> (待命) -> A -> B -> (待命) -> A -> B
|
||||
在本示例中,两个相同类型任务之间必须间隔长度为 n = 2 的冷却时间,而执行一个任务只需要一个单位时间,所以中间出现了(待命)状态。 </pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>tasks = ["A","A","A","B","B","B"], n = 0
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>在这种情况下,任何大小为 6 的排列都可以满足要求,因为 n = 0
|
||||
["A","A","A","B","B","B"]
|
||||
["A","B","A","B","A","B"]
|
||||
["B","B","B","A","A","A"]
|
||||
...
|
||||
诸如此类
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
|
||||
<strong>输出:</strong>16
|
||||
<strong>解释:</strong>一种可能的解决方案是:
|
||||
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> (待命) -> (待命) -> A -> (待命) -> (待命) -> A
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= task.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>tasks[i]</code> 是大写英文字母</li>
|
||||
<li><code>n</code> 的取值范围为 <code>[0, 100]</code></li>
|
||||
</ul>
|
27
算法题(国内版)/problem (Chinese)/优势洗牌 [advantage-shuffle].html
Normal file
27
算法题(国内版)/problem (Chinese)/优势洗牌 [advantage-shuffle].html
Normal file
@@ -0,0 +1,27 @@
|
||||
<p>给定两个大小相等的数组 <code>A</code> 和 <code>B</code>,A 相对于 B 的<em>优势</em>可以用满足 <code>A[i] > B[i]</code> 的索引 <code>i</code> 的数目来描述。</p>
|
||||
|
||||
<p>返回 <code>A</code> 的<strong>任意</strong>排列,使其相对于 <code>B</code> 的优势最大化。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>A = [2,7,11,15], B = [1,10,4,11]
|
||||
<strong>输出:</strong>[2,11,7,15]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>A = [12,24,8,32], B = [13,25,32,11]
|
||||
<strong>输出:</strong>[24,32,8,12]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= A.length = B.length <= 10000</code></li>
|
||||
<li><code>0 <= A[i] <= 10^9</code></li>
|
||||
<li><code>0 <= B[i] <= 10^9</code></li>
|
||||
</ol>
|
@@ -0,0 +1,35 @@
|
||||
<p>给你两个整数 <code>n</code> 和 <code>k</code> ,请你构造一个答案列表 <code>answer</code> ,该列表应当包含从 <code>1</code> 到 <code>n</code> 的 <code>n</code> 个不同正整数,并同时满足下述条件:</p>
|
||||
|
||||
<ul>
|
||||
<li>假设该列表是 <code>answer = [a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>, ... , a<sub>n</sub>]</code> ,那么列表 <code>[|a<sub>1</sub> - a<sub>2</sub>|, |a<sub>2</sub> - a<sub>3</sub>|, |a<sub>3</sub> - a<sub>4</sub>|, ... , |a<sub>n-1</sub> - a<sub>n</sub>|]</code> 中应该有且仅有 <code>k</code> 个不同整数。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回列表 <code>answer</code> 。如果存在多种答案,只需返回其中 <strong>任意一种</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, k = 1
|
||||
<strong>输出:</strong>[1, 2, 3]
|
||||
<strong>解释:</strong>[1, 2, 3] 包含 3 个范围在 1-3 的不同整数,并且 [1, 1] 中有且仅有 1 个不同整数:1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, k = 2
|
||||
<strong>输出:</strong>[1, 3, 2]
|
||||
<strong>解释:</strong>[1, 3, 2] 包含 3 个范围在 1-3 的不同整数,并且 [2, 1] 中有且仅有 2 个不同整数:1 和 2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k < n <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,54 @@
|
||||
表:<code>Stadium</code>
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| id | int |
|
||||
| visit_date | date |
|
||||
| people | int |
|
||||
+---------------+---------+
|
||||
visit_date 是表的主键
|
||||
每日人流量信息被记录在这三列信息中:<strong>序号</strong> (id)、<strong>日期</strong> (visit_date)、 <strong>人流量</strong> (people)
|
||||
每天只有一行记录,日期随着 id 的增加而增加
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写一个 SQL 查询以找出每行的人数大于或等于 <code>100</code> 且 <code>id</code> 连续的三行或更多行记录。</p>
|
||||
|
||||
<p>返回按 <code>visit_date</code> <strong>升序排列</strong> 的结果表。</p>
|
||||
|
||||
<p>查询结果格式如下所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<code><strong>输入:</strong>
|
||||
Stadium</code> 表:
|
||||
+------+------------+-----------+
|
||||
| id | visit_date | people |
|
||||
+------+------------+-----------+
|
||||
| 1 | 2017-01-01 | 10 |
|
||||
| 2 | 2017-01-02 | 109 |
|
||||
| 3 | 2017-01-03 | 150 |
|
||||
| 4 | 2017-01-04 | 99 |
|
||||
| 5 | 2017-01-05 | 145 |
|
||||
| 6 | 2017-01-06 | 1455 |
|
||||
| 7 | 2017-01-07 | 199 |
|
||||
| 8 | 2017-01-09 | 188 |
|
||||
+------+------------+-----------+
|
||||
<strong>输出:</strong>
|
||||
+------+------------+-----------+
|
||||
| id | visit_date | people |
|
||||
+------+------------+-----------+
|
||||
| 5 | 2017-01-05 | 145 |
|
||||
| 6 | 2017-01-06 | 1455 |
|
||||
| 7 | 2017-01-07 | 199 |
|
||||
| 8 | 2017-01-09 | 188 |
|
||||
+------+------------+-----------+
|
||||
<strong>解释:
|
||||
id</strong> 为 5、6、7、8 的四行 id 连续,并且每行都有 >= 100 的人数记录。
|
||||
请注意,即使第 7 行和第 8 行的 visit_date 不是连续的,输出也应当包含第 8 行,因为我们只需要考虑 id 连续的记录。
|
||||
不输出 id 为 2 和 3 的行,因为至少需要三条 id 连续的记录。</pre>
|
@@ -0,0 +1,44 @@
|
||||
<p>我们有两个长度相等且不为空的整型数组 <code>nums1</code> 和 <code>nums2</code> 。在一次操作中,我们可以交换 <code>nums1[i]</code> 和 <code>nums2[i]</code>的元素。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,如果 <code>nums1 = [1,2,3,<u>8</u>]</code> , <code>nums2 =[5,6,7,<u>4</u>]</code> ,你可以交换 <code>i = 3</code> 处的元素,得到 <code>nums1 =[1,2,3,4]</code> 和 <code>nums2 =[5,6,7,8]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回 <em>使 <code>nums1</code> 和 <code>nums2</code> <strong>严格递增 </strong>所需操作的最小次数</em> 。</p>
|
||||
|
||||
<p>数组 <code>arr</code> <strong>严格递增</strong> 且 <code>arr[0] < arr[1] < arr[2] < ... < arr[arr.length - 1]</code> 。</p>
|
||||
|
||||
<p><b>注意:</b></p>
|
||||
|
||||
<ul>
|
||||
<li>用例保证可以实现操作。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums1 = [1,3,5,4], nums2 = [1,2,3,7]
|
||||
<strong>输出:</strong> 1
|
||||
<strong>解释: </strong>
|
||||
交换 A[3] 和 B[3] 后,两个数组如下:
|
||||
A = [1, 3, 5, 7] , B = [1, 2, 3, 4]
|
||||
两个数组均为严格递增的。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9]
|
||||
<strong>输出:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums1.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums2.length == nums1.length</code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 2 * 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>只有满足下面几点之一,括号字符串才是有效的:</p>
|
||||
|
||||
<ul>
|
||||
<li>它是一个空字符串,或者</li>
|
||||
<li>它可以被写成 <code>AB</code> (<code>A</code> 与 <code>B</code> 连接), 其中 <code>A</code> 和 <code>B</code> 都是有效字符串,或者</li>
|
||||
<li>它可以被写作 <code>(A)</code>,其中 <code>A</code> 是有效字符串。</li>
|
||||
</ul>
|
||||
|
||||
<p>给定一个括号字符串 <code>s</code> ,移动N次,你就可以在字符串的任何位置插入一个括号。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,如果 <code>s = "()))"</code> ,你可以插入一个开始括号为 <code>"(()))"</code> 或结束括号为 <code>"())))"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回 <em>为使结果字符串 <code>s</code> 有效而必须添加的最少括号数</em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "())"
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "((("
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> 只包含 <code>'('</code> 和 <code>')'</code> 字符。</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 。每次 move 操作将会选择任意一个满足 <code>0 <= i < nums.length</code> 的下标 <code>i</code>,并将 <code>nums[i]</code> 递增 <code>1</code>。</p>
|
||||
|
||||
<p>返回使 <code>nums</code> 中的每个值都变成唯一的所需要的最少操作次数。</p>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,2]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>经过一次 <em>move</em> 操作,数组将变为 [1, 2, 3]。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,2,1,2,1,7]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>经过 6 次 <em>move</em> 操作,数组将变为 [3, 4, 1, 2, 5, 7]。
|
||||
可以看出 5 次或 5 次以下的 <em>move</em> 操作是不能让数组的每个值唯一的。</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<strong>提示:</strong>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个整数数组 <code>cost</code> ,其中 <code>cost[i]</code> 是从楼梯第 <code>i</code> 个台阶向上爬需要支付的费用。一旦你支付此费用,即可选择向上爬一个或者两个台阶。</p>
|
||||
|
||||
<p>你可以选择从下标为 <code>0</code> 或下标为 <code>1</code> 的台阶开始爬楼梯。</p>
|
||||
|
||||
<p>请你计算并返回达到楼梯顶部的最低花费。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>cost = [10,<em><strong>15</strong></em>,20]
|
||||
<strong>输出:</strong>15
|
||||
<strong>解释:</strong>你将从下标为 1 的台阶开始。
|
||||
- 支付 15 ,向上爬两个台阶,到达楼梯顶部。
|
||||
总花费为 15 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>cost = [<em><strong>1</strong></em>,100,<em><strong>1</strong></em>,1,<em><strong>1</strong></em>,100,<em><strong>1</strong></em>,<em><strong>1</strong></em>,100,<em><strong>1</strong></em>]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>你将从下标为 0 的台阶开始。
|
||||
- 支付 1 ,向上爬两个台阶,到达下标为 2 的台阶。
|
||||
- 支付 1 ,向上爬两个台阶,到达下标为 4 的台阶。
|
||||
- 支付 1 ,向上爬两个台阶,到达下标为 6 的台阶。
|
||||
- 支付 1 ,向上爬一个台阶,到达下标为 7 的台阶。
|
||||
- 支付 1 ,向上爬两个台阶,到达下标为 9 的台阶。
|
||||
- 支付 1 ,向上爬一个台阶,到达楼梯顶部。
|
||||
总花费为 6 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= cost.length <= 1000</code></li>
|
||||
<li><code>0 <= cost[i] <= 999</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一座由 <code>n x n</code> 个街区组成的城市,每个街区都包含一座立方体建筑。给你一个下标从 <strong>0</strong> 开始的 <code>n x n</code> 整数矩阵 <code>grid</code> ,其中 <code>grid[r][c]</code> 表示坐落于 <code>r</code> 行 <code>c</code> 列的建筑物的 <strong>高度</strong> 。</p>
|
||||
|
||||
<p>城市的 <strong>天际线</strong> 是从远处观察城市时,所有建筑物形成的外部轮廓。从东、南、西、北四个主要方向观测到的 <strong>天际线</strong> 可能不同。</p>
|
||||
|
||||
<p>我们被允许为 <strong>任意数量的建筑物 </strong>的高度增加<strong> 任意增量(不同建筑物的增量可能不同)</strong> 。 高度为 <code>0</code> 的建筑物的高度也可以增加。然而,增加的建筑物高度 <strong>不能影响</strong> 从任何主要方向观察城市得到的 <strong>天际线</strong> 。</p>
|
||||
|
||||
<p>在 <strong>不改变</strong> 从任何主要方向观测到的城市 <strong>天际线</strong> 的前提下,返回建筑物可以增加的 <strong>最大高度增量总和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/21/807-ex1.png" style="width: 700px; height: 603px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
|
||||
<strong>输出:</strong>35
|
||||
<strong>解释:</strong>建筑物的高度如上图中心所示。
|
||||
用红色绘制从不同方向观看得到的天际线。
|
||||
在不影响天际线的情况下,增加建筑物的高度:
|
||||
gridNew = [ [8, 4, 8, 7],
|
||||
[7, 4, 7, 7],
|
||||
[9, 4, 8, 7],
|
||||
[3, 3, 3, 3] ]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,0,0],[0,0,0],[0,0,0]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>增加任何建筑物的高度都会导致天际线的变化。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length</code></li>
|
||||
<li><code>n == grid[r].length</code></li>
|
||||
<li><code>2 <= n <= 50</code></li>
|
||||
<li><code>0 <= grid[r][c] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>给你二叉搜索树的根节点 <code>root</code> ,同时给定最小边界<code>low</code> 和最大边界 <code>high</code>。通过修剪二叉搜索树,使得所有节点的值在<code>[low, high]</code>中。修剪树 <strong>不应该</strong> 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 <strong>唯一的答案</strong> 。</p>
|
||||
|
||||
<p>所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg" style="height: 126px; width: 450px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,0,2], low = 1, high = 2
|
||||
<strong>输出:</strong>[1,null,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg" style="height: 277px; width: 450px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,0,4,null,2,null,null,1], low = 1, high = 3
|
||||
<strong>输出:</strong>[3,2,null,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数在范围 <code>[1, 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>树中每个节点的值都是 <strong>唯一</strong> 的</li>
|
||||
<li>题目数据保证输入是一棵有效的二叉搜索树</li>
|
||||
<li><code>0 <= low <= high <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
57
算法题(国内版)/problem (Chinese)/元音拼写检查器 [vowel-spellchecker].html
Normal file
57
算法题(国内版)/problem (Chinese)/元音拼写检查器 [vowel-spellchecker].html
Normal file
@@ -0,0 +1,57 @@
|
||||
<p>在给定单词列表 <code>wordlist</code> 的情况下,我们希望实现一个拼写检查器,将查询单词转换为正确的单词。</p>
|
||||
|
||||
<p>对于给定的查询单词 <code>query</code>,拼写检查器将会处理两类拼写错误:</p>
|
||||
|
||||
<ul>
|
||||
<li>大小写:如果查询匹配单词列表中的某个单词(<strong>不区分大小写</strong>),则返回的正确单词与单词列表中的大小写相同。
|
||||
|
||||
<ul>
|
||||
<li>例如:<code>wordlist = ["yellow"]</code>, <code>query = "YellOw"</code>: <code>correct = "yellow"</code></li>
|
||||
<li>例如:<code>wordlist = ["Yellow"]</code>, <code>query = "yellow"</code>: <code>correct = "Yellow"</code></li>
|
||||
<li>例如:<code>wordlist = ["yellow"]</code>, <code>query = "yellow"</code>: <code>correct = "yellow"</code></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>元音错误:如果在将查询单词中的元音 <code>('a', 'e', 'i', 'o', 'u')</code> 分别替换为任何元音后,能与单词列表中的单词匹配(<strong>不区分大小写</strong>),则返回的正确单词与单词列表中的匹配项大小写相同。
|
||||
<ul>
|
||||
<li>例如:<code>wordlist = ["YellOw"]</code>, <code>query = "yollow"</code>: <code>correct = "YellOw"</code></li>
|
||||
<li>例如:<code>wordlist = ["YellOw"]</code>, <code>query = "yeellow"</code>: <code>correct = ""</code> (无匹配项)</li>
|
||||
<li>例如:<code>wordlist = ["YellOw"]</code>, <code>query = "yllw"</code>: <code>correct = ""</code> (无匹配项)</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>此外,拼写检查器还按照以下优先级规则操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>当查询完全匹配单词列表中的某个单词(<strong>区分大小写</strong>)时,应返回相同的单词。</li>
|
||||
<li>当查询匹配到大小写问题的单词时,您应该返回单词列表中的第一个这样的匹配项。</li>
|
||||
<li>当查询匹配到元音错误的单词时,您应该返回单词列表中的第一个这样的匹配项。</li>
|
||||
<li>如果该查询在单词列表中没有匹配项,则应返回空字符串。</li>
|
||||
</ul>
|
||||
|
||||
<p>给出一些查询 <code>queries</code>,返回一个单词列表 <code>answer</code>,其中 <code>answer[i]</code> 是由查询 <code>query = queries[i]</code> 得到的正确单词。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
|
||||
<strong>输出:</strong>["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>wordlist = ["yellow"], queries = ["YellOw"]
|
||||
<b>输出:</b>["yellow"]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= wordlist.length, queries.length <= 5000</code></li>
|
||||
<li><code>1 <= wordlist[i].length, queries[i].length <= 7</code></li>
|
||||
<li><code>wordlist[i]</code> 和 <code>queries[i]</code> 只包含英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> ,表示由范围 <code>[0, n - 1]</code> 内所有整数组成的一个排列。</p>
|
||||
|
||||
<p><strong>全局倒置</strong> 的数目等于满足下述条件不同下标对 <code>(i, j)</code> 的数目:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j < n</code></li>
|
||||
<li><code>nums[i] > nums[j]</code></li>
|
||||
</ul>
|
||||
|
||||
<p><strong>局部倒置</strong> 的数目等于满足下述条件的下标 <code>i</code> 的数目:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < n - 1</code></li>
|
||||
<li><code>nums[i] > nums[i + 1]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>当数组 <code>nums</code> 中 <strong>全局倒置</strong> 的数量等于 <strong>局部倒置</strong> 的数量时,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,0,2]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>有 1 个全局倒置,和 1 个局部倒置。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,0]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>有 2 个全局倒置,和 1 个局部倒置。
|
||||
</pre>
|
||||
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 5000</code></li>
|
||||
<li><code>0 <= nums[i] < n</code></li>
|
||||
<li><code>nums</code> 中的所有整数 <strong>互不相同</strong></li>
|
||||
<li><code>nums</code> 是范围 <code>[0, n - 1]</code> 内所有数字组成的一个排列</li>
|
||||
</ul>
|
39
算法题(国内版)/problem (Chinese)/公交路线 [bus-routes].html
Normal file
39
算法题(国内版)/problem (Chinese)/公交路线 [bus-routes].html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>给你一个数组 <code>routes</code> ,表示一系列公交线路,其中每个 <code>routes[i]</code> 表示一条公交线路,第 <code>i</code> 辆公交车将会在上面循环行驶。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,路线 <code>routes[0] = [1, 5, 7]</code> 表示第 <code>0</code> 辆公交车会一直按序列 <code>1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ...</code> 这样的车站路线行驶。</li>
|
||||
</ul>
|
||||
|
||||
<p>现在从 <code>source</code> 车站出发(初始时不在公交车上),要前往 <code>target</code> 车站。 期间仅可乘坐公交车。</p>
|
||||
|
||||
<p>求出 <strong>最少乘坐的公交车数量</strong> 。如果不可能到达终点车站,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>routes = [[1,2,7],[3,6,7]], source = 1, target = 6
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
|
||||
<strong>输出:</strong>-1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= routes.length <= 500</code>.</li>
|
||||
<li><code>1 <= routes[i].length <= 10<sup>5</sup></code></li>
|
||||
<li><code>routes[i]</code> 中的所有值 <strong>互不相同</strong></li>
|
||||
<li><code>sum(routes[i].length) <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= routes[i][j] < 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= source, target < 10<sup>6</sup></code></li>
|
||||
</ul>
|
46
算法题(国内版)/problem (Chinese)/公平的糖果交换 [fair-candy-swap].html
Normal file
46
算法题(国内版)/problem (Chinese)/公平的糖果交换 [fair-candy-swap].html
Normal file
@@ -0,0 +1,46 @@
|
||||
<p>爱丽丝和鲍勃拥有不同总数量的糖果。给你两个数组 <code>aliceSizes</code> 和 <code>bobSizes</code> ,<code>aliceSizes[i]</code> 是爱丽丝拥有的第 <code>i</code> 盒糖果中的糖果数量,<code>bobSizes[j]</code> 是鲍勃拥有的第 <code>j</code> 盒糖果中的糖果数量。</p>
|
||||
|
||||
<p>两人想要互相交换一盒糖果,这样在交换之后,他们就可以拥有相同总数量的糖果。一个人拥有的糖果总数量是他们每盒糖果数量的总和。</p>
|
||||
|
||||
<p>返回一个整数数组 <code>answer</code>,其中 <code>answer[0]</code> 是爱丽丝必须交换的糖果盒中的糖果的数目,<code>answer[1]</code> 是鲍勃必须交换的糖果盒中的糖果的数目。如果存在多个答案,你可以返回其中 <strong>任何一个</strong> 。题目测试用例保证存在与输入对应的答案。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>aliceSizes = [1,1], bobSizes = [2,2]
|
||||
<strong>输出:</strong>[1,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>aliceSizes = [1,2], bobSizes = [2,3]
|
||||
<strong>输出:</strong>[1,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>aliceSizes = [2], bobSizes = [1,3]
|
||||
<strong>输出:</strong>[2,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>aliceSizes = [1,2,5], bobSizes = [2,4]
|
||||
<strong>输出:</strong>[5,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= aliceSizes.length, bobSizes.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= aliceSizes[i], bobSizes[j] <= 10<sup>5</sup></code></li>
|
||||
<li>爱丽丝和鲍勃的糖果总数量不同。</li>
|
||||
<li>题目数据保证对于给定的输入至少存在一个有效答案。</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>给定一个根为 <code>root</code> 的二叉树,每个节点的深度是 <strong>该节点到根的最短距离</strong> 。</p>
|
||||
|
||||
<p>返回包含原始树中所有 <strong>最深节点</strong> 的 <em>最小子树</em> 。</p>
|
||||
|
||||
<p>如果一个节点在 <strong>整个树 </strong>的任意节点之间具有最大的深度,则该节点是 <strong>最深的</strong> 。</p>
|
||||
|
||||
<p>一个节点的 <strong>子树</strong> 是该节点加上它的所有后代的集合。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png" style="width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,5,1,6,2,0,8,null,null,7,4]
|
||||
<strong>输出:</strong>[2,7,4]
|
||||
<strong>解释:</strong>
|
||||
我们返回值为 2 的节点,在图中用黄色标记。
|
||||
在图中用蓝色标记的是树的最深的节点。
|
||||
注意,节点 5、3 和 2 包含树中最深的节点,但节点 2 的子树最小,因此我们返回它。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1]
|
||||
<strong>输出:</strong>[1]
|
||||
<strong>解释:</strong>根节点是树中最深的节点。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [0,1,3,null,2]
|
||||
<strong>输出:</strong>[2]
|
||||
<strong>解释:</strong>树中最深的节点为 2 ,有效子树为节点 2、1 和 0 的子树,但节点 2 的子树最小。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点的数量在<meta charset="UTF-8" /> <code>[1, 500]</code> 范围内。</li>
|
||||
<li><code>0 <= Node.val <= 500</code></li>
|
||||
<li>每个节点的值都是 <strong>独一无二</strong> 的。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>本题与力扣 1123 重复:<a href="https://leetcode-cn.com/problems/lowest-common-ancestor-of-deepest-leaves/" target="_blank">https://leetcode-cn.com/problems/lowest-common-ancestor-of-deepest-leaves</a></p>
|
@@ -0,0 +1,34 @@
|
||||
<p>在本问题中,有根树指满足以下条件的 <strong>有向</strong> 图。该树只有一个根节点,所有其他节点都是该根节点的后继。该树除了根节点之外的每一个节点都有且只有一个父节点,而根节点没有父节点。</p>
|
||||
|
||||
<p>输入一个有向图,该图由一个有着 <code>n</code> 个节点(节点值不重复,从 <code>1</code> 到 <code>n</code>)的树及一条附加的有向边构成。附加的边包含在 <code>1</code> 到 <code>n</code> 中的两个不同顶点间,这条附加的边不属于树中已存在的边。</p>
|
||||
|
||||
<p>结果图是一个以边组成的二维数组 <code>edges</code> 。 每个元素是一对 <code>[u<sub>i</sub>, v<sub>i</sub>]</code>,用以表示 <strong>有向 </strong>图中连接顶点 <code>u<sub>i</sub></code> 和顶点 <code>v<sub>i</sub></code> 的边,其中 <code>u<sub>i</sub></code> 是 <code>v<sub>i</sub></code> 的一个父节点。</p>
|
||||
|
||||
<p>返回一条能删除的边,使得剩下的图是有 <code>n</code> 个节点的有根树。若有多个答案,返回最后出现在给定二维数组的答案。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/graph1.jpg" style="width: 222px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>edges = [[1,2],[1,3],[2,3]]
|
||||
<strong>输出:</strong>[2,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/20/graph2.jpg" style="width: 222px; height: 382px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
|
||||
<strong>输出:</strong>[4,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == edges.length</code></li>
|
||||
<li><code>3 <= n <= 1000</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li>
|
||||
</ul>
|
39
算法题(国内版)/problem (Chinese)/冗余连接 [redundant-connection].html
Normal file
39
算法题(国内版)/problem (Chinese)/冗余连接 [redundant-connection].html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>树可以看成是一个连通且 <strong>无环 </strong>的 <strong>无向 </strong>图。</p>
|
||||
|
||||
<p>给定往一棵 <code>n</code> 个节点 (节点值 <code>1~n</code>) 的树中添加一条边后的图。添加的边的两个顶点包含在 <code>1</code> 到 <code>n</code> 中间,且这条附加的边不属于树中已存在的边。图的信息记录于长度为 <code>n</code> 的二维数组 <code>edges</code> ,<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示图中在 <code>ai</code> 和 <code>bi</code> 之间存在一条边。</p>
|
||||
|
||||
<p>请找出一条可以删去的边,删除后可使得剩余部分是一个有着 <code>n</code> 个节点的树。如果有多个答案,则返回数组 <code>edges</code> 中最后出现的边。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode-cn.com/1626676174-hOEVUL-image.png" style="width: 152px; " /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> edges = [[1,2], [1,3], [2,3]]
|
||||
<strong>输出:</strong> [2,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode-cn.com/1626676179-kGxcmu-image.png" style="width: 250px; " /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> edges = [[1,2], [2,3], [3,4], [1,4], [1,5]]
|
||||
<strong>输出:</strong> [1,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == edges.length</code></li>
|
||||
<li><code>3 <= n <= 1000</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= ai < bi <= edges.length</code></li>
|
||||
<li><code>ai != bi</code></li>
|
||||
<li><code>edges</code> 中无重复元素</li>
|
||||
<li>给定的图是连通的 </li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>我们要把给定的字符串 <code>S</code> 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100 个单位,那么我们应该把这个字母写到下一行。我们给定了一个数组 <code>widths</code> ,这个数组 widths[0] 代表 'a' 需要的单位, widths[1] 代表 'b' 需要的单位,..., widths[25] 代表 'z' 需要的单位。</p>
|
||||
|
||||
<p>现在回答两个问题:至少多少行能放下<code>S</code>,以及最后一行使用的宽度是多少个单位?将你的答案作为长度为2的整数列表返回。</p>
|
||||
|
||||
<pre>
|
||||
<strong>示例 1:</strong>
|
||||
<strong>输入:</strong>
|
||||
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
|
||||
S = "abcdefghijklmnopqrstuvwxyz"
|
||||
<strong>输出:</strong> [3, 60]
|
||||
<strong>解释:
|
||||
</strong>所有的字符拥有相同的占用单位10。所以书写所有的26个字母,
|
||||
我们需要2个整行和占用60个单位的一行。
|
||||
</pre>
|
||||
|
||||
<pre>
|
||||
<strong>示例 2:</strong>
|
||||
<strong>输入:</strong>
|
||||
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
|
||||
S = "bbbcccdddaaa"
|
||||
<strong>输出:</strong> [2, 4]
|
||||
<strong>解释:
|
||||
</strong>除去字母'a'所有的字符都是相同的单位10,并且字符串 "bbbcccdddaa" 将会覆盖 9 * 10 + 2 * 4 = 98 个单位.
|
||||
最后一个字母 'a' 将会被写到第二行,因为第一行只剩下2个单位了。
|
||||
所以,这个答案是2行,第二行有4个单位宽度。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>字符串 <code>S</code> 的长度在 [1, 1000] 的范围。</li>
|
||||
<li><code>S</code> 只包含小写字母。</li>
|
||||
<li><code>widths</code> 是长度为 <code>26</code>的数组。</li>
|
||||
<li><code>widths[i]</code> 值的范围在 <code>[2, 10]</code>。</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你一个大小为 <code>m x n</code> 的网格和一个球。球的起始坐标为 <code>[startRow, startColumn]</code> 。你可以将球移到在四个方向上相邻的单元格内(可以穿过网格边界到达网格之外)。你 <strong>最多</strong> 可以移动 <code>maxMove</code> 次球。</p>
|
||||
|
||||
<p>给你五个整数 <code>m</code>、<code>n</code>、<code>maxMove</code>、<code>startRow</code> 以及 <code>startColumn</code> ,找出并返回可以将球移出边界的路径数量。因为答案可能非常大,返回对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后的结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png" style="width: 500px; height: 296px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
|
||||
<strong>输出:</strong>6
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png" style="width: 500px; height: 293px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
|
||||
<strong>输出:</strong>12
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 50</code></li>
|
||||
<li><code>0 <= maxMove <= 50</code></li>
|
||||
<li><code>0 <= startRow < m</code></li>
|
||||
<li><code>0 <= startColumn < n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,77 @@
|
||||
<p>有一个 <strong>单线程</strong> CPU 正在运行一个含有 <code>n</code> 道函数的程序。每道函数都有一个位于 <code>0</code> 和 <code>n-1</code> 之间的唯一标识符。</p>
|
||||
|
||||
<p>函数调用 <strong>存储在一个 <a href="https://baike.baidu.com/item/%E8%B0%83%E7%94%A8%E6%A0%88/22718047?fr=aladdin" target="_blank">调用栈</a> 上</strong> :当一个函数调用开始时,它的标识符将会推入栈中。而当一个函数调用结束时,它的标识符将会从栈中弹出。标识符位于栈顶的函数是 <strong>当前正在执行的函数</strong> 。每当一个函数开始或者结束时,将会记录一条日志,包括函数标识符、是开始还是结束、以及相应的时间戳。</p>
|
||||
|
||||
<p>给你一个由日志组成的列表 <code>logs</code> ,其中 <code>logs[i]</code> 表示第 <code>i</code> 条日志消息,该消息是一个按 <code>"{function_id}:{"start" | "end"}:{timestamp}"</code> 进行格式化的字符串。例如,<code>"0:start:3"</code> 意味着标识符为 <code>0</code> 的函数调用在时间戳 <code>3</code> 的 <strong>起始开始执行</strong> ;而 <code>"1:end:2"</code> 意味着标识符为 <code>1</code> 的函数调用在时间戳 <code>2</code> 的 <strong>末尾结束执行</strong>。注意,函数可以 <strong>调用多次,可能存在递归调用 </strong>。</p>
|
||||
|
||||
<p>函数的 <strong>独占时间</strong> 定义是在这个函数在程序所有函数调用中执行时间的总和,调用其他函数花费的时间不算该函数的独占时间。例如,如果一个函数被调用两次,一次调用执行 <code>2</code> 单位时间,另一次调用执行 <code>1</code> 单位时间,那么该函数的 <strong>独占时间</strong> 为 <code>2 + 1 = 3</code> 。</p>
|
||||
|
||||
<p>以数组形式返回每个函数的 <strong>独占时间</strong> ,其中第 <code>i</code> 个下标对应的值表示标识符 <code>i</code> 的函数的独占时间。</p>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/05/diag1b.png" style="width: 550px; height: 239px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2, logs = ["0:start:0","1:start:2","1:end:5","0:end:6"]
|
||||
<strong>输出:</strong>[3,4]
|
||||
<strong>解释:</strong>
|
||||
函数 0 在时间戳 0 的起始开始执行,执行 2 个单位时间,于时间戳 1 的末尾结束执行。
|
||||
函数 1 在时间戳 2 的起始开始执行,执行 4 个单位时间,于时间戳 5 的末尾结束执行。
|
||||
函数 0 在时间戳 6 的开始恢复执行,执行 1 个单位时间。
|
||||
所以函数 0 总共执行 2 + 1 = 3 个单位时间,函数 1 总共执行 4 个单位时间。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 1, logs = ["0:start:0","0:start:2","0:end:5","0:start:6","0:end:6","0:end:7"]
|
||||
<strong>输出:</strong>[8]
|
||||
<strong>解释:</strong>
|
||||
函数 0 在时间戳 0 的起始开始执行,执行 2 个单位时间,并递归调用它自身。
|
||||
函数 0(递归调用)在时间戳 2 的起始开始执行,执行 4 个单位时间。
|
||||
函数 0(初始调用)恢复执行,并立刻再次调用它自身。
|
||||
函数 0(第二次递归调用)在时间戳 6 的起始开始执行,执行 1 个单位时间。
|
||||
函数 0(初始调用)在时间戳 7 的起始恢复执行,执行 1 个单位时间。
|
||||
所以函数 0 总共执行 2 + 4 + 1 + 1 = 8 个单位时间。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:6","1:end:6","0:end:7"]
|
||||
<strong>输出:</strong>[7,1]
|
||||
<strong>解释:</strong>
|
||||
函数 0 在时间戳 0 的起始开始执行,执行 2 个单位时间,并递归调用它自身。
|
||||
函数 0(递归调用)在时间戳 2 的起始开始执行,执行 4 个单位时间。
|
||||
函数 0(初始调用)恢复执行,并立刻调用函数 1 。
|
||||
函数 1在时间戳 6 的起始开始执行,执行 1 个单位时间,于时间戳 6 的末尾结束执行。
|
||||
函数 0(初始调用)在时间戳 7 的起始恢复执行,执行 1 个单位时间,于时间戳 7 的末尾结束执行。
|
||||
所以函数 0 总共执行 2 + 4 + 1 = 7 个单位时间,函数 1 总共执行 1 个单位时间。 </pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2, logs = ["0:start:0","0:start:2","0:end:5","1:start:7","1:end:7","0:end:8"]
|
||||
<strong>输出:</strong>[8,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 1, logs = ["0:start:0","0:end:0"]
|
||||
<strong>输出:</strong>[1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= logs.length <= 500</code></li>
|
||||
<li><code>0 <= function_id < n</code></li>
|
||||
<li><code>0 <= timestamp <= 10<sup>9</sup></code></li>
|
||||
<li>两个开始事件不会在同一时间戳发生</li>
|
||||
<li>两个结束事件不会在同一时间戳发生</li>
|
||||
<li>每道函数都有一个对应 <code>"start"</code> 日志的 <code>"end"</code> 日志</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>给定一个数组 <code>nums</code> ,将其划分为两个连续子数组 <code>left</code> 和 <code>right</code>, 使得:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>left</code> 中的每个元素都小于或等于 <code>right</code> 中的每个元素。</li>
|
||||
<li><code>left</code> 和 <code>right</code> 都是非空的。</li>
|
||||
<li><code>left</code> 的长度要尽可能小。</li>
|
||||
</ul>
|
||||
|
||||
<p><em>在完成这样的分组后返回 <code>left</code> 的 <strong>长度 </strong></em>。</p>
|
||||
|
||||
<p>用例可以保证存在这样的划分方法。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,0,3,8,6]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>left = [5,0,3],right = [8,6]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1,0,6,12]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>left = [1,1,1,0],right = [6,12]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
<li>可以保证至少有一种方法能够按题目所描述的那样对 <code>nums</code> 进行划分。</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个按升序排序的整数数组 <code>num</code>(可能包含重复数字),请你将它们分割成一个或多个长度至少为 3 的子序列,其中每个子序列都由连续整数组成。</p>
|
||||
|
||||
<p>如果可以完成上述分割,则返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> [1,2,3,3,4,5]
|
||||
<strong>输出:</strong> True
|
||||
<strong>解释:</strong>
|
||||
你可以分割出这样两个连续子序列 :
|
||||
1, 2, 3
|
||||
3, 4, 5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> [1,2,3,3,4,4,5,5]
|
||||
<strong>输出:</strong> True
|
||||
<strong>解释:</strong>
|
||||
你可以分割出这样两个连续子序列 :
|
||||
1, 2, 3, 4, 5
|
||||
3, 4, 5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> [1,2,3,4,4,5]
|
||||
<strong>输出:</strong> False
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>提示:</b></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给定一个表示分数加减运算的字符串 <code>expression</code> ,你需要返回一个字符串形式的计算结果。 </p>
|
||||
|
||||
<p>这个结果应该是不可约分的分数,即<a href="https://baike.baidu.com/item/%E6%9C%80%E7%AE%80%E5%88%86%E6%95%B0" target="_blank">最简分数</a>。 如果最终结果是一个整数,例如 <code>2</code>,你需要将它转换成分数形式,其分母为 <code>1</code>。所以在上述例子中, <code>2</code> 应该被转换为 <code>2/1</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> <code>expression</code> = "-1/2+1/2"
|
||||
<strong>输出:</strong> "0/1"
|
||||
</pre>
|
||||
|
||||
<p><strong> 示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> <code>expression</code> = "-1/2+1/2+1/3"
|
||||
<strong>输出:</strong> "1/3"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> <code>expression</code> = "1/3-1/2"
|
||||
<strong>输出:</strong> "-1/6"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>输入和输出字符串只包含 <code>'0'</code> 到 <code>'9'</code> 的数字,以及 <code>'/'</code>, <code>'+'</code> 和 <code>'-'</code>。 </li>
|
||||
<li>输入和输出分数格式均为 <code>±分子/分母</code>。如果输入的第一个分数或者输出的分数是正数,则 <code>'+'</code> 会被省略掉。</li>
|
||||
<li>输入只包含合法的<strong>最简分数</strong>,每个分数的<strong>分子</strong>与<strong>分母</strong>的范围是 [1,10]。 如果分母是1,意味着这个分数实际上是一个整数。</li>
|
||||
<li>输入的分数个数范围是 [1,10]。</li>
|
||||
<li><strong>最终结果</strong>的分子与分母保证是 32 位整数范围内的有效整数。</li>
|
||||
</ul>
|
42
算法题(国内版)/problem (Chinese)/分汤 [soup-servings].html
Normal file
42
算法题(国内版)/problem (Chinese)/分汤 [soup-servings].html
Normal file
@@ -0,0 +1,42 @@
|
||||
<p>有 <strong>A 和 B 两种类型 </strong>的汤。一开始每种类型的汤有 <code>n</code> 毫升。有四种分配操作:</p>
|
||||
|
||||
<ol>
|
||||
<li>提供 <code>100ml</code> 的 <strong>汤A</strong> 和 <code>0ml</code> 的 <strong>汤B</strong> 。</li>
|
||||
<li>提供 <code>75ml</code> 的 <strong>汤A</strong> 和 <code>25ml</code> 的 <strong>汤B</strong> 。</li>
|
||||
<li>提供 <code>50ml</code> 的 <strong>汤A</strong> 和 <code>50ml</code> 的 <strong>汤B</strong> 。</li>
|
||||
<li>提供 <code>25ml</code> 的 <strong>汤A</strong> 和 <code>75ml</code> 的 <strong>汤B</strong> 。</li>
|
||||
</ol>
|
||||
|
||||
<p>当我们把汤分配给某人之后,汤就没有了。每个回合,我们将从四种概率同为 <code>0.25</code> 的操作中进行分配选择。如果汤的剩余量不足以完成某次操作,我们将尽可能分配。当两种类型的汤都分配完时,停止操作。</p>
|
||||
|
||||
<p><strong>注意 </strong>不存在先分配 <code>100</code> ml <strong>汤B</strong> 的操作。</p>
|
||||
|
||||
<p>需要返回的值: <strong>汤A </strong>先分配完的概率 + <strong>汤A和汤B </strong>同时分配完的概率 / 2。返回值在正确答案 <code>10<sup>-5</sup></code> 的范围内将被认为是正确的。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> n = 50
|
||||
<strong>输出:</strong> 0.62500
|
||||
<strong>解释:</strong>如果我们选择前两个操作<strong>,</strong>A 首先将变为空。
|
||||
对于第三个操作,A 和 B 会同时变为空。
|
||||
对于第四个操作,B 首先将变为空。<strong>
|
||||
</strong>所以 A 变为空的总概率加上 A 和 B 同时变为空的概率的一半是 0.25 *(1 + 1 + 0.5 + 0)= 0.625。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> n = 100
|
||||
<strong>输出:</strong> 0.71875
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
42
算法题(国内版)/problem (Chinese)/分糖果 [distribute-candies].html
Normal file
42
算法题(国内版)/problem (Chinese)/分糖果 [distribute-candies].html
Normal file
@@ -0,0 +1,42 @@
|
||||
<p>Alice 有 <code>n</code> 枚糖,其中第 <code>i</code> 枚糖的类型为 <code>candyType[i]</code> 。Alice 注意到她的体重正在增长,所以前去拜访了一位医生。</p>
|
||||
|
||||
<p>医生建议 Alice 要少摄入糖分,只吃掉她所有糖的 <code>n / 2</code> 即可(<code>n</code> 是一个偶数)。Alice 非常喜欢这些糖,她想要在遵循医生建议的情况下,尽可能吃到最多不同种类的糖。</p>
|
||||
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>candyType</code> ,返回: Alice <em>在仅吃掉 <code>n / 2</code> 枚糖的情况下,可以吃到糖的 <strong>最多</strong> 种类数</em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>candyType = [1,1,2,2,3,3]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>Alice 只能吃 6 / 2 = 3 枚糖,由于只有 3 种糖,她可以每种吃一枚。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>candyType = [1,1,2,3]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>Alice 只能吃 4 / 2 = 2 枚糖,不管她选择吃的种类是 [1,2]、[1,3] 还是 [2,3],她只能吃到两种不同类的糖。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>candyType = [6,6,6,6]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>Alice 只能吃 4 / 2 = 2 枚糖,尽管她能吃 2 枚,但只能吃到 1 种糖。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == candyType.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>n</code> 是一个偶数</li>
|
||||
<li><code>-10<sup>5</sup> <= candyType[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个头结点为 <code>head</code> 的单链表和一个整数 <code>k</code> ,请你设计一个算法将链表分隔为 <code>k</code> 个连续的部分。</p>
|
||||
|
||||
<p>每部分的长度应该尽可能的相等:任意两部分的长度差距不能超过 1 。这可能会导致有些部分为 null 。</p>
|
||||
|
||||
<p>这 <code>k</code> 个部分应该按照在链表中出现的顺序排列,并且排在前面的部分的长度应该大于或等于排在后面的长度。</p>
|
||||
|
||||
<p>返回一个由上述 <code>k</code> 部分组成的数组。</p>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/split1-lc.jpg" style="width: 400px; height: 134px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,2,3], k = 5
|
||||
<strong>输出:</strong>[[1],[2],[3],[],[]]
|
||||
<strong>解释:</strong>
|
||||
第一个元素 output[0] 为 output[0].val = 1 ,output[0].next = null 。
|
||||
最后一个元素 output[4] 为 null ,但它作为 ListNode 的字符串表示是 [] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/split2-lc.jpg" style="width: 600px; height: 60px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,2,3,4,5,6,7,8,9,10], k = 3
|
||||
<strong>输出:</strong>[[1,2,3,4],[5,6,7],[8,9,10]]
|
||||
<strong>解释:</strong>
|
||||
输入被分成了几个连续的部分,并且每部分的长度相差不超过 1 。前面部分的长度大于等于后面部分的长度。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>链表中节点的数目在范围 <code>[0, 1000]</code></li>
|
||||
<li><code>0 <= Node.val <= 1000</code></li>
|
||||
<li><code>1 <= k <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>给定一个整数数组 <code>nums</code> 和一个正整数 <code>k</code>,找出是否有可能把这个数组分成 <code>k</code> 个非空子集,其总和都相等。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [4, 3, 2, 3, 5, 2, 1], k = 4
|
||||
<strong>输出:</strong> True
|
||||
<strong>说明:</strong> 有可能将其分成 4 个子集(5),(1,4),(2,3),(2,3)等于总和。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [1,2,3,4], k = 3
|
||||
<strong>输出:</strong> false</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= len(nums) <= 16</code></li>
|
||||
<li><code>0 < nums[i] < 10000</code></li>
|
||||
<li>每个元素的频率在 <code>[1,4]</code> 范围内</li>
|
||||
</ul>
|
23
算法题(国内版)/problem (Chinese)/划分字母区间 [partition-labels].html
Normal file
23
算法题(国内版)/problem (Chinese)/划分字母区间 [partition-labels].html
Normal file
@@ -0,0 +1,23 @@
|
||||
<p>字符串 <code>S</code> 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>S = "ababcbacadefegdehijhklij"
|
||||
<strong>输出:</strong>[9,7,8]
|
||||
<strong>解释:</strong>
|
||||
划分结果为 "ababcbaca", "defegde", "hijhklij"。
|
||||
每个字母最多出现在一个片段中。
|
||||
像 "ababcbacadefegde", "hijhklij" 的划分是错误的,因为划分的片段数较少。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>S</code>的长度在<code>[1, 500]</code>之间。</li>
|
||||
<li><code>S</code>只包含小写字母 <code>'a'</code> 到 <code>'z'</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>给定由 <code>n</code> 个字符串组成的数组 <code>strs</code>,其中每个字符串长度相等。</p>
|
||||
|
||||
<p>选取一个删除索引序列,对于 <code>strs</code> 中的每个字符串,删除对应每个索引处的字符。</p>
|
||||
|
||||
<p>比如,有 <code>strs = ["abcdef", "uvwxyz"]</code>,删除索引序列 <code>{0, 2, 3}</code>,删除后 <code>strs</code> 为<code>["bef", "vyz"]</code>。</p>
|
||||
|
||||
<p>假设,我们选择了一组删除索引 <code>answer</code>,那么在执行删除操作之后,最终得到的数组的元素是按 <strong>字典序</strong>(<code>strs[0] <= strs[1] <= strs[2] ... <= strs[n - 1]</code>)排列的,然后请你返回 <code>answer.length</code> 的最小可能值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["ca","bb","ac"]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释: </strong>
|
||||
删除第一列后,strs = ["a", "b", "c"]。
|
||||
现在 strs 中元素是按字典排列的 (即,strs[0] <= strs[1] <= strs[2])。
|
||||
我们至少需要进行 1 次删除,因为最初 strs 不是按字典序排列的,所以答案是 1。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["xc","yb","za"]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>
|
||||
strs 的列已经是按字典序排列了,所以我们不需要删除任何东西。
|
||||
注意 strs 的行不需要按字典序排列。
|
||||
也就是说,strs[0][0] <= strs[0][1] <= ... 不一定成立。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["zyx","wvu","tsr"]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
我们必须删掉每一列。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == strs.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= strs[i].length <= 100</code></li>
|
||||
<li><code>strs[i]</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>给定由<meta charset="UTF-8" /> <code>n</code> 个小写字母字符串组成的数组<meta charset="UTF-8" /> <code>strs</code> ,其中每个字符串长度相等。</p>
|
||||
|
||||
<p>选取一个删除索引序列,对于<meta charset="UTF-8" /> <code>strs</code> 中的每个字符串,删除对应每个索引处的字符。</p>
|
||||
|
||||
<p>比如,有<meta charset="UTF-8" /> <code>strs = ["abcdef","uvwxyz"]</code> ,删除索引序列<meta charset="UTF-8" /> <code>{0, 2, 3}</code> ,删除后为<meta charset="UTF-8" /> <code>["bef", "vyz"]</code> 。</p>
|
||||
|
||||
<p>假设,我们选择了一组删除索引<meta charset="UTF-8" /> <code>answer</code> ,那么在执行删除操作之后,最终得到的数组的行中的 <strong>每个元素</strong> 都是按<strong>字典序</strong>排列的(即 <code>(strs[0][0] <= strs[0][1] <= ... <= strs[0][strs[0].length - 1])</code> 和 <code>(strs[1][0] <= strs[1][1] <= ... <= strs[1][strs[1].length - 1])</code> ,依此类推)。</p>
|
||||
|
||||
<p>请返回<meta charset="UTF-8" /><em> <code>answer.length</code> 的最小可能值</em> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["babca","bbazb"]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:
|
||||
</strong>删除 0、1 和 4 这三列后,最终得到的数组是 A = ["bc", "az"]。
|
||||
这两行是分别按字典序排列的(即,A[0][0] <= A[0][1] 且 A[1][0] <= A[1][1])。
|
||||
注意,A[0] > A[1] —— 数组 A 不一定是按字典序排列的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["edcba"]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>如果删除的列少于 4 列,则剩下的行都不会按字典序排列。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["ghi","def","abc"]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>所有行都已按字典序排列。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
<meta charset="UTF-8" />
|
||||
|
||||
<ul>
|
||||
<li><code>n == strs.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= strs[i].length <= 100</code></li>
|
||||
<li><code>strs[i]</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,60 @@
|
||||
<p>给你由 <code>n</code> 个小写字母字符串组成的数组 <code>strs</code>,其中每个字符串长度相等。</p>
|
||||
|
||||
<p>这些字符串可以每个一行,排成一个网格。例如,<code>strs = ["abc", "bce", "cae"]</code> 可以排列为:</p>
|
||||
|
||||
<pre>
|
||||
abc
|
||||
bce
|
||||
cae</pre>
|
||||
|
||||
<p>你需要找出并删除 <strong>不是按字典序升序排列的</strong> 列。在上面的例子(下标从 0 开始)中,列 0(<code>'a'</code>, <code>'b'</code>, <code>'c'</code>)和列 2(<code>'c'</code>, <code>'e'</code>, <code>'e'</code>)都是按升序排列的,而列 1(<code>'b'</code>, <code>'c'</code>, <code>'a'</code>)不是,所以要删除列 1 。</p>
|
||||
|
||||
<p>返回你需要删除的列数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["cba","daf","ghi"]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>网格示意如下:
|
||||
cba
|
||||
daf
|
||||
ghi
|
||||
列 0 和列 2 按升序排列,但列 1 不是,所以只需要删除列 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["a","b"]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>网格示意如下:
|
||||
a
|
||||
b
|
||||
只有列 0 这一列,且已经按升序排列,所以不用删除任何列。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["zyx","wvu","tsr"]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>网格示意如下:
|
||||
zyx
|
||||
wvu
|
||||
tsr
|
||||
所有 3 列都是非升序排列的,所以都要删除。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == strs.length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= strs[i].length <= 1000</code></li>
|
||||
<li><code>strs[i]</code> 由小写英文字母组成</li>
|
||||
</ul>
|
37
算法题(国内版)/problem (Chinese)/删除并获得点数 [delete-and-earn].html
Normal file
37
算法题(国内版)/problem (Chinese)/删除并获得点数 [delete-and-earn].html
Normal file
@@ -0,0 +1,37 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,你可以对它进行一些操作。</p>
|
||||
|
||||
<p>每次操作中,选择任意一个 <code>nums[i]</code> ,删除它并获得 <code>nums[i]</code> 的点数。之后,你必须删除 <strong>所有 </strong>等于 <code>nums[i] - 1</code> 和 <code>nums[i] + 1</code> 的元素。</p>
|
||||
|
||||
<p>开始你拥有 <code>0</code> 个点数。返回你能通过这些操作获得的最大点数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,4,2]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>
|
||||
删除 4 获得 4 个点数,因此 3 也被删除。
|
||||
之后,删除 2 获得 2 个点数。总共获得 6 个点数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,2,3,3,3,4]
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>
|
||||
删除 3 获得 3 个点数,接着要删除两个 2 和 4 。
|
||||
之后,再次删除 3 获得 3 个点数,再次删除 3 获得 3 个点数。
|
||||
总共获得 9 个点数。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>有效括号字符串为空 <code>""</code>、<code>"(" + A + ")"</code> 或 <code>A + B</code> ,其中 <code>A</code> 和 <code>B</code> 都是有效的括号字符串,<code>+</code> 代表字符串的连接。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>""</code>,<code>"()"</code>,<code>"(())()"</code> 和 <code>"(()(()))"</code> 都是有效的括号字符串。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果有效字符串 <code>s</code> 非空,且不存在将其拆分为 <code>s = A + B</code> 的方法,我们称其为<strong>原语(primitive)</strong>,其中 <code>A</code> 和 <code>B</code> 都是非空有效括号字符串。</p>
|
||||
|
||||
<p>给出一个非空有效字符串 <code>s</code>,考虑将其进行原语化分解,使得:<code>s = P_1 + P_2 + ... + P_k</code>,其中 <code>P_i</code> 是有效括号字符串原语。</p>
|
||||
|
||||
<p>对 <code>s</code> 进行原语化分解,删除分解中每个原语字符串的最外层括号,返回 <code>s</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "(()())(())"
|
||||
<strong>输出:</strong>"()()()"
|
||||
<strong>解释:
|
||||
</strong>输入字符串为 "(()())(())",原语化分解得到 "(()())" + "(())",
|
||||
删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "(()())(())(()(()))"
|
||||
<strong>输出:</strong>"()()()()(())"
|
||||
<strong>解释:</strong>
|
||||
输入字符串为 "(()())(())(()(()))",原语化分解得到 "(()())" + "(())" + "(()(()))",
|
||||
删除每个部分中的最外层括号后得到 "()()" + "()" + "()(())" = "()()()()(())"。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "()()"
|
||||
<strong>输出:</strong>""
|
||||
<strong>解释:</strong>
|
||||
输入字符串为 "()()",原语化分解得到 "()" + "()",
|
||||
删除每个部分中的最外层括号后得到 "" + "" = ""。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> 为 <code>'('</code> 或 <code>')'</code></li>
|
||||
<li><code>s</code> 是一个有效括号字符串</li>
|
||||
</ul>
|
80
算法题(国内版)/problem (Chinese)/删除注释 [remove-comments].html
Normal file
80
算法题(国内版)/problem (Chinese)/删除注释 [remove-comments].html
Normal file
@@ -0,0 +1,80 @@
|
||||
<p>给一个 C++ 程序,删除程序中的注释。这个程序<code>source</code>是一个数组,其中<code>source[i]</code>表示第 <code>i</code> 行源码。 这表示每行源码由<font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4"> <code>'\n' </code></span></span></font></font>分隔。</p>
|
||||
|
||||
<p>在 C++ 中有两种注释风格,行内注释和块注释。</p>
|
||||
|
||||
<ul>
|
||||
<li>字符串<code>//</code> 表示行注释,表示<code>//</code>和其右侧的其余字符应该被忽略。</li>
|
||||
<li>字符串<code>/*</code> 表示一个块注释,它表示直到下一个(非重叠)出现的<code>*/</code>之间的所有字符都应该被忽略。(阅读顺序为从左到右)非重叠是指,字符串<code>/*/</code>并没有结束块注释,因为注释的结尾与开头相重叠。</li>
|
||||
</ul>
|
||||
|
||||
<p>第一个有效注释优先于其他注释。</p>
|
||||
|
||||
<ul>
|
||||
<li>如果字符串<code>//</code>出现在块注释中会被忽略。</li>
|
||||
<li>同样,如果字符串<code>/*</code>出现在行或块注释中也会被忽略。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果一行在删除注释之后变为空字符串,那么<strong>不要</strong>输出该行。即,答案列表中的每个字符串都是非空的。</p>
|
||||
|
||||
<p>样例中<strong>没有</strong>控制字符,单引号或双引号字符。</p>
|
||||
|
||||
<ul>
|
||||
<li>比如,<code>source = "string s = "/* Not a comment. */";"</code> 不会出现在测试样例里。</li>
|
||||
</ul>
|
||||
|
||||
<p>此外,没有其他内容(如定义或宏)会干扰注释。</p>
|
||||
|
||||
<p>我们保证每一个块注释最终都会被闭合, 所以在行或块注释之外的<code>/*</code>总是开始新的注释。</p>
|
||||
|
||||
<p>最后,隐式换行符<strong>可以</strong>通过块注释删除。 有关详细信息,请参阅下面的示例。</p>
|
||||
|
||||
<p>从源代码中删除注释后,需要以相同的格式返回源代码。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"]
|
||||
<strong>输出:</strong> ["int main()","{ "," ","int a, b, c;","a = b + c;","}"]
|
||||
<strong>解释:</strong> 示例代码可以编排成这样:
|
||||
/*Test program */
|
||||
int main()
|
||||
{
|
||||
// variable declaration
|
||||
int a, b, c;
|
||||
/* This is a test
|
||||
multiline
|
||||
comment for
|
||||
testing */
|
||||
a = b + c;
|
||||
}
|
||||
第 1 行和第 6-9 行的字符串 /* 表示块注释。第 4 行的字符串 // 表示行注释。
|
||||
编排后:
|
||||
int main()
|
||||
{
|
||||
|
||||
int a, b, c;
|
||||
a = b + c;
|
||||
}</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> source = ["a/*comment", "line", "more_comment*/b"]
|
||||
<strong>输出:</strong> ["ab"]
|
||||
<strong>解释:</strong> 原始的 source 字符串是 "a/*comment<strong>\n</strong>line<strong>\n</strong>more_comment*/b", 其中我们用粗体显示了换行符。删除注释后,隐含的换行符被删除,留下字符串 "ab" 用换行符分隔成数组时就是 ["ab"].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= source.length <= 100</code></li>
|
||||
<li><code>0 <= source[i].length <= 80</code></li>
|
||||
<li><code>source[i]</code> 由可打印的 <strong>ASCII</strong> 字符组成。</li>
|
||||
<li>每个块注释都会被闭合。</li>
|
||||
<li>给定的源码中不会有单引号、双引号或其他控制字符。</li>
|
||||
</ul>
|
||||
<span style="display:block"><span style="height:0px"><span style="position:absolute"><span style="top:0px"><span style="left:-9999px"><span style="opacity:0"><span style="overflow:hidden"> </span></span></span></span></span></span></span>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user