mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-04 23:11:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始长度为 <strong>偶数</strong> 的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>只要 <code>nums</code> <strong>不是</strong> 空数组,你就重复执行以下步骤:</p>
|
||||
|
||||
<ul>
|
||||
<li>找到 <code>nums</code> 中的最小值,并删除它。</li>
|
||||
<li>找到 <code>nums</code> 中的最大值,并删除它。</li>
|
||||
<li>计算删除两数的平均值。</li>
|
||||
</ul>
|
||||
|
||||
<p>两数 <code>a</code> 和 <code>b</code> 的 <strong>平均值</strong> 为 <code>(a + b) / 2</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,<code>2</code> 和 <code>3</code> 的平均值是 <code>(2 + 3) / 2 = 2.5</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回上述过程能得到的 <strong>不同</strong> 平均值的数目。</p>
|
||||
|
||||
<p><strong>注意</strong> ,如果最小值或者最大值有重复元素,可以删除任意一个。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [4,1,4,0,3,5]
|
||||
<b>输出:</b>2
|
||||
<strong>解释:</strong>
|
||||
1. 删除 0 和 5 ,平均值是 (0 + 5) / 2 = 2.5 ,现在 nums = [4,1,4,3] 。
|
||||
2. 删除 1 和 4 ,平均值是 (1 + 4) / 2 = 2.5 ,现在 nums = [4,3] 。
|
||||
3. 删除 3 和 4 ,平均值是 (3 + 4) / 2 = 3.5 。
|
||||
2.5 ,2.5 和 3.5 之中总共有 2 个不同的数,我们返回 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,100]
|
||||
<b>输出:</b>1
|
||||
<strong>解释:</strong>
|
||||
删除 1 和 100 后只有一个平均值,所以我们返回 1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>nums.length</code> 是偶数。</li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个字符串 <code>s</code> 和一个 <strong>正</strong> 整数 <code>k</code> 。</p>
|
||||
|
||||
<p>从字符串 <code>s</code> 中选出一组满足下述条件且 <strong>不重叠</strong> 的子字符串:</p>
|
||||
|
||||
<ul>
|
||||
<li>每个子字符串的长度 <strong>至少</strong> 为 <code>k</code> 。</li>
|
||||
<li>每个子字符串是一个 <strong>回文串</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回最优方案中能选择的子字符串的 <strong>最大</strong> 数目。</p>
|
||||
|
||||
<p><strong>子字符串</strong> 是字符串中一个连续的字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1 :</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abaccdbbd", k = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>可以选择 s = "<em><strong>aba</strong></em>cc<em><strong>dbbd</strong></em>" 中斜体加粗的子字符串。"aba" 和 "dbbd" 都是回文,且长度至少为 k = 3 。
|
||||
可以证明,无法选出两个以上的有效子字符串。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2 :</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "adbcda", k = 2
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>字符串中不存在长度至少为 2 的回文子字符串。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> 仅由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,请你统计并返回 <code>nums</code> 的 <strong>子数组</strong> 中满足 <em>元素最小公倍数为 <code>k</code> </em>的子数组数目。</p>
|
||||
|
||||
<p><strong>子数组</strong> 是数组中一个连续非空的元素序列。</p>
|
||||
|
||||
<p><strong>数组的最小公倍数</strong> 是可被所有数组元素整除的最小正整数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1 :</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [3,6,2,7,1], k = 6
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>以 6 为最小公倍数的子数组是:
|
||||
- [<em><strong>3</strong></em>,<em><strong>6</strong></em>,2,7,1]
|
||||
- [<em><strong>3</strong></em>,<em><strong>6</strong></em>,<em><strong>2</strong></em>,7,1]
|
||||
- [3,<em><strong>6</strong></em>,2,7,1]
|
||||
- [3,<em><strong>6</strong></em>,<em><strong>2</strong></em>,7,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2 :</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [3], k = 2
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>不存在以 2 为最小公倍数的子数组。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i], k <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,73 @@
|
||||
<p>一个 <code>n</code> 个节点的无向树,节点编号为 <code>0</code> 到 <code>n - 1</code> ,树的根结点是 <code>0</code> 号节点。给你一个长度为 <code>n - 1</code> 的二维整数数组 <code>edges</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> ,表示节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 在树中有一条边。</p>
|
||||
|
||||
<p>在每一个节点 <code>i</code> 处有一扇门。同时给你一个都是偶数的数组 <code>amount</code> ,其中 <code>amount[i]</code> 表示:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>amount[i]</code> 的值是负数,那么它表示打开节点 <code>i</code> 处门扣除的分数。</li>
|
||||
<li>如果 <code>amount[i]</code> 的值是正数,那么它表示打开节点 <code>i</code> 处门加上的分数。</li>
|
||||
</ul>
|
||||
|
||||
<p>游戏按照如下规则进行:</p>
|
||||
|
||||
<ul>
|
||||
<li>一开始,Alice 在节点 <code>0</code> 处,Bob 在节点 <code>bob</code> 处。</li>
|
||||
<li>每一秒钟,Alice 和 Bob <strong>分别</strong> 移动到相邻的节点。Alice 朝着某个 <strong>叶子结点</strong> 移动,Bob 朝着节点 <code>0</code> 移动。</li>
|
||||
<li>对于他们之间路径上的 <strong>每一个</strong> 节点,Alice 和 Bob 要么打开门并扣分,要么打开门并加分。注意:
|
||||
<ul>
|
||||
<li>如果门 <strong>已经打开</strong> (被另一个人打开),不会有额外加分也不会扣分。</li>
|
||||
<li>如果 Alice 和 Bob <strong>同时</strong> 到达一个节点,他们会共享这个节点的加分或者扣分。换言之,如果打开这扇门扣 <code>c</code> 分,那么 Alice 和 Bob 分别扣 <code>c / 2</code> 分。如果这扇门的加分为 <code>c</code> ,那么他们分别加 <code>c / 2</code> 分。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>如果 Alice 到达了一个叶子结点,她会停止移动。类似的,如果 Bob 到达了节点 <code>0</code> ,他也会停止移动。注意这些事件互相 <strong>独立</strong> ,不会影响另一方移动。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 Alice 朝最优叶子结点移动的 <strong>最大</strong> 净得分。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg1.png" style="width: 275px; height: 275px;"></p>
|
||||
|
||||
<pre><b>输入:</b>edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>
|
||||
上图展示了输入给出的一棵树。游戏进行如下:
|
||||
- Alice 一开始在节点 0 处,Bob 在节点 3 处。他们分别打开所在节点的门。
|
||||
Alice 得分为 -2 。
|
||||
- Alice 和 Bob 都移动到节点 1 。
|
||||
因为他们同时到达这个节点,他们一起打开门并平分得分。
|
||||
Alice 的得分变为 -2 + (4 / 2) = 0 。
|
||||
- Alice 移动到节点 3 。因为 Bob 已经打开了这扇门,Alice 得分不变。
|
||||
Bob 移动到节点 0 ,并停止移动。
|
||||
- Alice 移动到节点 4 并打开这个节点的门,她得分变为 0 + 6 = 6 。
|
||||
现在,Alice 和 Bob 都不能进行任何移动了,所以游戏结束。
|
||||
Alice 无法得到更高分数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg2.png" style="width: 250px; height: 78px;"></p>
|
||||
|
||||
<pre><b>输入:</b>edges = [[0,1]], bob = 1, amount = [-7280,2350]
|
||||
<b>输出:</b>-7280
|
||||
<b>解释:</b>
|
||||
Alice 按照路径 0->1 移动,同时 Bob 按照路径 1->0 移动。
|
||||
所以 Alice 只打开节点 0 处的门,她的得分为 -7280 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> 表示一棵有效的树。</li>
|
||||
<li><code>1 <= bob < n</code></li>
|
||||
<li><code>amount.length == n</code></li>
|
||||
<li><code>amount[i]</code> 是范围 <code>[-10<sup>4</sup>, 10<sup>4</sup>]</code> 之间的一个 <strong>偶数</strong> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个字符串 <code>message</code> 和一个正整数 <code>limit</code> 。</p>
|
||||
|
||||
<p>你需要根据 <code>limit</code> 将 <code>message</code> <strong>分割</strong> 成一个或多个 <strong>部分</strong> 。每个部分的结尾都是 <code>"<a/b>"</code> ,其中 <code>"b"</code> 用分割出来的总数 <b>替换</b>, <code>"a"</code> 用当前部分所在的编号 <strong>替换</strong> ,编号从 <code>1</code> 到 <code>b</code> 依次编号。除此以外,除了最后一部分长度 <strong>小于等于</strong> <code>limit</code> 以外,其他每一部分(包括结尾部分)的长度都应该 <strong>等于</strong> <code>limit</code> 。</p>
|
||||
|
||||
<p>你需要确保分割后的结果数组,删掉每部分的结尾并<strong> 按顺序 </strong>连起来后,能够得到 <code>message</code> 。同时,结果数组越短越好。</p>
|
||||
|
||||
<p>请你返回<em> </em><code>message</code> 分割后得到的结果数组。如果无法按要求分割 <code>message</code> ,返回一个空数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>message = "this is really a very awesome message", limit = 9
|
||||
<b>输出:</b>["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"]
|
||||
<strong>解释:</strong>
|
||||
前面 9 个部分分别从 message 中得到 3 个字符。
|
||||
接下来的 5 个部分分别从 message 中得到 2 个字符。
|
||||
这个例子中,包含最后一个部分在内,每个部分的长度都为 9 。
|
||||
可以证明没有办法分割成少于 14 个部分。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>message = "short message", limit = 15
|
||||
<b>输出:</b>["short mess<1/2>","age<2/2>"]
|
||||
<strong>解释:</strong>
|
||||
在给定限制下,字符串可以分成两个部分:
|
||||
- 第一个部分包含 10 个字符,长度为 15 。
|
||||
- 第二个部分包含 3 个字符,长度为 8 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= message.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>message</code> 只包含小写英文字母和 <code>' '</code> 。</li>
|
||||
<li><code>1 <= limit <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>给你一个四舍五入到两位小数的非负浮点数 <code>celsius</code> 来表示温度,以 <strong>摄氏度</strong>(<strong>Celsius</strong>)为单位。</p>
|
||||
|
||||
<p>你需要将摄氏度转换为 <strong>开氏度</strong>(<strong>Kelvin</strong>)和 <strong>华氏度</strong>(<strong>Fahrenheit</strong>),并以数组 <code>ans = [kelvin, fahrenheit]</code> 的形式返回结果。</p>
|
||||
|
||||
<p>返回数组<em> <code>ans</code></em> 。与实际答案误差不超过 <code>10<sup>-5</sup></code> 的会视为正确答案<strong>。</strong></p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>开氏度 = 摄氏度 + 273.15</code></li>
|
||||
<li><code>华氏度 = 摄氏度 * 1.80 + 32.00</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1 :</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>celsius = 36.50
|
||||
<strong>输出:</strong>[309.65000,97.70000]
|
||||
<strong>解释:</strong>36.50 摄氏度:转换为开氏度是 309.65 ,转换为华氏度是 97.70 。</pre>
|
||||
|
||||
<p><strong>示例 2 :</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>celsius = 122.11
|
||||
<strong>输出:</strong>[395.26000,251.79800]
|
||||
<strong>解释:</strong>122.11 摄氏度:转换为开氏度是 395.26 ,转换为华氏度是 251.798 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= celsius <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你整数 <code>zero</code> ,<code>one</code> ,<code>low</code> 和 <code>high</code> ,我们从空字符串开始构造一个字符串,每一步执行下面操作中的一种:</p>
|
||||
|
||||
<ul>
|
||||
<li>将 <code>'0'</code> 在字符串末尾添加 <code>zero</code> 次。</li>
|
||||
<li>将 <code>'1'</code> 在字符串末尾添加 <code>one</code> 次。</li>
|
||||
</ul>
|
||||
|
||||
<p>以上操作可以执行任意次。</p>
|
||||
|
||||
<p>如果通过以上过程得到一个 <strong>长度</strong> 在 <code>low</code> 和 <code>high</code> 之间(包含上下边界)的字符串,那么这个字符串我们称为 <strong>好</strong> 字符串。</p>
|
||||
|
||||
<p>请你返回满足以上要求的 <strong>不同</strong> 好字符串数目。由于答案可能很大,请将结果对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>low = 3, high = 3, zero = 1, one = 1
|
||||
<b>输出:</b>8
|
||||
<b>解释:</b>
|
||||
一个可能的好字符串是 "011" 。
|
||||
可以这样构造得到:"" -> "0" -> "01" -> "011" 。
|
||||
从 "000" 到 "111" 之间所有的二进制字符串都是好字符串。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>low = 2, high = 3, zero = 1, one = 2
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>好字符串为 "00" ,"11" ,"000" ,"110" 和 "011" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= zero, one <= low</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你一个 <strong>值互不相同</strong> 的二叉树的根节点 <code>root</code> 。</p>
|
||||
|
||||
<p>在一步操作中,你可以选择 <strong>同一层</strong> 上任意两个节点,交换这两个节点的值。</p>
|
||||
|
||||
<p>返回每一层按 <strong>严格递增顺序</strong> 排序所需的最少操作数目。</p>
|
||||
|
||||
<p>节点的 <strong>层数</strong> 是该节点和根节点之间的路径的边数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1 :</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" style="width: 500px; height: 324px;">
|
||||
<pre><strong>输入:</strong>root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
- 交换 4 和 3 。第 2 层变为 [3,4] 。
|
||||
- 交换 7 和 5 。第 3 层变为 [5,6,8,7] 。
|
||||
- 交换 8 和 7 。第 3 层变为 [5,6,7,8] 。
|
||||
共计用了 3 步操作,所以返回 3 。
|
||||
可以证明 3 是需要的最少操作数目。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2 :</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" style="width: 400px; height: 303px;">
|
||||
<pre><strong>输入:</strong>root = [1,3,2,7,6,5,4]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:
|
||||
</strong>- 交换 3 和 2 。第 2 层变为 [2,3] 。
|
||||
- 交换 7 和 4 。第 3 层变为 [4,6,5,7] 。
|
||||
- 交换 6 和 5 。第 3 层变为 [4,5,6,7] 。
|
||||
共计用了 3 步操作,所以返回 3 。
|
||||
可以证明 3 是需要的最少操作数目。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3 :</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" style="width: 400px; height: 274px;">
|
||||
<pre><strong>输入:</strong>root = [1,2,3,4,5,6]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>每一层已经按递增顺序排序,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点的数目在范围 <code>[1, 10<sup>5</sup>]</code> 。</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li>树中的所有值 <strong>互不相同</strong> 。</li>
|
||||
</ul>
|
Reference in New Issue
Block a user