mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-12 19:01:47 +08:00
add leetcode problem-cn part1
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<p>给你一个披萨,它由 3n 块不同大小的部分组成,现在你和你的朋友们需要按照如下规则来分披萨:</p>
|
||||
|
||||
<ul>
|
||||
<li>你挑选 <strong>任意</strong> 一块披萨。</li>
|
||||
<li>Alice 将会挑选你所选择的披萨逆时针方向的下一块披萨。</li>
|
||||
<li>Bob 将会挑选你所选择的披萨顺时针方向的下一块披萨。</li>
|
||||
<li>重复上述过程直到没有披萨剩下。</li>
|
||||
</ul>
|
||||
|
||||
<p>每一块披萨的大小按顺时针方向由循环数组 <code>slices</code> 表示。</p>
|
||||
|
||||
<p>请你返回你可以获得的披萨大小总和的最大值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/03/21/sample_3_1723.png" style="height: 240px; width: 475px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>slices = [1,2,3,4,5,6]
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>选择大小为 4 的披萨,Alice 和 Bob 分别挑选大小为 3 和 5 的披萨。然后你选择大小为 6 的披萨,Alice 和 Bob 分别挑选大小为 2 和 1 的披萨。你获得的披萨总大小为 4 + 6 = 10 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/03/21/sample_4_1723.png" style="height: 250px; width: 475px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>slices = [8,9,8,6,1,1]
|
||||
<strong>输出:</strong>16
|
||||
<strong>解释:</strong>两轮都选大小为 8 的披萨。如果你选择大小为 9 的披萨,你的朋友们就会选择大小为 8 的披萨,这种情况下你的总和不是最大的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= slices.length <= 500</code></li>
|
||||
<li><code>slices.length % 3 == 0</code></li>
|
||||
<li><code>1 <= slices[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个仅由数字 6 和 9 组成的正整数 <code>num</code>。</p>
|
||||
|
||||
<p>你最多只能翻转一位数字,将 6 变成 9,或者把 9 变成 6 。</p>
|
||||
|
||||
<p>请返回你可以得到的最大数字。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num = 9669
|
||||
<strong>输出:</strong>9969
|
||||
<strong>解释:</strong>
|
||||
改变第一位数字可以得到 6669 。
|
||||
改变第二位数字可以得到 9969 。
|
||||
改变第三位数字可以得到 9699 。
|
||||
改变第四位数字可以得到 9666 。
|
||||
其中最大的数字是 9969 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num = 9996
|
||||
<strong>输出:</strong>9999
|
||||
<strong>解释:</strong>将最后一位从 6 变到 9,其结果 9999 是最大的数。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num = 9999
|
||||
<strong>输出:</strong>9999
|
||||
<strong>解释:</strong>无需改变就已经是最大的数字了。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10^4</code></li>
|
||||
<li><code>num</code> 每一位上的数字都是 6 或者 9 。</li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>给出第一个词 <code>first</code> 和第二个词 <code>second</code>,考虑在某些文本 <code>text</code> 中可能以 <code>"first second third"</code> 形式出现的情况,其中 <code>second</code> 紧随 <code>first</code> 出现,<code>third</code> 紧随 <code>second</code> 出现。</p>
|
||||
|
||||
<p>对于每种这样的情况,将第三个词 "<code>third</code>" 添加到答案中,并返回答案。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>text = "alice is a good girl she is a good student", first = "a", second = "good"
|
||||
<strong>输出:</strong>["girl","student"]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>text = "we will we will rock you", first = "we", second = "will"
|
||||
<strong>输出:</strong>["we","rock"]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= text.length <= 1000</code></li>
|
||||
<li><code>text</code> 由小写英文字母和空格组成</li>
|
||||
<li><code>text</code> 中的所有单词之间都由 <strong>单个空格字符</strong> 分隔</li>
|
||||
<li><code>1 <= first.length, second.length <= 10</code></li>
|
||||
<li><code>first</code> 和 <code>second</code> 由小写英文字母组成</li>
|
||||
</ul>
|
48
算法题(国内版)/problem (Chinese)/H2O 生成 [building-h2o].html
Normal file
48
算法题(国内版)/problem (Chinese)/H2O 生成 [building-h2o].html
Normal file
@@ -0,0 +1,48 @@
|
||||
<p>现在有两种线程,氧 <code>oxygen</code> 和氢 <code>hydrogen</code>,你的目标是组织这两种线程来产生水分子。</p>
|
||||
|
||||
<p>存在一个屏障(barrier)使得每个线程必须等候直到一个完整水分子能够被产生出来。</p>
|
||||
|
||||
<p>氢和氧线程会被分别给予 <code>releaseHydrogen</code> 和 <code>releaseOxygen</code> 方法来允许它们突破屏障。</p>
|
||||
|
||||
<p>这些线程应该三三成组突破屏障并能立即组合产生一个水分子。</p>
|
||||
|
||||
<p>你必须保证产生一个水分子所需线程的结合必须发生在下一个水分子产生之前。</p>
|
||||
|
||||
<p>换句话说:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果一个氧线程到达屏障时没有氢线程到达,它必须等候直到两个氢线程到达。</li>
|
||||
<li>如果一个氢线程到达屏障时没有其它线程到达,它必须等候直到一个氧线程和另一个氢线程到达。</li>
|
||||
</ul>
|
||||
|
||||
<p>书写满足这些限制条件的氢、氧线程同步代码。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>water = "HOH"
|
||||
<strong>输出: </strong>"HHO"
|
||||
<strong>解释:</strong> "HOH" 和 "OHH" 依然都是有效解。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>water = "OOHHHH"
|
||||
<strong>输出: </strong>"HHOHHO"
|
||||
<strong>解释:</strong> "HOHHHO", "OHHHHO", "HHOHOH", "HOHHOH", "OHHHOH", "HHOOHH", "HOHOHH" 和 "OHHOHH" 依然都是有效解。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 * n == water.length</code></li>
|
||||
<li><code>1 <= n <= 20</code></li>
|
||||
<li><code>water[i] == 'O' or 'H'</code></li>
|
||||
<li>输入字符串 <code>water</code> 中的 <font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">'H'</span></span></font></font> 总数将会是 <code>2 * n</code> 。</li>
|
||||
<li>输入字符串 <code>water</code> 中的 <font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">'O'</span></span></font></font> 总数将会是 <code>n</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,61 @@
|
||||
<p>「HTML 实体解析器」 是一种特殊的解析器,它将 HTML 代码作为输入,并用字符本身替换掉所有这些特殊的字符实体。</p>
|
||||
|
||||
<p>HTML 里这些特殊字符和它们对应的字符实体包括:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>双引号:</strong>字符实体为 <code>&quot;</code> ,对应的字符是 <code>"</code> 。</li>
|
||||
<li><strong>单引号:</strong>字符实体为 <code>&apos;</code> ,对应的字符是 <code>'</code> 。</li>
|
||||
<li><strong>与符号:</strong>字符实体为 <code>&amp;</code> ,对应对的字符是 <code>&</code> 。</li>
|
||||
<li><strong>大于号:</strong>字符实体为 <code>&gt;</code> ,对应的字符是 <code>></code> 。</li>
|
||||
<li><strong>小于号:</strong>字符实体为 <code>&lt;</code> ,对应的字符是 <code><</code> 。</li>
|
||||
<li><strong>斜线号:</strong>字符实体为 <code>&frasl;</code> ,对应的字符是 <code>/</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你输入字符串 <code>text</code> ,请你实现一个 HTML 实体解析器,返回解析器解析后的结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>text = "&amp; is an HTML entity but &ambassador; is not."
|
||||
<strong>输出:</strong>"& is an HTML entity but &ambassador; is not."
|
||||
<strong>解释:</strong>解析器把字符实体 &amp; 用 & 替换
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>text = "and I quote: &quot;...&quot;"
|
||||
<strong>输出:</strong>"and I quote: \"...\""
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>text = "Stay home! Practice on Leetcode :)"
|
||||
<strong>输出:</strong>"Stay home! Practice on Leetcode :)"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>text = "x &gt; y &amp;&amp; x &lt; y is always false"
|
||||
<strong>输出:</strong>"x > y && x < y is always false"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>text = "leetcode.com&frasl;problemset&frasl;all"
|
||||
<strong>输出:</strong>"leetcode.com/problemset/all"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= text.length <= 10^5</code></li>
|
||||
<li>字符串可能包含 256 个ASCII 字符中的任意字符。</li>
|
||||
</ul>
|
@@ -0,0 +1,25 @@
|
||||
<p>给你一个有效的 <a href="https://baike.baidu.com/item/IPv4" target="_blank">IPv4</a> 地址 <code>address</code>,返回这个 IP 地址的无效化版本。</p>
|
||||
|
||||
<p>所谓无效化 IP 地址,其实就是用 <code>"[.]"</code> 代替了每个 <code>"."</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>address = "1.1.1.1"
|
||||
<strong>输出:</strong>"1[.]1[.]1[.]1"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>address = "255.100.50.0"
|
||||
<strong>输出:</strong>"255[.]100[.]50[.]0"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>给出的 <code>address</code> 是一个有效的 IPv4 地址</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给定一个整数数组 <code>arr</code> 和一个整数 <code>k</code> ,通过重复 <code>k</code> 次来修改数组。</p>
|
||||
|
||||
<p>例如,如果 <code>arr = [1, 2]</code> ,<meta charset="UTF-8" /> <code>k = 3</code> ,那么修改后的数组将是 <code>[1, 2, 1, 2, 1, 2]</code> 。</p>
|
||||
|
||||
<p>返回修改后的数组中的最大的子数组之和。注意,子数组长度可以是 <code>0</code>,在这种情况下它的总和也是 <code>0</code>。</p>
|
||||
|
||||
<p>由于 <strong>结果可能会很大</strong>,需要返回的<meta charset="UTF-8" /> <code>10<sup>9</sup> + 7</code> 的 <strong>模</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,2], k = 3
|
||||
<strong>输出:</strong>9
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,-2,1], k = 5
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [-1,-2], k = 7
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
<meta charset="UTF-8" />
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= arr[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,它表示一个 <strong>栈</strong> ,其中 <code>nums[0]</code> 是栈顶的元素。</p>
|
||||
|
||||
<p>每一次操作中,你可以执行以下操作 <strong>之一</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>如果栈非空,那么 <strong>删除</strong> 栈顶端的元素。</li>
|
||||
<li>如果存在 1 个或者多个被删除的元素,你可以从它们中选择任何一个,<b>添加</b> 回栈顶,这个元素成为新的栈顶元素。</li>
|
||||
</ul>
|
||||
|
||||
<p>同时给你一个整数 <code>k</code> ,它表示你总共需要执行操作的次数。</p>
|
||||
|
||||
<p>请你返回 <strong>恰好</strong> 执行 <code>k</code> 次操作以后,栈顶元素的 <strong>最大值</strong> 。如果执行完 <code>k</code> 次操作以后,栈一定为空,请你返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [5,2,2,4,0,6], k = 4
|
||||
<b>输出:</b>5
|
||||
<strong>解释:</strong>
|
||||
4 次操作后,栈顶元素为 5 的方法之一为:
|
||||
- 第 1 次操作:删除栈顶元素 5 ,栈变为 [2,2,4,0,6] 。
|
||||
- 第 2 次操作:删除栈顶元素 2 ,栈变为 [2,4,0,6] 。
|
||||
- 第 3 次操作:删除栈顶元素 2 ,栈变为 [4,0,6] 。
|
||||
- 第 4 次操作:将 5 添加回栈顶,栈变为 [5,4,0,6] 。
|
||||
注意,这不是最后栈顶元素为 5 的唯一方式。但可以证明,4 次操作以后 5 是能得到的最大栈顶元素。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2], k = 1
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>
|
||||
第 1 次操作中,我们唯一的选择是将栈顶元素弹出栈。
|
||||
由于 1 次操作后无法得到一个非空的栈,所以我们返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i], k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你两个字符串 <code>s</code> 和 <code>t</code> ,你的目标是在 <code>k</code> 次操作以内把字符串 <code>s</code> 转变成 <code>t</code> 。</p>
|
||||
|
||||
<p>在第 <code>i</code> 次操作时(<code>1 <= i <= k</code>),你可以选择进行如下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择字符串 <code>s</code> 中满足 <code>1 <= j <= s.length</code> 且之前未被选过的任意下标 <code>j</code> (下标从 1 开始),并将此位置的字符切换 <code>i</code> 次。</li>
|
||||
<li>不进行任何操作。</li>
|
||||
</ul>
|
||||
|
||||
<p>切换 1 个字符的意思是用字母表中该字母的下一个字母替换它(字母表环状接起来,所以 <code>'z'</code> 切换后会变成 <code>'a'</code>)。第 <code>i</code> 次操作意味着该字符应切换 <code>i</code> 次</p>
|
||||
|
||||
<p>请记住任意一个下标 <code>j</code> 最多只能被操作 1 次。</p>
|
||||
|
||||
<p>如果在不超过 <code>k</code> 次操作内可以把字符串 <code>s</code> 转变成 <code>t</code> ,那么请你返回 <code>true</code> ,否则请你返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "input", t = "ouput", k = 9
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>第 6 次操作时,我们将 'i' 切换 6 次得到 'o' 。第 7 次操作时,我们将 'n' 切换 7 次得到 'u' 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abc", t = "bcd", k = 10
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>我们需要将每个字符切换 1 次才能得到 t 。我们可以在第 1 次操作时将 'a' 切换成 'b' ,但另外 2 个字母在剩余操作中无法再转变为 t 中对应字母。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aab", t = "bbb", k = 27
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>第 1 次操作时,我们将第一个 'a' 切换 1 次得到 'b' 。在第 27 次操作时,我们将第二个字母 'a' 切换 27 次得到 'b' 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length, t.length <= 10^5</code></li>
|
||||
<li><code>0 <= k <= 10^9</code></li>
|
||||
<li><code>s</code> 和 <code>t</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>给你一棵由 n 个顶点组成的无向树,顶点编号从 1 到 <code>n</code>。青蛙从 <strong>顶点 1</strong> 开始起跳。规则如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>在一秒内,青蛙从它所在的当前顶点跳到另一个 <strong>未访问</strong> 过的顶点(如果它们直接相连)。</li>
|
||||
<li>青蛙无法跳回已经访问过的顶点。</li>
|
||||
<li>如果青蛙可以跳到多个不同顶点,那么它跳到其中任意一个顶点上的机率都相同。</li>
|
||||
<li>如果青蛙不能跳到任何未访问过的顶点上,那么它每次跳跃都会停留在原地。</li>
|
||||
</ul>
|
||||
|
||||
<p>无向树的边用数组 <code>edges</code> 描述,其中 <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> 意味着存在一条直接连通 <code>from<sub>i</sub></code> 和 <code>to<sub>i</sub></code> 两个顶点的边。</p>
|
||||
|
||||
<p>返回青蛙在 <em><code>t</code></em> 秒后位于目标顶点 <em><code>target</code> </em>上的概率。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2021/12/21/frog1.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4
|
||||
<strong>输出:</strong>0.16666666666666666
|
||||
<strong>解释:</strong>上图显示了青蛙的跳跃路径。青蛙从顶点 1 起跳,第 <strong>1 秒</strong> 有 1/3 的概率跳到顶点 2 ,然后第 <strong>2 秒</strong> 有 1/2 的概率跳到顶点 4,因此青蛙在 2 秒后位于顶点 4 的概率是 1/3 * 1/2 = 1/6 = 0.16666666666666666 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2021/12/21/frog2.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 1, target = 7
|
||||
<strong>输出:</strong>0.3333333333333333
|
||||
<strong>解释:</strong>上图显示了青蛙的跳跃路径。青蛙从顶点 1 起跳,有 1/3 = 0.3333333333333333 的概率能够 <strong>1 秒</strong> 后跳到顶点 7 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>1 <= a<sub>i</sub>, b<sub>i</sub> <= n</code></li>
|
||||
<li><code>1 <= t <= 50</code></li>
|
||||
<li><code>1 <= target <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>给你两个正整数 <code>n</code> 和 <code>k</code> 。</p>
|
||||
|
||||
<p>如果正整数 <code>i</code> 满足 <code>n % i == 0</code> ,那么我们就说正整数 <code>i</code> 是整数 <code>n</code> 的因子。</p>
|
||||
|
||||
<p>考虑整数 <code>n</code> 的所有因子,将它们 <strong>升序排列</strong> 。请你返回第 <code>k</code> 个因子。如果 <code>n</code> 的因子数少于 <code>k</code> ,请你返回 <strong>-1</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 12, k = 3
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>因子列表包括 [1, 2, 3, 4, 6, 12],第 3 个因子是 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 7, k = 2
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>因子列表包括 [1, 7] ,第 2 个因子是 7 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4, k = 4
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>因子列表包括 [1, 2, 4] ,只有 3 个因子,所以我们应该返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= n <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>给你一个字符串 <code>text</code>,你需要使用 <code>text</code> 中的字母来拼凑尽可能多的单词 <strong>"balloon"(气球)</strong>。</p>
|
||||
|
||||
<p>字符串 <code>text</code> 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 <strong>"balloon"</strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/09/14/1536_ex1_upd.jpeg" style="height: 35px; width: 154px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "nlaebolko"
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/09/14/1536_ex2_upd.jpeg" style="height: 35px; width: 233px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "loonbalxballpoon"
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "leetcode"
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= text.length <= 10^4</code></li>
|
||||
<li><code>text</code> 全部由小写英文字母组成</li>
|
||||
</ul>
|
33
算法题(国内版)/problem (Chinese)/一周中的第几天 [day-of-the-week].html
Normal file
33
算法题(国内版)/problem (Chinese)/一周中的第几天 [day-of-the-week].html
Normal file
@@ -0,0 +1,33 @@
|
||||
<p>给你一个日期,请你设计一个算法来判断它是对应一周中的哪一天。</p>
|
||||
|
||||
<p>输入为三个整数:<code>day</code>、<code>month</code> 和 <code>year</code>,分别表示日、月、年。</p>
|
||||
|
||||
<p>您返回的结果必须是这几个值中的一个 <code>{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>day = 31, month = 8, year = 2019
|
||||
<strong>输出:</strong>"Saturday"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>day = 18, month = 7, year = 1999
|
||||
<strong>输出:</strong>"Sunday"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>day = 15, month = 8, year = 1993
|
||||
<strong>输出:</strong>"Sunday"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>给出的日期一定是在 <code>1971</code> 到 <code>2100</code> 年之间的有效日期。</li>
|
||||
</ul>
|
27
算法题(国内版)/problem (Chinese)/一年中的第几天 [day-of-the-year].html
Normal file
27
算法题(国内版)/problem (Chinese)/一年中的第几天 [day-of-the-year].html
Normal file
@@ -0,0 +1,27 @@
|
||||
<p>给你一个字符串 <code>date</code> ,按 <code>YYYY-MM-DD</code> 格式表示一个 <a href="https://baike.baidu.com/item/公元/17855" target="_blank">现行公元纪年法</a> 日期。返回该日期是当年的第几天。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>date = "2019-01-09"
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>给定日期是2019年的第九天。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>date = "2019-02-10"
|
||||
<strong>输出:</strong>41
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>date.length == 10</code></li>
|
||||
<li><code>date[4] == date[7] == '-'</code>,其他的 <code>date[i]</code> 都是数字</li>
|
||||
<li><code>date</code> 表示的范围从 1900 年 1 月 1 日至 2019 年 12 月 31 日</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>给你一个数组 <code>nums</code> 。数组「动态和」的计算公式为:<code>runningSum[i] = sum(nums[0]…nums[i])</code> 。</p>
|
||||
|
||||
<p>请返回 <code>nums</code> 的动态和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,2,3,4]
|
||||
<strong>输出:</strong>[1,3,6,10]
|
||||
<strong>解释:</strong>动态和计算过程为 [1, 1+2, 1+2+3, 1+2+3+4] 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,1,1,1,1]
|
||||
<strong>输出:</strong>[1,2,3,4,5]
|
||||
<strong>解释:</strong>动态和计算过程为 [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1] 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [3,1,2,10,1]
|
||||
<strong>输出:</strong>[3,4,6,16,17]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>-10^6 <= nums[i] <= 10^6</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个数组 <code>nums</code> ,每次操作你可以选择 <code>nums</code> 中的任意一个元素并将它改成任意值。</p>
|
||||
|
||||
<p>请你返回三次操作后, <code>nums</code> 中最大值与最小值的差的最小值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [5,3,2,4]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>将数组 [5,3,2,4] 变成 [<strong>2</strong>,<strong>2</strong>,2,<strong>2</strong>].
|
||||
最大值与最小值的差为 2-2 = 0 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,5,0,10,14]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>将数组 [1,5,0,10,14] 变成 [1,<strong>1</strong>,0,<strong>1</strong>,<strong>1</strong>] 。
|
||||
最大值与最小值的差为 1-0 = 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [6,6,0,1,1,4,6]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,5,6,14,15]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10^5</code></li>
|
||||
<li><code>-10^9 <= nums[i] <= 10^9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>给你一个字符串 <code>s</code> ,请你根据下面的算法重新构造字符串:</p>
|
||||
|
||||
<ol>
|
||||
<li>从 <code>s</code> 中选出 <strong>最小</strong> 的字符,将它 <strong>接在</strong> 结果字符串的后面。</li>
|
||||
<li>从 <code>s</code> 剩余字符中选出 <strong>最小</strong> 的字符,且该字符比上一个添加的字符大,将它 <strong>接在</strong> 结果字符串后面。</li>
|
||||
<li>重复步骤 2 ,直到你没法从 <code>s</code> 中选择字符。</li>
|
||||
<li>从 <code>s</code> 中选出 <strong>最大</strong> 的字符,将它 <strong>接在</strong> 结果字符串的后面。</li>
|
||||
<li>从 <code>s</code> 剩余字符中选出 <strong>最大</strong> 的字符,且该字符比上一个添加的字符小,将它 <strong>接在</strong> 结果字符串后面。</li>
|
||||
<li>重复步骤 5 ,直到你没法从 <code>s</code> 中选择字符。</li>
|
||||
<li>重复步骤 1 到 6 ,直到 <code>s</code> 中所有字符都已经被选过。</li>
|
||||
</ol>
|
||||
|
||||
<p>在任何一步中,如果最小或者最大字符不止一个 ,你可以选择其中任意一个,并将其添加到结果字符串。</p>
|
||||
|
||||
<p>请你返回将 <code>s</code> 中字符重新排序后的 <strong>结果字符串</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "aaaabbbbcccc"
|
||||
<strong>输出:</strong>"abccbaabccba"
|
||||
<strong>解释:</strong>第一轮的步骤 1,2,3 后,结果字符串为 result = "abc"
|
||||
第一轮的步骤 4,5,6 后,结果字符串为 result = "abccba"
|
||||
第一轮结束,现在 s = "aabbcc" ,我们再次回到步骤 1
|
||||
第二轮的步骤 1,2,3 后,结果字符串为 result = "abccbaabc"
|
||||
第二轮的步骤 4,5,6 后,结果字符串为 result = "abccbaabccba"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "rat"
|
||||
<strong>输出:</strong>"art"
|
||||
<strong>解释:</strong>单词 "rat" 在上述算法重排序以后变成 "art"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "leetcode"
|
||||
<strong>输出:</strong>"cdelotee"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "ggggggg"
|
||||
<strong>输出:</strong>"ggggggg"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "spo"
|
||||
<strong>输出:</strong>"ops"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 500</code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个 <code>n x n</code> 整数矩阵 <code>arr</code> ,请你返回 <strong>非零偏移下降路径</strong> 数字和的最小值。</p>
|
||||
|
||||
<p><strong>非零偏移下降路径</strong> 定义为:从 <code>arr</code> 数组中的每一行选择一个数字,且按顺序选出来的数字中,相邻数字不在原数组的同一列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/falling-grid.jpg" style="width: 244px; height: 245px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [[1,2,3],[4,5,6],[7,8,9]]
|
||||
<strong>输出:</strong>13
|
||||
<strong>解释:</strong>
|
||||
所有非零偏移下降路径包括:
|
||||
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
|
||||
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
|
||||
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
|
||||
下降路径中数字和最小的是 [1,5,7] ,所以答案是 13 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[7]]
|
||||
<strong>输出:</strong>7
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>-99 <= grid[i][j] <= 99</code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>返回 <code>s</code> 字典序最小的子序列,该子序列包含 <code>s</code> 的所有不同字符,且只包含一次。</p>
|
||||
|
||||
<p><strong>注意:</strong>该题与 316 <a href="https://leetcode.com/problems/remove-duplicate-letters/">https://leetcode.com/problems/remove-duplicate-letters/</a> 相同</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong><code>s = "bcabc"</code>
|
||||
<strong>输出<code>:</code></strong><code>"abc"</code>
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong><code>s = "cbacdcbc"</code>
|
||||
<strong>输出:</strong><code>"acdb"</code></pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>给你一个整数数组 <code>arr</code> 和一个整数 <code>k</code> 。现需要从数组中恰好移除 <code>k</code> 个元素,请找出移除后数组中不同整数的最少数目。</p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr = [5,5,4], k = 1
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>移除 1 个 4 ,数组中只剩下 5 一种整数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr = [4,3,1,1,3,3,2], k = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>先移除 4、2 ,然后再移除两个 1 中的任意 1 个或者三个 3 中的任意 1 个,最后剩下 1 和 3 两种整数。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10^5</code></li>
|
||||
<li><code>1 <= arr[i] <= 10^9</code></li>
|
||||
<li><code>0 <= k <= arr.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>给你一个字符串 <code>text</code> ,请你返回满足下述条件的 <strong>不同</strong> 非空子字符串的数目:</p>
|
||||
|
||||
<ul>
|
||||
<li>可以写成某个字符串与其自身相连接的形式(即,可以写为 <code>a + a</code>,其中 <code>a</code> 是某个字符串)。</li>
|
||||
</ul>
|
||||
|
||||
<p>例如,<code>abcabc</code> 就是 <code>abc</code> 和它自身连接形成的。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "abcabcabc"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>3 个子字符串分别为 "abcabc","bcabca" 和 "cabcab" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "leetcodeleetcode"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>2 个子字符串为 "ee" 和 "leetcodeleetcode" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= text.length <= 2000</code></li>
|
||||
<li><code>text</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>圣诞活动预热开始啦,汉堡店推出了全新的汉堡套餐。为了避免浪费原料,请你帮他们制定合适的制作计划。</p>
|
||||
|
||||
<p>给你两个整数 <code>tomatoSlices</code> 和 <code>cheeseSlices</code>,分别表示番茄片和奶酪片的数目。不同汉堡的原料搭配如下:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>巨无霸汉堡:</strong>4 片番茄和 1 片奶酪</li>
|
||||
<li><strong>小皇堡:</strong>2 片番茄和 1 片奶酪</li>
|
||||
</ul>
|
||||
|
||||
<p>请你以 <code>[total_jumbo, total_small]</code>([巨无霸汉堡总数,小皇堡总数])的格式返回恰当的制作方案,使得剩下的番茄片 <code>tomatoSlices</code> 和奶酪片 <code>cheeseSlices</code> 的数量都是 <code>0</code>。</p>
|
||||
|
||||
<p>如果无法使剩下的番茄片 <code>tomatoSlices</code> 和奶酪片 <code>cheeseSlices</code> 的数量为 <code>0</code>,就请返回 <code>[]</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>tomatoSlices = 16, cheeseSlices = 7
|
||||
<strong>输出:</strong>[1,6]
|
||||
<strong>解释:</strong>制作 1 个巨无霸汉堡和 6 个小皇堡需要 4*1 + 2*6 = 16 片番茄和 1 + 6 = 7 片奶酪。不会剩下原料。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>tomatoSlices = 17, cheeseSlices = 4
|
||||
<strong>输出:</strong>[]
|
||||
<strong>解释:</strong>只制作小皇堡和巨无霸汉堡无法用光全部原料。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>tomatoSlices = 4, cheeseSlices = 17
|
||||
<strong>输出:</strong>[]
|
||||
<strong>解释:</strong>制作 1 个巨无霸汉堡会剩下 16 片奶酪,制作 2 个小皇堡会剩下 15 片奶酪。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>tomatoSlices = 0, cheeseSlices = 0
|
||||
<strong>输出:</strong>[0,0]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>tomatoSlices = 2, cheeseSlices = 1
|
||||
<strong>输出:</strong>[0,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= tomatoSlices <= 10^7</code></li>
|
||||
<li><code>0 <= cheeseSlices <= 10^7</code></li>
|
||||
</ul>
|
51
算法题(国内版)/problem (Chinese)/不相交的线 [uncrossed-lines].html
Normal file
51
算法题(国内版)/problem (Chinese)/不相交的线 [uncrossed-lines].html
Normal file
@@ -0,0 +1,51 @@
|
||||
<p>在两条独立的水平线上按给定的顺序写下 <code>nums1</code> 和 <code>nums2</code> 中的整数。</p>
|
||||
|
||||
<p>现在,可以绘制一些连接两个数字 <code>nums1[i]</code> 和 <code>nums2[j]</code> 的直线,这些直线需要同时满足满足:</p>
|
||||
|
||||
<ul>
|
||||
<li> <code>nums1[i] == nums2[j]</code></li>
|
||||
<li>且绘制的直线不与任何其他连线(非水平线)相交。</li>
|
||||
</ul>
|
||||
|
||||
<p>请注意,连线即使在端点也不能相交:每个数字只能属于一条连线。</p>
|
||||
|
||||
<p>以这种方法绘制线条,并返回可以绘制的最大连线数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/26/142.png" style="width: 400px; height: 286px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = <span id="example-input-1-1">[1,4,2]</span>, nums2 = <span id="example-input-1-2">[1,2,4]</span>
|
||||
<strong>输出:</strong><span id="example-output-1">2</span>
|
||||
<strong>解释:</strong>可以画出两条不交叉的线,如上图所示。
|
||||
但无法画出第三条不相交的直线,因为从 nums1[1]=4 到 nums2[2]=4 的直线将与从 nums1[2]=2 到 nums2[1]=2 的直线相交。
|
||||
</pre>
|
||||
|
||||
<div>
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = <span id="example-input-2-1">[2,5,1,2,5]</span>, nums2 = <span id="example-input-2-2">[10,5,2,1,5,2]</span>
|
||||
<strong>输出:</strong><span id="example-output-2">3</span>
|
||||
</pre>
|
||||
|
||||
<div>
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = <span id="example-input-3-1">[1,3,7,1,7,5]</span>, nums2 = <span id="example-input-3-2">[1,9,2,5,1]</span>
|
||||
<strong>输出:</strong><span id="example-output-3">2</span></pre>
|
||||
|
||||
<p> </p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 500</code></li>
|
||||
<li><code>1 <= nums1[i], nums2[j] <= 2000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,48 @@
|
||||
<p>有 <code>n</code> 个花园,按从 <code>1</code> 到 <code>n</code> 标记。另有数组 <code>paths</code> ,其中 <code>paths[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 描述了花园 <code>x<sub>i</sub></code> 到花园 <code>y<sub>i</sub></code> 的双向路径。在每个花园中,你打算种下四种花之一。</p>
|
||||
|
||||
<p>另外,所有花园 <strong>最多</strong> 有 <strong>3</strong> 条路径可以进入或离开.</p>
|
||||
|
||||
<p>你需要为每个花园选择一种花,使得通过路径相连的任何两个花园中的花的种类互不相同。</p>
|
||||
|
||||
<p><em>以数组形式返回 <strong>任一</strong> 可行的方案作为答案 <code>answer</code>,其中 <code>answer[i]</code> 为在第 <code>(i+1)</code> 个花园中种植的花的种类。花的种类用 1、2、3、4 表示。保证存在答案。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, paths = [[1,2],[2,3],[3,1]]
|
||||
<strong>输出:</strong>[1,2,3]
|
||||
<strong>解释:</strong>
|
||||
花园 1 和 2 花的种类不同。
|
||||
花园 2 和 3 花的种类不同。
|
||||
花园 3 和 1 花的种类不同。
|
||||
因此,[1,2,3] 是一个满足题意的答案。其他满足题意的答案有 [1,2,4]、[1,4,2] 和 [3,2,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4, paths = [[1,2],[3,4]]
|
||||
<strong>输出:</strong>[1,2,1,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
|
||||
<strong>输出:</strong>[1,2,3,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= paths.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>paths[i].length == 2</code></li>
|
||||
<li><code>1 <= x<sub>i</sub>, y<sub>i</sub> <= n</code></li>
|
||||
<li><code>x<sub>i</sub> != y<sub>i</sub></code></li>
|
||||
<li>每个花园 <strong>最多</strong> 有 <strong>3</strong> 条路径可以进入或离开</li>
|
||||
</ul>
|
45
算法题(国内版)/problem (Chinese)/丑数 III [ugly-number-iii].html
Normal file
45
算法题(国内版)/problem (Chinese)/丑数 III [ugly-number-iii].html
Normal file
@@ -0,0 +1,45 @@
|
||||
<p>给你四个整数:<code>n</code> 、<code>a</code> 、<code>b</code> 、<code>c</code> ,请你设计一个算法来找出第 <code>n</code> 个丑数。</p>
|
||||
|
||||
<p>丑数是可以被 <code>a</code> <strong>或</strong> <code>b</code> <strong>或</strong> <code>c</code> 整除的 <strong>正整数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, a = 2, b = 3, c = 5
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>丑数序列为 2, 3, 4, 5, 6, 8, 9, 10... 其中第 3 个是 4。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4, a = 2, b = 3, c = 4
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>丑数序列为 2, 3, 4, 6, 8, 9, 10, 12... 其中第 4 个是 6。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5, a = 2, b = 11, c = 13
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>丑数序列为 2, 4, 6, 8, 10, 11, 12, 13... 其中第 5 个是 10。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 1000000000, a = 2, b = 217983653, c = 336916467
|
||||
<strong>输出:</strong>1999999984
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n, a, b, c <= 10^9</code></li>
|
||||
<li><code>1 <= a * b * c <= 10^18</code></li>
|
||||
<li>本题结果在 <code>[1, 2 * 10^9]</code> 的范围内</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> ,你需要找到两个 <strong>不重叠</strong><strong>的回文 </strong>子字符串,它们的长度都必须为 <strong>奇数</strong> ,使得它们长度的乘积最大。</p>
|
||||
|
||||
<p>更正式地,你想要选择四个整数 <code>i</code> ,<code>j</code> ,<code>k</code> ,<code>l</code> ,使得 <code>0 <= i <= j < k <= l < s.length</code> ,且子字符串 <code>s[i...j]</code> 和 <code>s[k...l]</code> 都是回文串且长度为奇数。<code>s[i...j]</code> 表示下标从 <code>i</code> 到 <code>j</code> 且 <strong>包含</strong> 两端下标的子字符串。</p>
|
||||
|
||||
<p>请你返回两个不重叠回文子字符串长度的 <strong>最大</strong> 乘积。</p>
|
||||
|
||||
<p><strong>回文字符串</strong> 指的是一个从前往后读和从后往前读一模一样的字符串。<strong>子字符串</strong> 指的是一个字符串中一段连续字符。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "ababbb"
|
||||
<b>输出:</b>9
|
||||
<b>解释:</b>子字符串 "aba" 和 "bbb" 为奇数长度的回文串。乘积为 3 * 3 = 9 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "zaaaxbbby"
|
||||
<b>输出:</b>9
|
||||
<b>解释:</b>子字符串 "aaa" 和 "bbb" 为奇数长度的回文串。乘积为 3 * 3 = 9 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>给你两个数组 <code>nums1</code> 和 <code>nums2</code> 。</p>
|
||||
|
||||
<p>请你返回 <code>nums1</code> 和 <code>nums2</code> 中两个长度相同的 <strong>非空</strong> 子序列的最大点积。</p>
|
||||
|
||||
<p>数组的非空子序列是通过删除原数组中某些元素(可能一个也不删除)后剩余数字组成的序列,但不能改变数字间相对顺序。比方说,<code>[2,3,5]</code> 是 <code>[1,2,3,4,5]</code> 的一个子序列而 <code>[1,5,3]</code> 不是。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [2,1,-2,5], nums2 = [3,0,-6]
|
||||
<strong>输出:</strong>18
|
||||
<strong>解释:</strong>从 nums1 中得到子序列 [2,-2] ,从 nums2 中得到子序列 [3,-6] 。
|
||||
它们的点积为 (2*3 + (-2)*(-6)) = 18 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [3,-2], nums2 = [2,-6,7]
|
||||
<strong>输出:</strong>21
|
||||
<strong>解释:</strong>从 nums1 中得到子序列 [3] ,从 nums2 中得到子序列 [7] 。
|
||||
它们的点积为 (3*7) = 21 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [-1,-1], nums2 = [1,1]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>从 nums1 中得到子序列 [-1] ,从 nums2 中得到子序列 [1] 。
|
||||
它们的点积为 -1 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 500</code></li>
|
||||
<li><code>-1000 <= nums1[i], nums2[i] <= 100</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>点积:</strong></p>
|
||||
|
||||
<pre>
|
||||
定义 <code><strong>a</strong> = [<em>a</em><sub>1</sub>, <em>a</em><sub>2</sub>,…, <em>a</em><sub><em>n</em></sub>]</code> 和<strong> <code>b</code></strong><code> = [<em>b</em><sub>1</sub>, <em>b</em><sub>2</sub>,…, <em>b</em><sub><em>n</em></sub>]</code> 的点积为:
|
||||
|
||||
<img alt="\mathbf{a}\cdot \mathbf{b} = \sum_{i=1}^n a_ib_i = a_1b_1 + a_2b_2 + \cdots + a_nb_n " class="tex" src="http://upload.wikimedia.org/math/c/3/2/c329bf86e747d74f55ed2e17c36fd83f.png" />
|
||||
|
||||
这里的 <strong>Σ</strong> 指示总和符号。
|
||||
</pre>
|
@@ -0,0 +1,55 @@
|
||||
<p>给你两个整数数组 <code>arr1</code> , <code>arr2</code> 和一个整数 <code>d</code> ,请你返回两个数组之间的 <strong>距离值</strong> 。</p>
|
||||
|
||||
<p>「<strong>距离值</strong>」<strong> </strong>定义为符合此距离要求的元素数目:对于元素 <code>arr1[i]</code> ,不存在任何元素 <code>arr2[j]</code> 满足 <code>|arr1[i]-arr2[j]| <= d</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
对于 arr1[0]=4 我们有:
|
||||
|4-10|=6 > d=2
|
||||
|4-9|=5 > d=2
|
||||
|4-1|=3 > d=2
|
||||
|4-8|=4 > d=2
|
||||
所以 arr1[0]=4 符合距离要求
|
||||
|
||||
对于 arr1[1]=5 我们有:
|
||||
|5-10|=5 > d=2
|
||||
|5-9|=4 > d=2
|
||||
|5-1|=4 > d=2
|
||||
|5-8|=3 > d=2
|
||||
所以 arr1[1]=5 也符合距离要求
|
||||
|
||||
对于 arr1[2]=8 我们有:
|
||||
<strong>|8-10|=2 <= d=2</strong>
|
||||
<strong>|8-9|=1 <= d=2</strong>
|
||||
|8-1|=7 > d=2
|
||||
<strong>|8-8|=0 <= d=2</strong>
|
||||
存在距离小于等于 2 的情况,不符合距离要求
|
||||
|
||||
故而只有 arr1[0]=4 和 arr1[1]=5 两个符合距离要求,距离值为 2</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr1.length, arr2.length <= 500</code></li>
|
||||
<li><code>-10^3 <= arr1[i], arr2[j] <= 10^3</code></li>
|
||||
<li><code>0 <= d <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>桌面上有 <code>2n</code> 个颜色不完全相同的球,球上的颜色共有 <code>k</code> 种。给你一个大小为 <code>k</code> 的整数数组 <code>balls</code> ,其中 <code>balls[i]</code> 是颜色为 <code>i</code> 的球的数量。</p>
|
||||
|
||||
<p>所有的球都已经 <strong>随机打乱顺序</strong> ,前 <code>n</code> 个球放入第一个盒子,后 <code>n</code> 个球放入另一个盒子(请认真阅读示例 2 的解释部分)。</p>
|
||||
|
||||
<p><strong>注意:</strong>这两个盒子是不同的。例如,两个球颜色分别为 <code>a</code> 和 <code>b</code>,盒子分别为 <code>[]</code> 和 <code>()</code>,那么 <code>[a] (b)</code> 和 <code>[b] (a)</code> 这两种分配方式是不同的(请认真阅读示例的解释部分)。</p>
|
||||
|
||||
<p>请返回「两个盒子中球的颜色数相同」的情况的概率。答案与真实值误差在 <code>10^-5</code> 以内,则被视为正确答案</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>balls = [1,1]
|
||||
<strong>输出:</strong>1.00000
|
||||
<strong>解释:</strong>球平均分配的方式只有两种:
|
||||
- 颜色为 1 的球放入第一个盒子,颜色为 2 的球放入第二个盒子
|
||||
- 颜色为 2 的球放入第一个盒子,颜色为 1 的球放入第二个盒子
|
||||
这两种分配,两个盒子中球的颜色数都相同。所以概率为 2/2 = 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>balls = [2,1,1]
|
||||
<strong>输出:</strong>0.66667
|
||||
<strong>解释:</strong>球的列表为 [1, 1, 2, 3]
|
||||
随机打乱,得到 12 种等概率的不同打乱方案,每种方案概率为 1/12 :
|
||||
[1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1]
|
||||
然后,我们将前两个球放入第一个盒子,后两个球放入第二个盒子。
|
||||
这 12 种可能的随机打乱方式中的 8 种满足「两个盒子中球的颜色数相同」。
|
||||
概率 = 8/12 = 0.66667
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>balls = [1,2,1,2]
|
||||
<strong>输出:</strong>0.60000
|
||||
<strong>解释:</strong>球的列表为 [1, 2, 2, 3, 4, 4]。要想显示所有 180 种随机打乱方案是很难的,但只检查「两个盒子中球的颜色数相同」的 108 种情况是比较容易的。
|
||||
概率 = 108 / 180 = 0.6 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= balls.length <= 8</code></li>
|
||||
<li><code>1 <= balls[i] <= 6</code></li>
|
||||
<li><code>sum(balls)</code> 是偶数</li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你 <code>root1</code> 和 <code>root2</code> 这两棵二叉搜索树。请你返回一个列表,其中包含 <strong>两棵树 </strong>中的所有整数并按 <strong>升序</strong> 排序。.</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/12/29/q2-e1.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root1 = [2,1,4], root2 = [1,0,3]
|
||||
<strong>输出:</strong>[0,1,1,2,3,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/12/29/q2-e5-.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root1 = [1,null,8], root2 = [8,1]
|
||||
<strong>输出:</strong>[1,1,8,8]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>每棵树的节点数在 <code>[0, 5000]</code> 范围内</li>
|
||||
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给定一个字符串数组 <code>arr</code>,字符串 <code>s</code> 是将 <code>arr</code> 的含有 <strong>不同字母</strong> 的 <strong>子序列</strong> 字符串 <strong>连接</strong> 所得的字符串。</p>
|
||||
|
||||
<p>请返回所有可行解 <code>s</code> 中最长长度。</p>
|
||||
|
||||
<p><strong>子序列</strong> 是一种可以从另一个数组派生而来的数组,通过删除某些元素或不删除元素而不改变其余元素的顺序。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = ["un","iq","ue"]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>所有可能的串联组合是:
|
||||
- ""
|
||||
- "un"
|
||||
- "iq"
|
||||
- "ue"
|
||||
- "uniq" ("un" + "iq")
|
||||
- "ique" ("iq" + "ue")
|
||||
最大长度为 4。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = ["cha","r","act","ers"]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>可能的解答有 "chaers" 和 "acters"。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = ["abcdefghijklmnopqrstuvwxyz"]
|
||||
<strong>输出:</strong>26
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 16</code></li>
|
||||
<li><code>1 <= arr[i].length <= 26</code></li>
|
||||
<li><code>arr[i]</code> 中只含有小写英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>给你一棵以 <code>root</code> 为根的 <strong>二叉树</strong> ,请你返回 <strong>任意</strong> 二叉搜索子树的最大键值和。</p>
|
||||
|
||||
<p>二叉搜索树的定义如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>任意节点的左子树中的键值都 <strong>小于</strong> 此节点的键值。</li>
|
||||
<li>任意节点的右子树中的键值都 <strong>大于</strong> 此节点的键值。</li>
|
||||
<li>任意节点的左子树和右子树都是二叉搜索树。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/03/07/sample_1_1709.png" style="height: 250px; width: 320px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
|
||||
<strong>输出:</strong>20
|
||||
<strong>解释:</strong>键值为 3 的子树是和最大的二叉搜索树。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/03/07/sample_2_1709.png" style="height: 180px; width: 134px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [4,3,null,1,2]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>键值为 2 的单节点子树是和最大的二叉搜索树。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [-4,-2,-5]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>所有节点键值都为负数,和最大的二叉搜索树为空。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [2,1,3]
|
||||
<strong>输出:</strong>6
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [5,4,8,3,null,6,3]
|
||||
<strong>输出:</strong>7
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>每棵树有 <code>1</code> 到 <code>40000</code> 个节点。</li>
|
||||
<li>每个节点的键值在 <code>[-4 * 10^4 , 4 * 10^4]</code> 之间。</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>给你一棵二叉树,每个节点的值为 1 到 9 。我们称二叉树中的一条路径是 「<strong>伪回文</strong>」的,当它满足:路径经过的所有节点值的排列中,存在一个回文序列。</p>
|
||||
|
||||
<p>请你返回从根到叶子节点的所有路径中 <strong>伪回文 </strong>路径的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/05/23/palindromic_paths_1.png" style="height: 201px; width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [2,3,1,3,1,null,1]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>上图为给定的二叉树。总共有 3 条从根到叶子的路径:红色路径 [2,3,3] ,绿色路径 [2,1,1] 和路径 [2,3,1] 。
|
||||
在这些路径中,只有红色和绿色的路径是伪回文路径,因为红色路径 [2,3,3] 存在回文排列 [3,2,3] ,绿色路径 [2,1,1] 存在回文排列 [1,2,1] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/05/23/palindromic_paths_2.png" style="height: 314px; width: 300px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [2,1,1,1,3,null,null,null,null,null,1]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>上图为给定二叉树。总共有 3 条从根到叶子的路径:绿色路径 [2,1,1] ,路径 [2,1,3,1] 和路径 [2,1] 。
|
||||
这些路径中只有绿色路径是伪回文路径,因为 [2,1,1] 存在回文排列 [1,2,1] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [9]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>给定二叉树的节点数目在范围 <code>[1, 10<sup>5</sup>]</code> 内</li>
|
||||
<li><code>1 <= Node.val <= 9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一棵以 <code>root</code> 为根的二叉树和一个 <code>head</code> 为第一个节点的链表。</p>
|
||||
|
||||
<p>如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 <code>head</code> 为首的链表中每个节点的值,那么请你返回 <code>True</code> ,否则返回 <code>False</code> 。</p>
|
||||
|
||||
<p>一直向下的路径的意思是:从树中某个节点开始,一直连续向下的路径。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/29/sample_1_1720.png" style="height: 280px; width: 220px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>树中蓝色的节点构成了与链表对应的子路径。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/29/sample_2_1720.png" style="height: 280px; width: 220px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>二叉树中不存在一一对应链表的路径。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树和链表中的每个节点的值都满足 <code>1 <= node.val <= 100</code> 。</li>
|
||||
<li>链表包含的节点数目在 <code>1</code> 到 <code>100</code> 之间。</li>
|
||||
<li>二叉树包含的节点数目在 <code>1</code> 到 <code>2500</code> 之间。</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你一棵以 <code>root</code> 为根的二叉树,二叉树中的交错路径定义如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择二叉树中 <strong>任意</strong> 节点和一个方向(左或者右)。</li>
|
||||
<li>如果前进方向为右,那么移动到当前节点的的右子节点,否则移动到它的左子节点。</li>
|
||||
<li>改变前进方向:左变右或者右变左。</li>
|
||||
<li>重复第二步和第三步,直到你在树中无法继续移动。</li>
|
||||
</ul>
|
||||
|
||||
<p>交错路径的长度定义为:<strong>访问过的节点数目 - 1</strong>(单个节点的路径长度为 0 )。</p>
|
||||
|
||||
<p>请你返回给定树中最长 <strong>交错路径</strong> 的长度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/03/07/sample_1_1702.png" style="height: 283px; width: 151px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>蓝色节点为树中最长交错路径(右 -> 左 -> 右)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/03/07/sample_2_1702.png" style="height: 253px; width: 120px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,1,1,null,1,null,null,1,1,null,1]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>蓝色节点为树中最长交错路径(左 -> 右 -> 左 -> 右)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>每棵树最多有 <code>50000</code> 个节点。</li>
|
||||
<li>每个节点的值在 <code>[1, 100]</code> 之间。</li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>在一棵无限的二叉树上,每个节点都有两个子节点,树中的节点 <strong>逐行</strong> 依次按 “之” 字形进行标记。</p>
|
||||
|
||||
<p>如下图所示,在奇数行(即,第一行、第三行、第五行……)中,按从左到右的顺序进行标记;</p>
|
||||
|
||||
<p>而偶数行(即,第二行、第四行、第六行……)中,按从右到左的顺序进行标记。</p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/06/28/tree.png" style="height: 138px; width: 300px;"></p>
|
||||
|
||||
<p>给你树上某一个节点的标号 <code>label</code>,请你返回从根节点到该标号为 <code>label</code> 节点的路径,该路径是由途经的节点标号所组成的。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>label = 14
|
||||
<strong>输出:</strong>[1,3,4,14]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>label = 26
|
||||
<strong>输出:</strong>[1,2,6,10,26]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= label <= 10^6</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>有两位极客玩家参与了一场「二叉树着色」的游戏。游戏中,给出二叉树的根节点 <code>root</code>,树上总共有 <code>n</code> 个节点,且 <code>n</code> 为奇数,其中每个节点上的值从 <code>1</code> 到 <code>n</code> 各不相同。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>游戏从「一号」玩家开始(「一号」玩家为红色,「二号」玩家为蓝色),最开始时,</p>
|
||||
|
||||
<p>「一号」玩家从 <code>[1, n]</code> 中取一个值 <code>x</code>(<code>1 <= x <= n</code>);</p>
|
||||
|
||||
<p>「二号」玩家也从 <code>[1, n]</code> 中取一个值 <code>y</code>(<code>1 <= y <= n</code>)且 <code>y != x</code>。</p>
|
||||
|
||||
<p>「一号」玩家给值为 <code>x</code> 的节点染上红色,而「二号」玩家给值为 <code>y</code> 的节点染上蓝色。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>之后两位玩家轮流进行操作,每一回合,玩家选择一个他之前涂好颜色的节点,将所选节点一个 <strong>未着色 </strong>的邻节点(即左右子节点、或父节点)进行染色。</p>
|
||||
|
||||
<p>如果当前玩家无法找到这样的节点来染色时,他的回合就会被跳过。</p>
|
||||
|
||||
<p>若两个玩家都没有可以染色的节点时,游戏结束。着色节点最多的那位玩家获得胜利 ✌️。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>现在,假设你是「二号」玩家,根据所给出的输入,假如存在一个 <code>y</code> 值可以确保你赢得这场游戏,则返回 <code>true</code>;若无法获胜,就请返回 <code>false</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/08/04/1480-binary-tree-coloring-game.png" style="height: 186px; width: 300px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,2,3,4,5,6,7,8,9,10,11], n = 11, x = 3
|
||||
<strong>输出:</strong>True
|
||||
<strong>解释:</strong>第二个玩家可以选择值为 2 的节点。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树的根节点为 <code>root</code>,树上由 <code>n</code> 个节点,节点上的值从 <code>1</code> 到 <code>n</code> 各不相同。</li>
|
||||
<li><code>n</code> 为奇数。</li>
|
||||
<li><code>1 <= x <= n <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/11/leetcode_keyboard.png" /></p>
|
||||
|
||||
<p>二指输入法定制键盘在 <strong>X-Y</strong> 平面上的布局如上图所示,其中每个大写英文字母都位于某个坐标处。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如字母 <strong>A</strong> 位于坐标 <strong>(0,0)</strong>,字母 <strong>B</strong> 位于坐标 <strong>(0,1)</strong>,字母 <strong>P</strong> 位于坐标 <strong>(2,3)</strong> 且字母 <strong>Z</strong> 位于坐标 <strong>(4,1)</strong>。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个待输入字符串 <code>word</code>,请你计算并返回在仅使用两根手指的情况下,键入该字符串需要的最小移动总距离。</p>
|
||||
|
||||
<p>坐标<code> <strong>(x<sub>1</sub>,y<sub>1</sub>)</strong> </code>和 <code><strong>(x<sub>2</sub>,y<sub>2</sub>)</strong></code> 之间的 <strong>距离</strong> 是 <code><strong>|x<sub>1</sub> - x<sub>2</sub>| + |y<sub>1</sub> - y<sub>2</sub>|</strong></code>。 </p>
|
||||
|
||||
<p><strong>注意</strong>,两根手指的起始位置是零代价的,不计入移动总距离。你的两根手指的起始位置也不必从首字母或者前两个字母开始。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "CAKE"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:
|
||||
</strong>使用两根手指输入 "CAKE" 的最佳方案之一是:
|
||||
手指 1 在字母 'C' 上 -> 移动距离 = 0
|
||||
手指 1 在字母 'A' 上 -> 移动距离 = 从字母 'C' 到字母 'A' 的距离 = 2
|
||||
手指 2 在字母 'K' 上 -> 移动距离 = 0
|
||||
手指 2 在字母 'E' 上 -> 移动距离 = 从字母 'K' 到字母 'E' 的距离 = 1
|
||||
总距离 = 3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "HAPPY"
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释: </strong>
|
||||
使用两根手指输入 "HAPPY" 的最佳方案之一是:
|
||||
手指 1 在字母 'H' 上 -> 移动距离 = 0
|
||||
手指 1 在字母 'A' 上 -> 移动距离 = 从字母 'H' 到字母 'A' 的距离 = 2
|
||||
手指 2 在字母 'P' 上 -> 移动距离 = 0
|
||||
手指 2 在字母 'P' 上 -> 移动距离 = 从字母 'P' 到字母 'P' 的距离 = 0
|
||||
手指 1 在字母 'Y' 上 -> 移动距离 = 从字母 'A' 到字母 'Y' 的距离 = 4
|
||||
总距离 = 6
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= word.length <= 300</code></li>
|
||||
<li>每个 <code>word[i]</code> 都是一个大写英文字母。</li>
|
||||
</ul>
|
51
算法题(国内版)/problem (Chinese)/二维网格迁移 [shift-2d-grid].html
Normal file
51
算法题(国内版)/problem (Chinese)/二维网格迁移 [shift-2d-grid].html
Normal file
@@ -0,0 +1,51 @@
|
||||
<p>给你一个 <code>m</code> 行 <code>n</code> 列的二维网格 <code>grid</code> 和一个整数 <code>k</code>。你需要将 <code>grid</code> 迁移 <code>k</code> 次。</p>
|
||||
|
||||
<p>每次「迁移」操作将会引发下述活动:</p>
|
||||
|
||||
<ul>
|
||||
<li>位于 <code>grid[i][j]</code> 的元素将会移动到 <code>grid[i][j + 1]</code>。</li>
|
||||
<li>位于 <code>grid[i][n - 1]</code> 的元素将会移动到 <code>grid[i + 1][0]</code>。</li>
|
||||
<li>位于 <code>grid[m - 1][n - 1]</code> 的元素将会移动到 <code>grid[0][0]</code>。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 <code>k</code> 次迁移操作后最终得到的 <strong>二维网格</strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/16/e1-1.png" style="height: 158px; width: 400px;" /></p>
|
||||
|
||||
<pre>
|
||||
<code><strong>输入:</strong>grid</code> = [[1,2,3],[4,5,6],[7,8,9]], k = 1
|
||||
<strong>输出:</strong>[[9,1,2],[3,4,5],[6,7,8]]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/16/e2-1.png" style="height: 166px; width: 400px;" /></p>
|
||||
|
||||
<pre>
|
||||
<code><strong>输入:</strong>grid</code> = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
|
||||
<strong>输出:</strong>[[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<code><strong>输入:</strong>grid</code> = [[1,2,3],[4,5,6],[7,8,9]], k = 9
|
||||
<strong>输出:</strong>[[1,2,3],[4,5,6],[7,8,9]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m <= 50</code></li>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>-1000 <= grid[i][j] <= 1000</code></li>
|
||||
<li><code>0 <= k <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>给你一个长度为 <code>n</code> 、下标从 <strong>1</strong> 开始的二进制字符串,所有位最开始都是 <code>0</code> 。我们会按步翻转该二进制字符串的所有位(即,将 <code>0</code> 变为 <code>1</code>)。</p>
|
||||
|
||||
<p>给你一个下标从 <strong>1</strong> 开始的整数数组 <code>flips</code> ,其中 <code>flips[i]</code> 表示对应下标 <code>i</code> 的位将会在第 <code>i</code> 步翻转。</p>
|
||||
|
||||
<p>二进制字符串 <strong>前缀一致</strong> 需满足:在第 <code>i</code> 步之后,在 <strong>闭</strong> 区间 <code>[1, i]</code> 内的所有位都是 1 ,而其他位都是 0 。</p>
|
||||
|
||||
<p>返回二进制字符串在翻转过程中 <strong>前缀一致</strong> 的次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>flips = [3,2,4,1,5]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>二进制字符串最开始是 "00000" 。
|
||||
执行第 1 步:字符串变为 "00100" ,不属于前缀一致的情况。
|
||||
执行第 2 步:字符串变为 "01100" ,不属于前缀一致的情况。
|
||||
执行第 3 步:字符串变为 "01110" ,不属于前缀一致的情况。
|
||||
执行第 4 步:字符串变为 "11110" ,属于前缀一致的情况。
|
||||
执行第 5 步:字符串变为 "11111" ,属于前缀一致的情况。
|
||||
在翻转过程中,前缀一致的次数为 2 ,所以返回 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>flips = [4,1,2,3]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>二进制字符串最开始是 "0000" 。
|
||||
执行第 1 步:字符串变为 "0001" ,不属于前缀一致的情况。
|
||||
执行第 2 步:字符串变为 "1001" ,不属于前缀一致的情况。
|
||||
执行第 3 步:字符串变为 "1101" ,不属于前缀一致的情况。
|
||||
执行第 4 步:字符串变为 "1111" ,属于前缀一致的情况。
|
||||
在翻转过程中,前缀一致的次数为 1 ,所以返回 1 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == flips.length</code></li>
|
||||
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>flips</code> 是范围 <code>[1, n]</code> 中所有整数构成的一个排列</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你一个 <code>n x n</code> 的二进制矩阵 <code>grid</code> 中,返回矩阵中最短 <strong>畅通路径</strong> 的长度。如果不存在这样的路径,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p>二进制矩阵中的 畅通路径 是一条从 <strong>左上角</strong> 单元格(即,<code>(0, 0)</code>)到 右下角 单元格(即,<code>(n - 1, n - 1)</code>)的路径,该路径同时满足下述要求:</p>
|
||||
|
||||
<ul>
|
||||
<li>路径途经的所有单元格都的值都是 <code>0</code> 。</li>
|
||||
<li>路径中所有相邻的单元格应当在 <strong>8 个方向之一</strong> 上连通(即,相邻两单元之间彼此不同且共享一条边或者一个角)。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>畅通路径的长度</strong> 是该路径途经的单元格总数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example1_1.png" style="width: 500px; height: 234px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,1],[1,0]]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example2_1.png" style="height: 216px; width: 500px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,0,0],[1,1,0],[1,1,0]]
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,0,0],[1,1,0],[1,1,0]]
|
||||
<strong>输出:</strong>-1
|
||||
</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 <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> 为 <code>0</code> 或 <code>1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>给你一个单链表的引用结点 <code>head</code>。链表中每个结点的值不是 0 就是 1。已知此链表是一个整数数字的二进制表示形式。</p>
|
||||
|
||||
<p>请你返回该链表所表示数字的 <strong>十进制值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/12/15/graph-1.png" style="height: 108px; width: 426px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,0,1]
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>二进制数 (101) 转化为十进制数 (5)
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [0]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
|
||||
<strong>输出:</strong>18880
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [0,0]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>链表不为空。</li>
|
||||
<li>链表的结点总数不超过 <code>30</code>。</li>
|
||||
<li>每个结点的值不是 <code>0</code> 就是 <code>1</code>。</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>给你一个正整数的数组 <code>A</code>(其中的元素不一定完全不同),请你返回可在 <strong>一次交换</strong>(交换两数字 <code>A[i]</code> 和 <code>A[j]</code> 的位置)后得到的、按字典序排列小于 <code>A</code> 的最大可能排列。</p>
|
||||
|
||||
<p>如果无法这么操作,就请返回原数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [3,2,1]
|
||||
<strong>输出:</strong>[3,1,2]
|
||||
<strong>解释:</strong>交换 2 和 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,1,5]
|
||||
<strong>输出:</strong>[1,1,5]
|
||||
<strong>解释:</strong>已经是最小排列
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,9,4,6,7]
|
||||
<strong>输出:</strong>[1,7,4,6,9]
|
||||
<strong>解释:</strong>交换 9 和 7
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [3,1,1,3]
|
||||
<strong>输出:</strong>[1,3,1,3]
|
||||
<strong>解释:</strong>交换 1 和 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= arr[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>给你一个字符串 <code>s</code>,以及该字符串中的一些「索引对」数组 <code>pairs</code>,其中 <code>pairs[i] = [a, b]</code> 表示字符串中的两个索引(编号从 0 开始)。</p>
|
||||
|
||||
<p>你可以 <strong>任意多次交换</strong> 在 <code>pairs</code> 中任意一对索引处的字符。</p>
|
||||
|
||||
<p>返回在经过若干次交换后,<code>s</code> 可以变成的按字典序最小的字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "dcab", pairs = [[0,3],[1,2]]
|
||||
<strong>输出:</strong>"bacd"
|
||||
<strong>解释:</strong>
|
||||
交换 s[0] 和 s[3], s = "bcad"
|
||||
交换 s[1] 和 s[2], s = "bacd"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "dcab", pairs = [[0,3],[1,2],[0,2]]
|
||||
<strong>输出:</strong>"abcd"
|
||||
<strong>解释:</strong>
|
||||
交换 s[0] 和 s[3], s = "bcad"
|
||||
交换 s[0] 和 s[2], s = "acbd"
|
||||
交换 s[1] 和 s[2], s = "abcd"</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "cba", pairs = [[0,1],[1,2]]
|
||||
<strong>输出:</strong>"abc"
|
||||
<strong>解释:</strong>
|
||||
交换 s[0] 和 s[1], s = "bca"
|
||||
交换 s[1] 和 s[2], s = "bac"
|
||||
交换 s[0] 和 s[1], s = "abc"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10^5</code></li>
|
||||
<li><code>0 <= pairs.length <= 10^5</code></li>
|
||||
<li><code>0 <= pairs[i][0], pairs[i][1] < s.length</code></li>
|
||||
<li><code>s</code> 中只含有小写英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>有两个长度相同的字符串 <code>s1</code> 和 <code>s2</code>,且它们其中 <strong>只含有</strong> 字符 <code>"x"</code> 和 <code>"y"</code>,你需要通过「交换字符」的方式使这两个字符串相同。</p>
|
||||
|
||||
<p>每次「交换字符」的时候,你都可以在两个字符串中各选一个字符进行交换。</p>
|
||||
|
||||
<p>交换只能发生在两个不同的字符串之间,绝对不能发生在同一个字符串内部。也就是说,我们可以交换 <code>s1[i]</code> 和 <code>s2[j]</code>,但不能交换 <code>s1[i]</code> 和 <code>s1[j]</code>。</p>
|
||||
|
||||
<p>最后,请你返回使 <code>s1</code> 和 <code>s2</code> 相同的最小交换次数,如果没有方法能够使得这两个字符串相同,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "xx", s2 = "yy"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:
|
||||
</strong>交换 s1[0] 和 s2[1],得到 s1 = "yx",s2 = "yx"。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "xy", s2 = "yx"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:
|
||||
</strong>交换 s1[0] 和 s2[0],得到 s1 = "yy",s2 = "xx" 。
|
||||
交换 s1[0] 和 s2[1],得到 s1 = "xy",s2 = "xy" 。
|
||||
注意,你不能交换 s1[0] 和 s1[1] 使得 s1 变成 "yx",因为我们只能交换属于两个不同字符串的字符。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "xx", s2 = "xy"
|
||||
<strong>输出:</strong>-1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length <= 1000</code></li>
|
||||
<li><code>s1, s2</code> 只包含 <code>'x'</code> 或 <code>'y'</code>。</li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>给你一个类:</p>
|
||||
|
||||
<pre>
|
||||
class FooBar {
|
||||
public void foo() {
|
||||
for (int i = 0; i < n; i++) {
|
||||
print("foo");
|
||||
}
|
||||
}
|
||||
|
||||
public void bar() {
|
||||
for (int i = 0; i < n; i++) {
|
||||
print("bar");
|
||||
}
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>两个不同的线程将会共用一个 <code>FooBar</code> 实例:</p>
|
||||
|
||||
<ul>
|
||||
<li>线程 A 将会调用 <code>foo()</code> 方法,而</li>
|
||||
<li>线程 B 将会调用 <code>bar()</code> 方法</li>
|
||||
</ul>
|
||||
|
||||
<p>请设计修改程序,以确保 <code>"foobar"</code> 被输出 <code>n</code> 次。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 1
|
||||
<strong>输出:</strong>"foobar"
|
||||
<strong>解释:</strong>这里有两个线程被异步启动。其中一个调用 foo() 方法, 另一个调用 bar() 方法,"foobar" 将被输出一次。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2
|
||||
<strong>输出:</strong>"foobarfoobar"
|
||||
<strong>解释:</strong>"foobar" 将被输出两次。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>编写一个可以从 1 到 n 输出代表这个数字的字符串的程序,但是:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果这个数字可以被 3 整除,输出 "fizz"。</li>
|
||||
<li>如果这个数字可以被 5 整除,输出 "buzz"。</li>
|
||||
<li>如果这个数字可以同时被 3 和 5 整除,输出 "fizzbuzz"。</li>
|
||||
</ul>
|
||||
|
||||
<p>例如,当 <code>n = 15</code>,输出: <code>1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz</code>。</p>
|
||||
|
||||
<p>假设有这么一个类:</p>
|
||||
|
||||
<pre>
|
||||
class FizzBuzz {
|
||||
public FizzBuzz(int n) { ... } // constructor
|
||||
public void fizz(printFizz) { ... } // only output "fizz"
|
||||
public void buzz(printBuzz) { ... } // only output "buzz"
|
||||
public void fizzbuzz(printFizzBuzz) { ... } // only output "fizzbuzz"
|
||||
public void number(printNumber) { ... } // only output the numbers
|
||||
}</pre>
|
||||
|
||||
<p>请你实现一个有四个线程的多线程版 <code>FizzBuzz</code>, 同一个 <code>FizzBuzz</code> 实例会被如下四个线程使用:</p>
|
||||
|
||||
<ol>
|
||||
<li>线程A将调用 <code>fizz()</code> 来判断是否能被 3 整除,如果可以,则输出 <code>fizz</code>。</li>
|
||||
<li>线程B将调用 <code>buzz()</code> 来判断是否能被 5 整除,如果可以,则输出 <code>buzz</code>。</li>
|
||||
<li>线程C将调用 <code>fizzbuzz()</code> 来判断是否同时能被 3 和 5 整除,如果可以,则输出 <code>fizzbuzz</code>。</li>
|
||||
<li>线程D将调用 <code>number()</code> 来实现输出既不能被 3 整除也不能被 5 整除的数字。</li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>本题已经提供了打印字符串的相关方法,如 <code>printFizz()</code> 等,具体方法名请参考答题模板中的注释部分。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,45 @@
|
||||
<p>给你一个二进制字符串 <code>s</code>(仅由 '0' 和 '1' 组成的字符串)。</p>
|
||||
|
||||
<p>返回所有字符都为 1 的子字符串的数目。</p>
|
||||
|
||||
<p>由于答案可能很大,请你将它对 10^9 + 7 取模后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "0110111"
|
||||
<strong>输出</strong>:9
|
||||
<strong>解释:</strong>共有 9 个子字符串仅由 '1' 组成
|
||||
"1" -> 5 次
|
||||
"11" -> 3 次
|
||||
"111" -> 1 次</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "101"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>子字符串 "1" 在 s 中共出现 2 次
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "111111"
|
||||
<strong>输出:</strong>21
|
||||
<strong>解释:</strong>每个子字符串都仅由 '1' 组成
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "000"
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>s[i] == '0'</code> 或 <code>s[i] == '1'</code></li>
|
||||
<li><code>1 <= s.length <= 10^5</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p><span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">给定一个二叉搜索树</font></span></span></span></span> <code>root</code> (BST)<span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">,请将它的每个</font></span></span></span></span>节点<span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">的值替换成树中大于或者等于该</font></span></span></span></span>节点<span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">值的所有</font></span></span></span></span>节点<span style="font-size:10.5pt"><span style="font-family:Calibri"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">值之和。</font></span></span></span></span></p>
|
||||
|
||||
<p>提醒一下, <em>二叉搜索树</em> 满足下列约束条件:</p>
|
||||
|
||||
<ul>
|
||||
<li>节点的左子树仅包含键<strong> 小于 </strong>节点键的节点。</li>
|
||||
<li>节点的右子树仅包含键<strong> 大于</strong> 节点键的节点。</li>
|
||||
<li>左右子树也必须是二叉搜索树。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/05/03/tree.png" style="height:273px; width:400px" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
|
||||
<strong>输出:</strong>[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [0,null,1]
|
||||
<strong>输出:</strong>[1,null,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中的节点数在 <code>[1, 100]</code> 范围内。</li>
|
||||
<li><code>0 <= Node.val <= 100</code></li>
|
||||
<li>树中的所有值均 <strong>不重复</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>该题目与 538: <a href="https://leetcode-cn.com/problems/convert-bst-to-greater-tree/">https://leetcode-cn.com/problems/convert-bst-to-greater-tree/ </a>相同</p>
|
@@ -0,0 +1,39 @@
|
||||
<p>一张桌子上总共有 <code>n</code> 个硬币 <b>栈</b> 。每个栈有 <strong>正整数</strong> 个带面值的硬币。</p>
|
||||
|
||||
<p>每一次操作中,你可以从任意一个栈的 <strong>顶部</strong> 取出 1 个硬币,从栈中移除它,并放入你的钱包里。</p>
|
||||
|
||||
<p>给你一个列表 <code>piles</code> ,其中 <code>piles[i]</code> 是一个整数数组,分别表示第 <code>i</code> 个栈里 <strong>从顶到底</strong> 的硬币面值。同时给你一个正整数 <code>k</code> ,请你返回在 <strong>恰好</strong> 进行 <code>k</code> 次操作的前提下,你钱包里硬币面值之和 <strong>最大为多少</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/09/e1.png" style="width: 600px; height: 243px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>piles = [[1,100,3],[7,8,9]], k = 2
|
||||
<b>输出:</b>101
|
||||
<strong>解释:</strong>
|
||||
上图展示了几种选择 k 个硬币的不同方法。
|
||||
我们可以得到的最大面值为 101 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7
|
||||
<b>输出:</b>706
|
||||
<strong>解释:
|
||||
</strong>如果我们所有硬币都从最后一个栈中取,可以得到最大面值和。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == piles.length</code></li>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
<li><code>1 <= piles[i][j] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= sum(piles[i].length) <= 2000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个链表的头节点 <code>head</code>,请你编写代码,反复删去链表中由 <strong>总和</strong> 值为 <code>0</code> 的连续节点组成的序列,直到不存在这样的序列为止。</p>
|
||||
|
||||
<p>删除完毕后,请你返回最终结果链表的头节点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>你可以返回任何满足题目要求的答案。</p>
|
||||
|
||||
<p>(注意,下面示例中的所有序列,都是对 <code>ListNode</code> 对象序列化的表示。)</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,2,-3,3,1]
|
||||
<strong>输出:</strong>[3,1]
|
||||
<strong>提示:</strong>答案 [1,2,1] 也是正确的。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,2,3,-3,4]
|
||||
<strong>输出:</strong>[1,2,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>head = [1,2,3,-3,-2]
|
||||
<strong>输出:</strong>[1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>给你的链表中可能有 <code>1</code> 到 <code>1000</code> 个节点。</li>
|
||||
<li>对于链表中的每个节点,节点的值:<code>-1000 <= node.val <= 1000</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,72 @@
|
||||
<p>给你 <code>n</code> 个盒子,每个盒子的格式为 <code>[status, candies, keys, containedBoxes]</code> ,其中:</p>
|
||||
|
||||
<ul>
|
||||
<li>状态字 <code>status[i]</code>:整数,如果 <code>box[i]</code> 是开的,那么是 <strong>1 </strong>,否则是 <strong>0 </strong>。</li>
|
||||
<li>糖果数 <code>candies[i]</code>: 整数,表示 <code>box[i]</code> 中糖果的数目。</li>
|
||||
<li>钥匙 <code>keys[i]</code>:数组,表示你打开 <code>box[i]</code> 后,可以得到一些盒子的钥匙,每个元素分别为该钥匙对应盒子的下标。</li>
|
||||
<li>内含的盒子 <code>containedBoxes[i]</code>:整数,表示放在 <code>box[i]</code> 里的盒子所对应的下标。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个 <code>initialBoxes</code> 数组,表示你现在得到的盒子,你可以获得里面的糖果,也可以用盒子里的钥匙打开新的盒子,还可以继续探索从这个盒子里找到的其他盒子。</p>
|
||||
|
||||
<p>请你按照上述规则,返回可以获得糖果的 <strong>最大数目 </strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
|
||||
<strong>输出:</strong>16
|
||||
<strong>解释:
|
||||
</strong>一开始你有盒子 0 。你将获得它里面的 7 个糖果和盒子 1 和 2。
|
||||
盒子 1 目前状态是关闭的,而且你还没有对应它的钥匙。所以你将会打开盒子 2 ,并得到里面的 4 个糖果和盒子 1 的钥匙。
|
||||
在盒子 1 中,你会获得 5 个糖果和盒子 3 ,但是你没法获得盒子 3 的钥匙所以盒子 3 会保持关闭状态。
|
||||
你总共可以获得的糖果数目 = 7 + 4 + 5 = 16 个。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:
|
||||
</strong>你一开始拥有盒子 0 。打开它你可以找到盒子 1,2,3,4,5 和它们对应的钥匙。
|
||||
打开这些盒子,你将获得所有盒子的糖果,所以总糖果数为 6 个。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = []
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0]
|
||||
<strong>输出:</strong>7
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= status.length <= 1000</code></li>
|
||||
<li><code>status.length == candies.length == keys.length == containedBoxes.length == n</code></li>
|
||||
<li><code>status[i]</code> 要么是 <code>0</code> 要么是 <code>1</code> 。</li>
|
||||
<li><code>1 <= candies[i] <= 1000</code></li>
|
||||
<li><code>0 <= keys[i].length <= status.length</code></li>
|
||||
<li><code>0 <= keys[i][j] < status.length</code></li>
|
||||
<li><code>keys[i]</code> 中的值都是互不相同的。</li>
|
||||
<li><code>0 <= containedBoxes[i].length <= status.length</code></li>
|
||||
<li><code>0 <= containedBoxes[i][j] < status.length</code></li>
|
||||
<li><code>containedBoxes[i]</code> 中的值都是互不相同的。</li>
|
||||
<li>每个盒子最多被一个盒子包含。</li>
|
||||
<li><code>0 <= initialBoxes.length <= status.length</code></li>
|
||||
<li><code>0 <= initialBoxes[i] < status.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给你两个整数数组 <code>arr1</code> 和 <code>arr2</code>,返回使 <code>arr1</code> 严格递增所需要的最小「操作」数(可能为 0)。</p>
|
||||
|
||||
<p>每一步「操作」中,你可以分别从 <code>arr1</code> 和 <code>arr2</code> 中各选出一个索引,分别为 <code>i</code> 和 <code>j</code>,<code>0 <= i < arr1.length</code> 和 <code>0 <= j < arr2.length</code>,然后进行赋值运算 <code>arr1[i] = arr2[j]</code>。</p>
|
||||
|
||||
<p>如果无法让 <code>arr1</code> 严格递增,请返回 <code>-1</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>用 2 来替换 <code>5,之后</code> <code>arr1 = [1, 2, 3, 6, 7]</code>。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr1 = [1,5,3,6,7], arr2 = [4,3,1]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>用 3 来替换 <code>5,然后</code>用 4 来替换 3<code>,得到</code> <code>arr1 = [1, 3, 4, 6, 7]</code>。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>无法使 <code>arr1 严格递增</code>。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr1.length, arr2.length <= 2000</code></li>
|
||||
<li><code>0 <= arr1[i], arr2[i] <= 10^9</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个正整数 <code>threshold</code> ,你需要选择一个正整数作为除数,然后将数组里每个数都除以它,并对除法结果求和。</p>
|
||||
|
||||
<p>请你找出能够使上述结果小于等于阈值 <code>threshold</code> 的除数中 <strong>最小</strong> 的那个。</p>
|
||||
|
||||
<p>每个数除以除数后都向上取整,比方说 7/3 = 3 , 10/2 = 5 。</p>
|
||||
|
||||
<p>题目保证一定有解。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,5,9], threshold = 6
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>如果除数为 1 ,我们可以得到和为 17 (1+2+5+9)。
|
||||
如果除数为 4 ,我们可以得到和为 7 (1+1+2+3) 。如果除数为 5 ,和为 5 (1+1+1+2)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,3,5,7,11], threshold = 11
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [19], threshold = 5
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5 * 10^4</code></li>
|
||||
<li><code>1 <= nums[i] <= 10^6</code></li>
|
||||
<li><code>nums.length <= threshold <= 10^6</code></li>
|
||||
</ul>
|
@@ -0,0 +1,68 @@
|
||||
<p>给你一个 m x n 的网格图 <code>grid</code> 。 <code>grid</code> 中每个格子都有一个数字,对应着从该格子出发下一步走的方向。 <code>grid[i][j]</code> 中的数字可能为以下几种情况:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>1</strong> ,下一步往右走,也就是你会从 <code>grid[i][j]</code> 走到 <code>grid[i][j + 1]</code></li>
|
||||
<li><strong>2</strong> ,下一步往左走,也就是你会从 <code>grid[i][j]</code> 走到 <code>grid[i][j - 1]</code></li>
|
||||
<li><strong>3</strong> ,下一步往下走,也就是你会从 <code>grid[i][j]</code> 走到 <code>grid[i + 1][j]</code></li>
|
||||
<li><strong>4</strong> ,下一步往上走,也就是你会从 <code>grid[i][j]</code> 走到 <code>grid[i - 1][j]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意网格图中可能会有 <strong>无效数字</strong> ,因为它们可能指向 <code>grid</code> 以外的区域。</p>
|
||||
|
||||
<p>一开始,你会从最左上角的格子 <code>(0,0)</code> 出发。我们定义一条 <strong>有效路径</strong> 为从格子 <code>(0,0)</code> 出发,每一步都顺着数字对应方向走,最终在最右下角的格子 <code>(m - 1, n - 1)</code> 结束的路径。有效路径 <strong>不需要是最短路径</strong> 。</p>
|
||||
|
||||
<p>你可以花费 <code>cost = 1</code> 的代价修改一个格子中的数字,但每个格子中的数字 <strong>只能修改一次</strong> 。</p>
|
||||
|
||||
<p>请你返回让网格图至少有一条有效路径的最小代价。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/29/grid1.png" style="height: 528px; width: 542px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>你将从点 (0, 0) 出发。
|
||||
到达 (3, 3) 的路径为: (0, 0) --> (0, 1) --> (0, 2) --> (0, 3) 花费代价 cost = 1 使方向向下 --> (1, 3) --> (1, 2) --> (1, 1) --> (1, 0) 花费代价 cost = 1 使方向向下 --> (2, 0) --> (2, 1) --> (2, 2) --> (2, 3) 花费代价 cost = 1 使方向向下 --> (3, 3)
|
||||
总花费为 cost = 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/29/grid2.png" style="height: 408px; width: 419px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[1,1,3],[3,2,2],[1,1,4]]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>不修改任何数字你就可以从 (0, 0) 到达 (2, 2) 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/29/grid3.png" style="height: 302px; width: 314px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[1,2],[4,3]]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[2,2,2],[2,2,2]]
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>grid = [[4]]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>给你一个长度为 <code>n</code> 的字符串数组 <code>names</code> 。你将会在文件系统中创建 <code>n</code> 个文件夹:在第 <code>i</code> 分钟,新建名为 <code>names[i]</code> 的文件夹。</p>
|
||||
|
||||
<p>由于两个文件 <strong>不能</strong> 共享相同的文件名,因此如果新建文件夹使用的文件名已经被占用,系统会以 <code>(k)</code> 的形式为新文件夹的文件名添加后缀,其中 <code>k</code> 是能保证文件名唯一的 <strong>最小正整数</strong> 。</p>
|
||||
|
||||
<p>返回长度为<em> <code>n</code></em> 的字符串数组,其中 <code>ans[i]</code> 是创建第 <code>i</code> 个文件夹时系统分配给该文件夹的实际名称。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>names = ["pes","fifa","gta","pes(2019)"]
|
||||
<strong>输出:</strong>["pes","fifa","gta","pes(2019)"]
|
||||
<strong>解释:</strong>文件系统将会这样创建文件名:
|
||||
"pes" --> 之前未分配,仍为 "pes"
|
||||
"fifa" --> 之前未分配,仍为 "fifa"
|
||||
"gta" --> 之前未分配,仍为 "gta"
|
||||
"pes(2019)" --> 之前未分配,仍为 "pes(2019)"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>names = ["gta","gta(1)","gta","avalon"]
|
||||
<strong>输出:</strong>["gta","gta(1)","gta(2)","avalon"]
|
||||
<strong>解释:</strong>文件系统将会这样创建文件名:
|
||||
"gta" --> 之前未分配,仍为 "gta"
|
||||
"gta(1)" --> 之前未分配,仍为 "gta(1)"
|
||||
"gta" --> 文件名被占用,系统为该名称添加后缀 (k),由于 "gta(1)" 也被占用,所以 k = 2 。实际创建的文件名为 "gta(2)" 。
|
||||
"avalon" --> 之前未分配,仍为 "avalon"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"]
|
||||
<strong>输出:</strong>["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"]
|
||||
<strong>解释:</strong>当创建最后一个文件夹时,最小的正有效 k 为 4 ,文件名变为 "onepiece(4)"。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>names = ["wano","wano","wano","wano"]
|
||||
<strong>输出:</strong>["wano","wano(1)","wano(2)","wano(3)"]
|
||||
<strong>解释:</strong>每次创建文件夹 "wano" 时,只需增加后缀中 k 的值即可。</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>names = ["kaido","kaido(1)","kaido","kaido(1)"]
|
||||
<strong>输出:</strong>["kaido","kaido(1)","kaido(2)","kaido(1)(1)"]
|
||||
<strong>解释:</strong>注意,如果含后缀文件名被占用,那么系统也会按规则在名称后添加新的后缀 (k) 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= names.length <= 5 * 10^4</code></li>
|
||||
<li><code>1 <= names[i].length <= 20</code></li>
|
||||
<li><code>names[i]</code> 由小写英文字母、数字和/或圆括号组成。</li>
|
||||
</ul>
|
42
算法题(国内版)/problem (Chinese)/做菜顺序 [reducing-dishes].html
Normal file
42
算法题(国内版)/problem (Chinese)/做菜顺序 [reducing-dishes].html
Normal file
@@ -0,0 +1,42 @@
|
||||
<p>一个厨师收集了他 <code>n</code> 道菜的满意程度 <code>satisfaction</code> ,这个厨师做出每道菜的时间都是 1 单位时间。</p>
|
||||
|
||||
<p>一道菜的 「喜爱时间」系数定义为烹饪这道菜以及之前每道菜所花费的时间乘以这道菜的满意程度,也就是 <code>time[i]</code>*<code>satisfaction[i]</code> 。</p>
|
||||
|
||||
<p>请你返回做完所有菜 「喜爱时间」总和的最大值为多少。</p>
|
||||
|
||||
<p>你可以按 <strong>任意</strong> 顺序安排做菜的顺序,你也可以选择放弃做某些菜来获得更大的总和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>satisfaction = [-1,-8,0,5,-9]
|
||||
<strong>输出:</strong>14
|
||||
<strong>解释:</strong>去掉第二道和最后一道菜,最大的喜爱时间系数和为 (-1*1 + 0*2 + 5*3 = 14) 。每道菜都需要花费 1 单位时间完成。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>satisfaction = [4,3,2]
|
||||
<strong>输出:</strong>20
|
||||
<strong>解释:</strong>按照原来顺序相反的时间做菜 (2*1 + 3*2 + 4*3 = 20)
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>satisfaction = [-1,-4,-5]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>大家都不喜欢这些菜,所以不做任何菜可以获得最大的喜爱时间系数。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == satisfaction.length</code></li>
|
||||
<li><code>1 <= n <= 500</code></li>
|
||||
<li><code>-1000 <= satisfaction[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>有一个长度为 <code>arrLen</code> 的数组,开始有一个指针在索引 <code>0</code> 处。</p>
|
||||
|
||||
<p>每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指针不能被移动到数组范围外)。</p>
|
||||
|
||||
<p>给你两个整数 <code>steps</code> 和 <code>arrLen</code> ,请你计算并返回:在恰好执行 <code>steps</code> 次操作以后,指针仍然指向索引 <code>0</code> 处的方案数。</p>
|
||||
|
||||
<p>由于答案可能会很大,请返回方案数 <strong>模</strong> <code>10^9 + 7</code> 后的结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>steps = 3, arrLen = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>3 步后,总共有 4 种不同的方法可以停在索引 0 处。
|
||||
向右,向左,不动
|
||||
不动,向右,向左
|
||||
向右,不动,向左
|
||||
不动,不动,不动
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>steps = 2, arrLen = 4
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>2 步后,总共有 2 种不同的方法可以停在索引 0 处。
|
||||
向右,向左
|
||||
不动,不动
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>steps = 4, arrLen = 2
|
||||
<strong>输出:</strong>8
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= steps <= 500</code></li>
|
||||
<li><code>1 <= arrLen <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>给出矩阵 <code>matrix</code> 和目标值 <code>target</code>,返回元素总和等于目标值的非空子矩阵的数量。</p>
|
||||
|
||||
<p>子矩阵 <code>x1, y1, x2, y2</code> 是满足 <code>x1 <= x <= x2</code> 且 <code>y1 <= y <= y2</code> 的所有单元 <code>matrix[x][y]</code> 的集合。</p>
|
||||
|
||||
<p>如果 <code>(x1, y1, x2, y2)</code> 和 <code>(x1', y1', x2', y2')</code> 两个子矩阵中部分坐标不同(如:<code>x1 != x1'</code>),那么这两个子矩阵也不同。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg" style="width: 242px; height: 242px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>四个只含 0 的 1x1 子矩阵。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[1,-1],[-1,1]], target = 0
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>两个 1x2 子矩阵,加上两个 2x1 子矩阵,再加上一个 2x2 子矩阵。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[904]], target = 0
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong><strong>提示:</strong></strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= matrix.length <= 100</code></li>
|
||||
<li><code>1 <= matrix[0].length <= 100</code></li>
|
||||
<li><code>-1000 <= matrix[i] <= 1000</code></li>
|
||||
<li><code>-10^8 <= target <= 10^8</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>mat</code> 和一个整数阈值 <code>threshold</code>。</p>
|
||||
|
||||
<p>请你返回元素总和小于或等于阈值的正方形区域的最大边长;如果没有这样的正方形区域,则返回 <strong>0 </strong>。<br />
|
||||
</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/12/15/e1.png" style="height: 186px; width: 335px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>总和小于或等于 4 的正方形的最大边长为 2,如图所示。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 300</code></li>
|
||||
<li><code>0 <= mat[i][j] <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= threshold <= 10<sup>5</sup></code><sup> </sup></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>环形公交路线上有 <code>n</code> 个站,按次序从 <code>0</code> 到 <code>n - 1</code> 进行编号。我们已知每一对相邻公交站之间的距离,<code>distance[i]</code> 表示编号为 <code>i</code> 的车站和编号为 <code>(i + 1) % n</code> 的车站之间的距离。</p>
|
||||
|
||||
<p>环线上的公交车都可以按顺时针和逆时针的方向行驶。</p>
|
||||
|
||||
<p>返回乘客从出发点 <code>start</code> 到目的地 <code>destination</code> 之间的最短距离。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/09/08/untitled-diagram-1.jpg" style="height: 240px; width: 388px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>distance = [1,2,3,4], start = 0, destination = 1
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>公交站 0 和 1 之间的距离是 1 或 9,最小值是 1。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/09/08/untitled-diagram-1-1.jpg" style="height: 240px; width: 388px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>distance = [1,2,3,4], start = 0, destination = 2
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>公交站 0 和 2 之间的距离是 3 或 7,最小值是 3。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/09/08/untitled-diagram-1-2.jpg" style="height: 240px; width: 388px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>distance = [1,2,3,4], start = 0, destination = 3
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>公交站 0 和 3 之间的距离是 6 或 4,最小值是 4。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10^4</code></li>
|
||||
<li><code>distance.length == n</code></li>
|
||||
<li><code>0 <= start, destination < n</code></li>
|
||||
<li><code>0 <= distance[i] <= 10^4</code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个由小写字母组成的字符串 <code>s</code>,和一个整数 <code>k</code>。</p>
|
||||
|
||||
<p>请你按下面的要求分割字符串:</p>
|
||||
|
||||
<ul>
|
||||
<li>首先,你可以将 <code>s</code> 中的部分字符修改为其他的小写英文字母。</li>
|
||||
<li>接着,你需要把 <code>s</code> 分割成 <code>k</code> 个非空且不相交的子串,并且每个子串都是回文串。</li>
|
||||
</ul>
|
||||
|
||||
<p>请返回以这种方式分割字符串所需修改的最少字符数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abc", k = 2
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>你可以把字符串分割成 "ab" 和 "c",并修改 "ab" 中的 1 个字符,将它变成回文串。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "aabbc", k = 3
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>你可以把字符串分割成 "aa"、"bb" 和 "c",它们都是回文串。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "leetcode", k = 8
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 100</code></li>
|
||||
<li><code>s</code> 中只含有小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个由若干 0 和 1 组成的字符串 <code>s</code> ,请你计算并返回将该字符串分割成两个 <strong>非空</strong> 子字符串(即 <strong>左</strong> 子字符串和 <strong>右</strong> 子字符串)所能获得的最大得分。</p>
|
||||
|
||||
<p>「分割字符串的得分」为 <strong>左</strong> 子字符串中 <strong>0</strong> 的数量加上 <strong>右</strong> 子字符串中 <strong>1</strong> 的数量。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "011101"
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>
|
||||
将字符串 s 划分为两个非空子字符串的可行方案有:
|
||||
左子字符串 = "0" 且 右子字符串 = "11101",得分 = 1 + 4 = 5
|
||||
左子字符串 = "01" 且 右子字符串 = "1101",得分 = 1 + 3 = 4
|
||||
左子字符串 = "011" 且 右子字符串 = "101",得分 = 1 + 2 = 3
|
||||
左子字符串 = "0111" 且 右子字符串 = "01",得分 = 1 + 1 = 2
|
||||
左子字符串 = "01110" 且 右子字符串 = "1",得分 = 2 + 1 = 3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "00111"
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>当 左子字符串 = "00" 且 右子字符串 = "111" 时,我们得到最大得分 = 2 + 3 = 5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "1111"
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 500</code></li>
|
||||
<li>字符串 <code>s</code> 仅由字符 <code>'0'</code> 和 <code>'1'</code> 组成。</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>在一个 <strong>平衡字符串</strong> 中,<code>'L'</code> 和 <code>'R'</code> 字符的数量是相同的。</p>
|
||||
|
||||
<p>给你一个平衡字符串 <code>s</code>,请你将它分割成尽可能多的平衡字符串。</p>
|
||||
|
||||
<p><strong>注意:</strong>分割得到的每个字符串都必须是平衡字符串,且分割得到的平衡字符串是原平衡字符串的连续子串。</p>
|
||||
|
||||
<p>返回可以通过分割得到的平衡字符串的 <strong>最大数量</strong> <strong>。</strong></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "RLRRLLRLRL"
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>s 可以分割为 "RL"、"RRLL"、"RL"、"RL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "RLLLLRRRLR"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>s 可以分割为 "RL"、"LLLRRR"、"LR" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "LLLLRRRR"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>s 只能保持原样 "LLLLRRRR".
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "RLRRRLLRLL"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>s 可以分割为 "RL"、"RRRLLRLL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i] = 'L' 或 'R'</code></li>
|
||||
<li><code>s</code> 是一个 <strong>平衡</strong> 字符串</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>排排坐,分糖果。</p>
|
||||
|
||||
<p>我们买了一些糖果 <code>candies</code>,打算把它们分给排好队的 <strong><code>n = num_people</code></strong> 个小朋友。</p>
|
||||
|
||||
<p>给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类推,直到给最后一个小朋友 <code>n</code> 颗糖果。</p>
|
||||
|
||||
<p>然后,我们再回到队伍的起点,给第一个小朋友 <code>n + 1</code> 颗糖果,第二个小朋友 <code>n + 2</code> 颗,依此类推,直到给最后一个小朋友 <code>2 * n</code> 颗糖果。</p>
|
||||
|
||||
<p>重复上述过程(每次都比上一次多给出一颗糖果,当到达队伍终点后再次从队伍起点开始),直到我们分完所有的糖果。注意,就算我们手中的剩下糖果数不够(不比前一次发出的糖果多),这些糖果也会全部发给当前的小朋友。</p>
|
||||
|
||||
<p>返回一个长度为 <code>num_people</code>、元素之和为 <code>candies</code> 的数组,以表示糖果的最终分发情况(即 <code>ans[i]</code> 表示第 <code>i</code> 个小朋友分到的糖果数)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>candies = 7, num_people = 4
|
||||
<strong>输出:</strong>[1,2,3,1]
|
||||
<strong>解释:</strong>
|
||||
第一次,ans[0] += 1,数组变为 [1,0,0,0]。
|
||||
第二次,ans[1] += 2,数组变为 [1,2,0,0]。
|
||||
第三次,ans[2] += 3,数组变为 [1,2,3,0]。
|
||||
第四次,ans[3] += 1(因为此时只剩下 1 颗糖果),最终数组变为 [1,2,3,1]。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>candies = 10, num_people = 3
|
||||
<strong>输出:</strong>[5,2,3]
|
||||
<strong>解释:</strong>
|
||||
第一次,ans[0] += 1,数组变为 [1,0,0]。
|
||||
第二次,ans[1] += 2,数组变为 [1,2,0]。
|
||||
第三次,ans[2] += 3,数组变为 [1,2,3]。
|
||||
第四次,ans[0] += 4,最终数组变为 [5,2,3]。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candies <= 10^9</code></li>
|
||||
<li><code>1 <= num_people <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你一棵二叉树,它的根为 <code>root</code> 。请你删除 1 条边,使二叉树分裂成两棵子树,且它们子树和的乘积尽可能大。</p>
|
||||
|
||||
<p>由于答案可能会很大,请你将结果对 10^9 + 7 取模后再返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/02/sample_1_1699.png" style="height: 200px; width: 495px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,2,3,4,5,6]
|
||||
<strong>输出:</strong>110
|
||||
<strong>解释:</strong>删除红色的边,得到 2 棵子树,和分别为 11 和 10 。它们的乘积是 110 (11*10)
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/02/sample_2_1699.png" style="height: 200px; width: 495px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,null,2,3,4,null,null,5,6]
|
||||
<strong>输出:</strong>90
|
||||
<strong>解释:</strong>移除红色的边,得到 2 棵子树,和分别是 15 和 6 。它们的乘积为 90 (15*6)
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [2,3,9,10,7,8,6,5,4,11,1]
|
||||
<strong>输出:</strong>1025
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,1]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>每棵树最多有 <code>50000</code> 个节点,且至少有 <code>2</code> 个节点。</li>
|
||||
<li>每个节点的值在 <code>[1, 10000]</code> 之间。</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个整数数组 <code>arr</code>,请你将该数组分隔为长度最多为 k 的一些(连续)子数组。分隔完成后,每个子数组的中的所有值都会变为该子数组中的最大值。</p>
|
||||
|
||||
<p>返回将数组分隔变换后能够得到的元素最大和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意,</strong>原数组和分隔后的数组对应顺序应当一致,也就是说,你只能选择分隔数组的位置而不能调整数组中的顺序。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,15,7,9,2,5,10], k = 3
|
||||
<strong>输出:</strong>84
|
||||
<strong>解释:</strong>
|
||||
因为 k=3 可以分隔成 [1,15,7] [9] [2,5,10],结果为 [15,15,15,9,10,10,10],和为 84,是该数组所有分隔变换后元素总和最大的。
|
||||
若是分隔成 [1] [15,7,9] [2,5,10],结果就是 [1, 15, 15, 15, 10, 10, 10] 但这种分隔方式的元素总和(76)小于上一种。 </pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
|
||||
<strong>输出:</strong>83
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1], k = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 500</code></li>
|
||||
<li><code>0 <= arr[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= arr.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>矩形蛋糕的高度为 <code>h</code> 且宽度为 <code>w</code>,给你两个整数数组 <code>horizontalCuts</code> 和 <code>verticalCuts</code>,其中:</p>
|
||||
|
||||
<ul>
|
||||
<li> <code>horizontalCuts[i]</code> 是从矩形蛋糕顶部到第 <code>i</code> 个水平切口的距离</li>
|
||||
<li><code>verticalCuts[j]</code> 是从矩形蛋糕的左侧到第 <code>j</code> 个竖直切口的距离</li>
|
||||
</ul>
|
||||
|
||||
<p>请你按数组 <em><code>horizontalCuts</code> </em>和<em> <code>verticalCuts</code> </em>中提供的水平和竖直位置切割后,请你找出 <strong>面积最大</strong> 的那份蛋糕,并返回其 <strong>面积</strong> 。由于答案可能是一个很大的数字,因此需要将结果 <strong>对</strong> <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/05/30/leetcode_max_area_2.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>上图所示的矩阵蛋糕中,红色线表示水平和竖直方向上的切口。切割蛋糕后,绿色的那份蛋糕面积最大。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/05/30/leetcode_max_area_3.png" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>上图所示的矩阵蛋糕中,红色线表示水平和竖直方向上的切口。切割蛋糕后,绿色和黄色的两份蛋糕面积最大。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
|
||||
<strong>输出:</strong>9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= h, w <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= horizontalCuts.length <= min(h - 1, 10<sup>5</sup>)</code></li>
|
||||
<li><code>1 <= verticalCuts.length <= min(w - 1, 10<sup>5</sup>)</code></li>
|
||||
<li><code>1 <= horizontalCuts[i] < h</code></li>
|
||||
<li><code>1 <= verticalCuts[i] < w</code></li>
|
||||
<li>题目数据保证 <code>horizontalCuts</code> 中的所有元素各不相同</li>
|
||||
<li>题目数据保证 <code>verticalCuts</code> 中的所有元素各不相同</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个 <code>rows x cols</code> 大小的矩形披萨和一个整数 <code>k</code> ,矩形包含两种字符: <code>'A'</code> (表示苹果)和 <code>'.'</code> (表示空白格子)。你需要切披萨 <code>k-1</code> 次,得到 <code>k</code> 块披萨并送给别人。</p>
|
||||
|
||||
<p>切披萨的每一刀,先要选择是向垂直还是水平方向切,再在矩形的边界上选一个切的位置,将披萨一分为二。如果垂直地切披萨,那么需要把左边的部分送给一个人,如果水平地切,那么需要把上面的部分送给一个人。在切完最后一刀后,需要把剩下来的一块送给最后一个人。</p>
|
||||
|
||||
<p>请你返回确保每一块披萨包含 <strong>至少</strong> 一个苹果的切披萨方案数。由于答案可能是个很大的数字,请你返回它对 10^9 + 7 取余的结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/05/10/ways_to_cut_apple_1.png" style="height: 378px; width: 500px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>pizza = ["A..","AAA","..."], k = 3
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>上图展示了三种切披萨的方案。注意每一块披萨都至少包含一个苹果。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>pizza = ["A..","AA.","..."], k = 3
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>pizza = ["A..","A..","..."], k = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= rows, cols <= 50</code></li>
|
||||
<li><code>rows == pizza.length</code></li>
|
||||
<li><code>cols == pizza[i].length</code></li>
|
||||
<li><code>1 <= k <= 10</code></li>
|
||||
<li><code>pizza</code> 只包含字符 <code>'A'</code> 和 <code>'.'</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个正整数 <code>k</code>,请你判断是否可以把这个数组划分成一些由 <code>k</code> 个连续数字组成的集合。<br />
|
||||
如果可以,请返回 <code>true</code>;否则,返回 <code>false</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,3,4,4,5,6], k = 4
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>数组可以分成 [1,2,3,4] 和 [3,4,5,6]。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>数组可以分成 [1,2,3] , [2,3,4] , [3,4,5] 和 [9,10,11]。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,3,2,2,1,1], k = 3
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4], k = 3
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>数组不能分成几个大小为 3 的子数组。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>此题目与 846 重复:<a href="https://leetcode-cn.com/problems/hand-of-straights/" target="_blank">https://leetcode-cn.com/problems/hand-of-straights/</a></p>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个二进制数组 <code>nums</code> ,你需要从中删掉一个元素。</p>
|
||||
|
||||
<p>请你在删掉元素的结果数组中,返回最长的且只包含 1 的非空子数组的长度。</p>
|
||||
|
||||
<p>如果不存在这样的子数组,请返回 0 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,0,1]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>删掉位置 2 的数后,[1,1,1] 包含 3 个 1 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,1,1,1,0,1,1,0,1]
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>删掉位置 4 的数字后,[0,1,1,1,1,1,0,1] 的最长全 1 子数组为 [1,1,1,1,1] 。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>你必须要删除一个元素。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums[i]</code> 要么是 <code>0</code> 要么是 <code>1</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给出二叉树的根节点 <code>root</code>,树上每个节点都有一个不同的值。</p>
|
||||
|
||||
<p>如果节点值在 <code>to_delete</code> 中出现,我们就把该节点从树上删去,最后得到一个森林(一些不相交的树构成的集合)。</p>
|
||||
|
||||
<p>返回森林中的每棵树。你可以按任意顺序组织答案。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/07/05/screen-shot-2019-07-01-at-53836-pm.png" style="height: 150px; width: 237px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,4,5,6,7], to_delete = [3,5]
|
||||
<strong>输出:</strong>[[1,2,null,4],[6],[7]]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,4,null,3], to_delete = [3]
|
||||
<strong>输出:</strong>[[1,2,4]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中的节点数最大为 <code>1000</code>。</li>
|
||||
<li>每个节点都有一个介于 <code>1</code> 到 <code>1000</code> 之间的值,且各不相同。</li>
|
||||
<li><code>to_delete.length <= 1000</code></li>
|
||||
<li><code>to_delete</code> 包含一些从 <code>1</code> 到 <code>1000</code>、各不相同的值。</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>给你一个整数数组,返回它的某个 <strong>非空</strong> 子数组(连续元素)在执行一次可选的删除操作后,所能得到的最大元素总和。换句话说,你可以从原数组中选出一个子数组,并可以决定要不要从中删除一个元素(只能删一次哦),(删除后)子数组中至少应当有一个元素,然后该子数组(剩下)的元素总和是所有子数组之中最大的。</p>
|
||||
|
||||
<p>注意,删除一个元素后,子数组 <strong>不能为空</strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,-2,0,3]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>我们可以选出 [1, -2, 0, 3],然后删掉 -2,这样得到 [1, 0, 3],和最大。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [1,-2,-2,3]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>我们直接选出 [3],这就是最大和。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = [-1,-1,-1,-1]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>最后得到的子数组不能为空,所以我们不能选择 [-1] 并从中删去 -1 来得到 0。
|
||||
我们应该直接选择 [-1],或者选择 [-1, -1] 再从中删去一个 -1。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
<meta charset="UTF-8" />
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= arr[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你一个字符串 <code>s</code>,它仅由字母 <code>'a'</code> 和 <code>'b'</code> 组成。每一次删除操作都可以从 <code>s</code> 中删除一个回文 <strong>子序列</strong>。</p>
|
||||
|
||||
<p>返回删除给定字符串中所有字符(字符串为空)的最小删除次数。</p>
|
||||
|
||||
<p>「子序列」定义:如果一个字符串可以通过删除原字符串某些字符而不改变原字符顺序得到,那么这个字符串就是原字符串的一个子序列。</p>
|
||||
|
||||
<p>「回文」定义:如果一个字符串向后和向前读是一致的,那么这个字符串就是一个回文。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "ababa"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>字符串本身就是回文序列,只需要删除一次。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abb"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>"<strong>a</strong>bb" -> "<strong>bb</strong>" -> "".
|
||||
先删除回文子序列 "a",然后再删除 "bb"。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "baabb"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>"<strong>baa</strong>b<strong>b</strong>" -> "b" -> "".
|
||||
先删除回文子序列 "baab",然后再删除 "b"。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> 仅包含字母 <code>'a'</code> 和 <code>'b'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>你是一位系统管理员,手里有一份文件夹列表 <code>folder</code>,你的任务是要删除该列表中的所有 <strong>子文件夹</strong>,并以 <strong>任意顺序</strong> 返回剩下的文件夹。</p>
|
||||
|
||||
<p>如果文件夹 <code>folder[i]</code> 位于另一个文件夹 <code>folder[j]</code> 下,那么 <code>folder[i]</code> 就是 <code>folder[j]</code> 的 <strong>子文件夹</strong> 。</p>
|
||||
|
||||
<p>文件夹的「路径」是由一个或多个按以下格式串联形成的字符串:<font color="#c7254e"><font face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size:12.6px"><span style="background-color:#f9f2f4">'/'</span></span></font></font> 后跟一个或者多个小写英文字母。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>"/leetcode"</code> 和 <code>"/leetcode/problems"</code> 都是有效的路径,而空字符串和 <code>"/"</code> 不是。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
|
||||
<strong>输出:</strong>["/a","/c/d","/c/f"]
|
||||
<strong>解释:</strong>"/a/b/" 是 "/a" 的子文件夹,而 "/c/d/e" 是 "/c/d" 的子文件夹。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>folder = ["/a","/a/b/c","/a/b/d"]
|
||||
<strong>输出:</strong>["/a"]
|
||||
<strong>解释:</strong>文件夹 "/a/b/c" 和 "/a/b/d/" 都会被删除,因为它们都是 "/a" 的子文件夹。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> folder = ["/a/b/c","/a/b/ca","/a/b/d"]
|
||||
<strong>输出:</strong> ["/a/b/c","/a/b/ca","/a/b/d"]</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= folder.length <= 4 * 10<sup>4</sup></code></li>
|
||||
<li><code>2 <= folder[i].length <= 100</code></li>
|
||||
<li><code>folder[i]</code> 只包含小写字母和 <code>'/'</code></li>
|
||||
<li><code>folder[i]</code> 总是以字符 <code>'/'</code> 起始</li>
|
||||
<li>每个文件夹名都是 <strong>唯一</strong> 的</li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个字符串 <code>s</code>,「<code>k</code> 倍重复项删除操作」将会从 <code>s</code> 中选择 <code>k</code> 个相邻且相等的字母,并删除它们,使被删去的字符串的左侧和右侧连在一起。</p>
|
||||
|
||||
<p>你需要对 <code>s</code> 重复进行无限次这样的删除操作,直到无法继续为止。</p>
|
||||
|
||||
<p>在执行完所有删除操作后,返回最终得到的字符串。</p>
|
||||
|
||||
<p>本题答案保证唯一。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abcd", k = 2
|
||||
<strong>输出:</strong>"abcd"
|
||||
<strong>解释:</strong>没有要删除的内容。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "deeedbbcccbdaa", k = 3
|
||||
<strong>输出:</strong>"aa"
|
||||
<strong>解释:
|
||||
</strong>先删除 "eee" 和 "ccc",得到 "ddbbbdaa"
|
||||
再删除 "bbb",得到 "dddaa"
|
||||
最后删除 "ddd",得到 "aa"</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "pbbcggttciiippooaais", k = 2
|
||||
<strong>输出:</strong>"ps"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10^5</code></li>
|
||||
<li><code>2 <= k <= 10^4</code></li>
|
||||
<li><code>s</code> 中只含有小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,24 @@
|
||||
<p>给出由小写字母组成的字符串 <code>S</code>,<strong>重复项删除操作</strong>会选择两个相邻且相同的字母,并删除它们。</p>
|
||||
|
||||
<p>在 S 上反复执行重复项删除操作,直到无法继续删除。</p>
|
||||
|
||||
<p>在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>"abbaca"
|
||||
<strong>输出:</strong>"ca"
|
||||
<strong>解释:</strong>
|
||||
例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>1 <= S.length <= 20000</code></li>
|
||||
<li><code>S</code> 仅由小写英文字母组成。</li>
|
||||
</ol>
|
@@ -0,0 +1,45 @@
|
||||
<p>一个字符串如果没有 <strong>三个连续</strong> 相同字符,那么它就是一个 <strong>好字符串</strong> 。</p>
|
||||
|
||||
<p>给你一个字符串 <code>s</code> ,请你从 <code>s</code> 删除 <strong>最少</strong> 的字符,使它变成一个 <strong>好字符串</strong> 。</p>
|
||||
|
||||
<p>请你返回删除后的字符串。题目数据保证答案总是 <strong>唯一的 </strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "le<strong>e</strong>etcode"
|
||||
<b>输出:</b>"leetcode"
|
||||
<strong>解释:</strong>
|
||||
从第一组 'e' 里面删除一个 'e' ,得到 "leetcode" 。
|
||||
没有连续三个相同字符,所以返回 "leetcode" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "<strong>a</strong>aab<strong>aa</strong>aa"
|
||||
<b>输出:</b>"aabaa"
|
||||
<strong>解释:</strong>
|
||||
从第一组 'a' 里面删除一个 'a' ,得到 "aabaaaa" 。
|
||||
从第二组 'a' 里面删除两个 'a' ,得到 "aabaa" 。
|
||||
没有连续三个相同字符,所以返回 "aabaa" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "aab"
|
||||
<b>输出:</b>"aab"
|
||||
<b>解释:</b>没有连续三个相同字符,所以返回 "aab" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一个整数数组 <code>arr</code> ,请你删除最小 <code>5%</code> 的数字和最大 <code>5%</code> 的数字后,剩余数字的平均值。</p>
|
||||
|
||||
<p>与 <strong>标准答案</strong> 误差在 <code>10<sup>-5</sup></code> 的结果都被视为正确结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3]
|
||||
<b>输出:</b>2.00000
|
||||
<b>解释:</b>删除数组中最大和最小的元素后,所有元素都等于 2,所以平均值为 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = [6,2,7,5,1,2,0,3,10,2,5,0,5,5,0,8,7,6,8,0]
|
||||
<b>输出:</b>4.00000
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = [6,0,7,0,7,5,7,8,3,4,0,7,8,1,6,8,1,1,2,4,8,1,9,5,4,3,8,5,10,8,6,6,1,0,6,10,8,2,3,4]
|
||||
<b>输出:</b>4.77778
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = [9,7,8,7,7,8,4,4,6,8,8,7,6,8,8,9,2,6,0,0,1,10,8,6,3,3,5,1,10,9,0,7,10,0,10,4,1,10,6,9,3,6,0,0,2,7,0,6,7,2,9,7,7,3,0,1,6,1,10,3]
|
||||
<b>输出:</b>5.27778
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>arr = [4,8,4,10,0,7,1,3,7,8,8,3,4,1,6,2,1,1,8,0,9,8,0,3,9,10,3,10,1,10,7,3,2,1,4,9,10,7,6,4,0,8,5,1,2,1,6,2,5,0,7,10,9,10,3,7,10,5,8,5,7,6,7,6,10,9,5,10,5,5,7,2,10,7,7,8,2,0,1,1]
|
||||
<b>输出:</b>5.29167
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>20 <= arr.length <= 1000</code></li>
|
||||
<li><code>arr.length</code><b> </b>是 <code>20</code> 的<strong> 倍数</strong> </li>
|
||||
<li><code>0 <= arr[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>给你一棵以 <code>root</code> 为根的二叉树和一个整数 <code>target</code> ,请你删除所有值为 <code>target</code> 的 <strong>叶子节点</strong> 。</p>
|
||||
|
||||
<p>注意,一旦删除值为 <code>target</code> 的叶子节点,它的父节点就可能变成叶子节点;如果新叶子节点的值恰好也是 <code>target</code> ,那么这个节点也应该被删除。</p>
|
||||
|
||||
<p>也就是说,你需要重复此过程直到不能继续删除。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/16/sample_1_1684.png" style="height: 120px; width: 550px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,2,3,2,null,2,4], target = 2
|
||||
<strong>输出:</strong>[1,null,3,null,4]
|
||||
<strong>解释:
|
||||
</strong>上面左边的图中,绿色节点为叶子节点,且它们的值与 target 相同(同为 2 ),它们会被删除,得到中间的图。
|
||||
有一个新的节点变成了叶子节点且它的值与 target 相同,所以将再次进行删除,从而得到最右边的图。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/16/sample_2_1684.png" style="height: 120px; width: 300px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,3,3,3,2], target = 3
|
||||
<strong>输出:</strong>[1,3,null,null,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/01/16/sample_3_1684.png" style="width: 450px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,2,null,2,null,2], target = 2
|
||||
<strong>输出:</strong>[1]
|
||||
<strong>解释:</strong>每一步都删除一个绿色的叶子节点(值为 2)。</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,1,1], target = 1
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,2,3], target = 1
|
||||
<strong>输出:</strong>[1,2,3]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= target <= 1000</code></li>
|
||||
<li>每一棵树最多有 <code>3000</code> 个节点。</li>
|
||||
<li>每一个节点值的范围是 <code>[1, 1000]</code> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,25 @@
|
||||
<p>给你一个区间列表,请你删除列表中被其他区间所覆盖的区间。</p>
|
||||
|
||||
<p>只有当 <code>c <= a</code> 且 <code>b <= d</code> 时,我们才认为区间 <code>[a,b)</code> 被区间 <code>[c,d)</code> 覆盖。</p>
|
||||
|
||||
<p>在完成所有删除操作后,请你返回列表中剩余区间的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>intervals = [[1,4],[3,6],[2,8]]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>区间 [3,6] 被区间 [2,8] 覆盖,所以它被删除了。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= intervals.length <= 1000</code></li>
|
||||
<li><code>0 <= intervals[i][0] < intervals[i][1] <= 10^5</code></li>
|
||||
<li>对于所有的 <code>i != j</code>:<code>intervals[i] != intervals[j]</code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你一个数字数组 <code>arr</code> 。</p>
|
||||
|
||||
<p>如果一个数列中,任意相邻两项的差总等于同一个常数,那么这个数列就称为 <strong>等差数列</strong> 。</p>
|
||||
|
||||
<p>如果可以重新排列数组形成等差数列,请返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr = [3,5,1]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>对数组重新排序得到 [1,3,5] 或者 [5,3,1] ,任意相邻两项的差分别为 2 或 -2 ,可以形成等差数列。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr = [1,2,4]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>无法通过重新排序得到等差数列。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= arr.length <= 1000</code></li>
|
||||
<li><code>-10^6 <= arr[i] <= 10^6</code></li>
|
||||
</ul>
|
34
算法题(国内版)/problem (Chinese)/判断路径是否相交 [path-crossing].html
Normal file
34
算法题(国内版)/problem (Chinese)/判断路径是否相交 [path-crossing].html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>给你一个字符串 <code>path</code>,其中 <code>path[i]</code> 的值可以是 <code>'N'</code>、<code>'S'</code>、<code>'E'</code> 或者 <code>'W'</code>,分别表示向北、向南、向东、向西移动一个单位。</p>
|
||||
|
||||
<p>你从二维平面上的原点 <code>(0, 0)</code> 处开始出发,按 <code>path</code> 所指示的路径行走。</p>
|
||||
|
||||
<p>如果路径在任何位置上与自身相交,也就是走到之前已经走过的位置,请返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/06/28/screen-shot-2020-06-10-at-123929-pm.png" style="height: 358px; width: 400px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>path = "NES"
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>该路径没有在任何位置相交。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/06/28/screen-shot-2020-06-10-at-123843-pm.png" style="height: 339px; width: 400px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>path = "NESWW"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>该路径经过原点两次。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= path.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>path[i]</code> 为 <code>'N'</code>、<code>'S'</code>、<code>'E'</code> 或 <code>'W'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,64 @@
|
||||
<p>给你一个整数数组 <code>bloomDay</code>,以及两个整数 <code>m</code> 和 <code>k</code> 。</p>
|
||||
|
||||
<p>现需要制作 <code>m</code> 束花。制作花束时,需要使用花园中 <strong>相邻的 <code>k</code> 朵花</strong> 。</p>
|
||||
|
||||
<p>花园中有 <code>n</code> 朵花,第 <code>i</code> 朵花会在 <code>bloomDay[i]</code> 时盛开,<strong>恰好</strong> 可以用于 <strong>一束</strong> 花中。</p>
|
||||
|
||||
<p>请你返回从花园中摘 <code>m</code> 束花需要等待的最少的天数。如果不能摘到 <code>m</code> 束花则返回 <strong>-1</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>bloomDay = [1,10,3,10,2], m = 3, k = 1
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>让我们一起观察这三天的花开过程,x 表示花开,而 _ 表示花还未开。
|
||||
现在需要制作 3 束花,每束只需要 1 朵。
|
||||
1 天后:[x, _, _, _, _] // 只能制作 1 束花
|
||||
2 天后:[x, _, _, _, x] // 只能制作 2 束花
|
||||
3 天后:[x, _, x, _, x] // 可以制作 3 束花,答案为 3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>bloomDay = [1,10,3,10,2], m = 3, k = 2
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>要制作 3 束花,每束需要 2 朵花,也就是一共需要 6 朵花。而花园中只有 5 朵花,无法满足制作要求,返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
|
||||
<strong>输出:</strong>12
|
||||
<strong>解释:</strong>要制作 2 束花,每束需要 3 朵。
|
||||
花园在 7 天后和 12 天后的情况如下:
|
||||
7 天后:[x, x, x, x, _, x, x]
|
||||
可以用前 3 朵盛开的花制作第一束花。但不能使用后 3 朵盛开的花,因为它们不相邻。
|
||||
12 天后:[x, x, x, x, x, x, x]
|
||||
显然,我们可以用不同的方式制作两束花。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>bloomDay = [1000000000,1000000000], m = 1, k = 1
|
||||
<strong>输出:</strong>1000000000
|
||||
<strong>解释:</strong>需要等 1000000000 天才能采到花来制作花束
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
|
||||
<strong>输出:</strong>9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>bloomDay.length == n</code></li>
|
||||
<li><code>1 <= n <= 10^5</code></li>
|
||||
<li><code>1 <= bloomDay[i] <= 10^9</code></li>
|
||||
<li><code>1 <= m <= 10^6</code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你两个长度相等的字符串 <code>s</code> 和 <code>t</code>。每一个步骤中,你可以选择将 <code>t</code> 中的 <strong>任一字符</strong> 替换为 <strong>另一个字符</strong>。</p>
|
||||
|
||||
<p>返回使 <code>t</code> 成为 <code>s</code> 的字母异位词的最小步骤数。</p>
|
||||
|
||||
<p><strong>字母异位词</strong> 指字母相同,但排列不同(也可能相同)的字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输出:</strong>s = "bab", t = "aba"
|
||||
<strong>输出:</strong>1
|
||||
<strong>提示:</strong>用 'b' 替换 t 中的第一个 'a',t = "bba" 是 s 的一个字母异位词。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输出:</strong>s = "leetcode", t = "practice"
|
||||
<strong>输出:</strong>5
|
||||
<strong>提示:</strong>用合适的字符替换 t 中的 'p', 'r', 'a', 'i' 和 'c',使 t 变成 s 的字母异位词。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输出:</strong>s = "anagram", t = "mangaar"
|
||||
<strong>输出:</strong>0
|
||||
<strong>提示:</strong>"anagram" 和 "mangaar" 本身就是一组字母异位词。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输出:</strong>s = "xxyyzz", t = "xxyyzz"
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输出:</strong>s = "friend", t = "family"
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 50000</code></li>
|
||||
<li><code>s.length == t.length</code></li>
|
||||
<li><code>s</code> 和 <code>t</code> 只包含小写英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个字符串 <code>s</code> ,它只包含三种字符 a, b 和 c 。</p>
|
||||
|
||||
<p>请你返回 a,b 和 c 都 <strong>至少 </strong>出现过一次的子字符串数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abcabc"
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>包含 a,b 和 c 各至少一次的子字符串为<em> "</em>abc<em>", "</em>abca<em>", "</em>abcab<em>", "</em>abcabc<em>", "</em>bca<em>", "</em>bcab<em>", "</em>bcabc<em>", "</em>cab<em>", "</em>cabc<em>" </em>和<em> "</em>abc<em>" </em>(<strong>相同</strong><strong>字符串算多次</strong>)<em>。</em>
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "aaacb"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>包含 a,b 和 c 各至少一次的子字符串为<em> "</em>aaacb<em>", "</em>aacb<em>" </em>和<em> "</em>acb<em>" 。</em>
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abc"
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 5 x 10^4</code></li>
|
||||
<li><code>s</code> 只包含字符 a,b 和 c 。</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>请你设计一个数据结构,它能求出给定子数组内一个给定值的 <strong>频率</strong> 。</p>
|
||||
|
||||
<p>子数组中一个值的 <strong>频率</strong> 指的是这个子数组中这个值的出现次数。</p>
|
||||
|
||||
<p>请你实现 <code>RangeFreqQuery</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>RangeFreqQuery(int[] arr)</code> 用下标从 <strong>0</strong> 开始的整数数组 <code>arr</code> 构造一个类的实例。</li>
|
||||
<li><code>int query(int left, int right, int value)</code> 返回子数组 <code>arr[left...right]</code> 中 <code>value</code> 的 <strong>频率</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>一个 <strong>子数组</strong> 指的是数组中一段连续的元素。<code>arr[left...right]</code> 指的是 <code>nums</code> 中包含下标 <code>left</code> 和 <code>right</code> <strong>在内</strong> 的中间一段连续元素。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>
|
||||
["RangeFreqQuery", "query", "query"]
|
||||
[[[12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]], [1, 2, 4], [0, 11, 33]]
|
||||
<strong>输出:</strong>
|
||||
[null, 1, 2]
|
||||
|
||||
<strong>解释:</strong>
|
||||
RangeFreqQuery rangeFreqQuery = new RangeFreqQuery([12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]);
|
||||
rangeFreqQuery.query(1, 2, 4); // 返回 1 。4 在子数组 [33, 4] 中出现 1 次。
|
||||
rangeFreqQuery.query(0, 11, 33); // 返回 2 。33 在整个子数组中出现 2 次。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= arr[i], value <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= left <= right < arr.length</code></li>
|
||||
<li>调用 <code>query</code> 不超过 <code>10<sup>5</sup></code> 次。</li>
|
||||
</ul>
|
35
算法题(国内版)/problem (Chinese)/千位分隔数 [thousand-separator].html
Normal file
35
算法题(国内版)/problem (Chinese)/千位分隔数 [thousand-separator].html
Normal file
@@ -0,0 +1,35 @@
|
||||
<p>给你一个整数 <code>n</code>,请你每隔三位添加点(即 "." 符号)作为千位分隔符,并将结果以字符串格式返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 987
|
||||
<strong>输出:</strong>"987"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 1234
|
||||
<strong>输出:</strong>"1.234"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 123456789
|
||||
<strong>输出:</strong>"123.456.789"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 0
|
||||
<strong>输出:</strong>"0"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n < 2^31</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>如果字符串中的所有字符都相同,那么这个字符串是单字符重复的字符串。</p>
|
||||
|
||||
<p>给你一个字符串 <code>text</code>,你只能交换其中两个字符一次或者什么都不做,然后得到一些单字符重复的子串。返回其中最长的子串的长度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "ababa"
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "aaabaaa"
|
||||
<strong>输出:</strong>6
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "aaabbaaa"
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "aaaaa"
|
||||
<strong>输出:</strong>5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "abcdef"
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= text.length <= 20000</code></li>
|
||||
<li><code>text</code> 仅由小写英文字母组成。</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p><a href="https://baike.baidu.com/item/%E8%A1%8C%E7%A8%8B%E9%95%BF%E5%BA%A6%E7%BC%96%E7%A0%81/2931940?fr=aladdin" target="_blank">行程长度编码</a> 是一种常用的字符串压缩方法,它将连续的相同字符(重复 2 次或更多次)替换为字符和表示字符计数的数字(行程长度)。例如,用此方法压缩字符串 <code>"aabccc"</code> ,将 <code>"aa"</code> 替换为 <code>"a2"</code> ,<code>"ccc"</code> 替换为` <code>"c3"</code> 。因此压缩后的字符串变为 <code>"a2bc3"</code> 。</p>
|
||||
|
||||
<p>注意,本问题中,压缩时没有在单个字符后附加计数 <code>'1'</code> 。</p>
|
||||
|
||||
<p>给你一个字符串 <code>s</code> 和一个整数 <code>k</code> 。你需要从字符串 <code>s</code> 中删除最多 <code>k</code> 个字符,以使 <code>s</code> 的行程长度编码长度最小。</p>
|
||||
|
||||
<p>请你返回删除最多 <code>k</code> 个字符后,<code>s</code> <strong>行程长度编码的最小长度</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "aaabcccd", k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>在不删除任何内容的情况下,压缩后的字符串是 "a3bc3d" ,长度为 6 。最优的方案是删除 'b' 和 'd',这样一来,压缩后的字符串为 "a3c3" ,长度是 4 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "aabbaa", k = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>如果删去两个 'b' 字符,那么压缩后的字符串是长度为 2 的 "a4" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "aaaaaaaaaaa", k = 0
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>由于 k 等于 0 ,不能删去任何字符。压缩后的字符串是 "a11" ,长度为 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>0 <= k <= s.length</code></li>
|
||||
<li><code>s</code> 仅包含小写英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你一个整数数组 <code>salary</code> ,数组里每个数都是 <strong>唯一</strong> 的,其中 <code>salary[i]</code> 是第 <code>i</code> 个员工的工资。</p>
|
||||
|
||||
<p>请你返回去掉最低工资和最高工资以后,剩下员工工资的平均值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>salary = [4000,3000,1000,2000]
|
||||
<strong>输出:</strong>2500.00000
|
||||
<strong>解释:</strong>最低工资和最高工资分别是 1000 和 4000 。
|
||||
去掉最低工资和最高工资以后的平均工资是 (2000+3000)/2= 2500
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>salary = [1000,2000,3000]
|
||||
<strong>输出:</strong>2000.00000
|
||||
<strong>解释:</strong>最低工资和最高工资分别是 1000 和 3000 。
|
||||
去掉最低工资和最高工资以后的平均工资是 (2000)/1= 2000
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>salary = [6000,5000,4000,3000,2000,1000]
|
||||
<strong>输出:</strong>3500.00000
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>salary = [8000,9000,2000,3000,6000,1000]
|
||||
<strong>输出:</strong>4750.00000
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= salary.length <= 100</code></li>
|
||||
<li><code>10^3 <= salary[i] <= 10^6</code></li>
|
||||
<li><code>salary[i]</code> 是唯一的。</li>
|
||||
<li>与真实值误差在 <code>10^-5</code> 以内的结果都将视为正确答案。</li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>给你一个 <code>m * n</code> 的矩阵 <code>seats</code> 表示教室中的座位分布。如果座位是坏的(不可用),就用 <code>'#'</code> 表示;否则,用 <code>'.'</code> 表示。</p>
|
||||
|
||||
<p>学生可以看到左侧、右侧、左上、右上这四个方向上紧邻他的学生的答卷,但是看不到直接坐在他前面或者后面的学生的答卷。请你计算并返回该考场可以容纳的一起参加考试且无法作弊的最大学生人数。</p>
|
||||
|
||||
<p>学生必须坐在状况良好的座位上。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/09/image.png" style="height: 197px; width: 339px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>seats = [["#",".","#","#",".","#"],
|
||||
[".","#","#","#","#","."],
|
||||
["#",".","#","#",".","#"]]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>教师可以让 4 个学生坐在可用的座位上,这样他们就无法在考试中作弊。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>seats = [[".","#"],
|
||||
["#","#"],
|
||||
["#","."],
|
||||
["#","#"],
|
||||
[".","#"]]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>让所有学生坐在可用的座位上。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>seats = [["#",".","<strong>.</strong>",".","#"],
|
||||
["<strong>.</strong>","#","<strong>.</strong>","#","<strong>.</strong>"],
|
||||
["<strong>.</strong>",".","#",".","<strong>.</strong>"],
|
||||
["<strong>.</strong>","#","<strong>.</strong>","#","<strong>.</strong>"],
|
||||
["#",".","<strong>.</strong>",".","#"]]
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>让学生坐在第 1、3 和 5 列的可用座位上。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>seats</code> 只包含字符 <code>'.' 和</code><code>'#'</code></li>
|
||||
<li><code>m == seats.length</code></li>
|
||||
<li><code>n == seats[i].length</code></li>
|
||||
<li><code>1 <= m <= 8</code></li>
|
||||
<li><code>1 <= n <= 8</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>给出一个字符串 <code>s</code>(仅含有小写英文字母和括号)。</p>
|
||||
|
||||
<p>请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。</p>
|
||||
|
||||
<p>注意,您的结果中 <strong>不应</strong> 包含任何括号。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "(abcd)"
|
||||
<strong>输出:</strong>"dcba"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "(u(love)i)"
|
||||
<strong>输出:</strong>"iloveu"
|
||||
<strong>解释:</strong>先反转子字符串 "love" ,然后反转整个字符串。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "(ed(et(oc))el)"
|
||||
<strong>输出:</strong>"leetcode"
|
||||
<strong>解释:</strong>先反转子字符串 "oc" ,接着反转 "etco" ,然后反转整个字符串。</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "a(bcdefghijkl(mno)p)q"
|
||||
<strong>输出:</strong>"apmnolkjihgfedcbq"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> 中只有小写英文字母和括号</li>
|
||||
<li>题目测试用例确保所有括号都是成对出现的</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>我们有一个 <code>n</code> 项的集合。给出两个整数数组 <code>values</code> 和 <code>labels</code> ,第 <code>i</code> 个元素的值和标签分别是 <code>values[i]</code> 和 <code>labels[i]</code>。还会给出两个整数 <code>numWanted</code> 和 <code>useLimit</code> 。</p>
|
||||
|
||||
<p>从 <code>n</code> 个元素中选择一个子集 <code>s</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li>子集 <code>s</code> 的大小 <strong>小于或等于</strong> <code>numWanted</code> 。</li>
|
||||
<li><code>s</code> 中 <strong>最多</strong> 有相同标签的 <code>useLimit</code> 项。</li>
|
||||
</ul>
|
||||
|
||||
<p>一个子集的 <strong>分数 </strong>是该子集的值之和。</p>
|
||||
|
||||
<p>返回子集 <code>s</code> 的最大 <strong>分数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>选出的子集是第一项,第三项和第五项。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2
|
||||
<strong>输出:</strong>12
|
||||
<strong>解释:</strong>选出的子集是第一项,第二项和第三项。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1
|
||||
<strong>输出:</strong>16
|
||||
<strong>解释:</strong>选出的子集是第一项和第四项。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == values.length == labels.length</code></li>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= values[i], labels[i] <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= numWanted, useLimit <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一个方程,左边用 <code>words</code> 表示,右边用 <code>result</code> 表示。</p>
|
||||
|
||||
<p>你需要根据以下规则检查方程是否可解:</p>
|
||||
|
||||
<ul>
|
||||
<li>每个字符都会被解码成一位数字(0 - 9)。</li>
|
||||
<li>每对不同的字符必须映射到不同的数字。</li>
|
||||
<li>每个 <code>words[i]</code> 和 <code>result</code> 都会被解码成一个没有前导零的数字。</li>
|
||||
<li>左侧数字之和(<code>words</code>)等于右侧数字(<code>result</code>)。 </li>
|
||||
</ul>
|
||||
|
||||
<p>如果方程可解,返回 <code>True</code>,否则返回 <code>False</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["SEND","MORE"], result = "MONEY"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>映射 'S'-> 9, 'E'->5, 'N'->6, 'D'->7, 'M'->1, 'O'->0, 'R'->8, 'Y'->'2'
|
||||
所以 "SEND" + "MORE" = "MONEY" , 9567 + 1085 = 10652</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["SIX","SEVEN","SEVEN"], result = "TWENTY"
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>映射 'S'-> 6, 'I'->5, 'X'->0, 'E'->8, 'V'->7, 'N'->2, 'T'->1, 'W'->'3', 'Y'->4
|
||||
所以 "SIX" + "SEVEN" + "SEVEN" = "TWENTY" , 650 + 68782 + 68782 = 138214</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["THIS","IS","TOO"], result = "FUNNY"
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>words = ["LEET","CODE"], result = "POINT"
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= words.length <= 5</code></li>
|
||||
<li><code>1 <= words[i].length, results.length <= 7</code></li>
|
||||
<li><code>words[i], result</code> 只含有大写英文字母</li>
|
||||
<li>表达式中使用的不同字符数最大为 10</li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>在一个 <strong>8x8</strong> 的棋盘上,放置着若干「黑皇后」和一个「白国王」。</p>
|
||||
|
||||
<p>给定一个由整数坐标组成的数组 <code>queens</code> ,表示黑皇后的位置;以及一对坐标 <code>king</code> ,表示白国王的位置,返回所有可以攻击国王的皇后的坐标(任意顺序)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/10/13/untitled-diagram.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0]
|
||||
<strong>输出:</strong>[[0,1],[1,0],[3,3]]
|
||||
<strong>解释:</strong>
|
||||
[0,1] 的皇后可以攻击到国王,因为他们在同一行上。
|
||||
[1,0] 的皇后可以攻击到国王,因为他们在同一列上。
|
||||
[3,3] 的皇后可以攻击到国王,因为他们在同一条对角线上。
|
||||
[0,4] 的皇后无法攻击到国王,因为她被位于 [0,1] 的皇后挡住了。
|
||||
[4,0] 的皇后无法攻击到国王,因为她被位于 [1,0] 的皇后挡住了。
|
||||
[2,4] 的皇后无法攻击到国王,因为她和国王不在同一行/列/对角线上。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/10/13/untitled-diagram-1.jpg" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king = [3,3]
|
||||
<strong>输出:</strong>[[2,2],[3,4],[4,4]]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/10/13/untitled-diagram-2.jpg" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>queens = [[5,6],[7,7],[2,1],[0,7],[1,6],[5,1],[3,7],[0,3],[4,0],[1,2],[6,3],[5,0],[0,4],[2,2],[1,1],[6,4],[5,4],[0,0],[2,6],[4,5],[5,2],[1,4],[7,5],[2,3],[0,5],[4,2],[1,0],[2,7],[0,1],[4,6],[6,1],[0,6],[4,3],[1,7]], king = [3,4]
|
||||
<strong>输出:</strong>[[2,3],[1,4],[1,6],[3,7],[4,3],[5,4],[4,5]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= queens.length <= 63</code></li>
|
||||
<li><code>queens[i].length == 2</code></li>
|
||||
<li><code>0 <= queens[i][j] < 8</code></li>
|
||||
<li><code>king.length == 2</code></li>
|
||||
<li><code>0 <= king[0], king[1] < 8</code></li>
|
||||
<li>一个棋盘格上最多只能放置一枚棋子。</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>键盘出现了一些故障,有些字母键无法正常工作。而键盘上所有其他键都能够正常工作。</p>
|
||||
|
||||
<p>给你一个由若干单词组成的字符串 <code>text</code> ,单词间由单个空格组成(不含前导和尾随空格);另有一个字符串 <code>brokenLetters</code> ,由所有已损坏的不同字母键组成,返回你可以使用此键盘完全输入的 <code>text</code> 中单词的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "hello world", brokenLetters = "ad"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>无法输入 "world" ,因为字母键 'd' 已损坏。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "leet code", brokenLetters = "lt"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>无法输入 "leet" ,因为字母键 'l' 和 't' 已损坏。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>text = "leet code", brokenLetters = "e"
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>无法输入任何单词,因为字母键 'e' 已损坏。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= text.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= brokenLetters.length <= 26</code></li>
|
||||
<li><code>text</code> 由若干用单个空格分隔的单词组成,且不含任何前导和尾随空格</li>
|
||||
<li>每个单词仅由小写英文字母组成</li>
|
||||
<li><code>brokenLetters</code> 由 <strong>互不相同</strong> 的小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>几张卡牌<strong> 排成一行</strong>,每张卡牌都有一个对应的点数。点数由整数数组 <code>cardPoints</code> 给出。</p>
|
||||
|
||||
<p>每次行动,你可以从行的开头或者末尾拿一张卡牌,最终你必须正好拿 <code>k</code> 张卡牌。</p>
|
||||
|
||||
<p>你的点数就是你拿到手中的所有卡牌的点数之和。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>cardPoints</code> 和整数 <code>k</code>,请你返回可以获得的最大点数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>cardPoints = [1,2,3,4,5,6,1], k = 3
|
||||
<strong>输出:</strong>12
|
||||
<strong>解释:</strong>第一次行动,不管拿哪张牌,你的点数总是 1 。但是,先拿最右边的卡牌将会最大化你的可获得点数。最优策略是拿右边的三张牌,最终点数为 1 + 6 + 5 = 12 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>cardPoints = [2,2,2], k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>无论你拿起哪两张卡牌,可获得的点数总是 4 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>cardPoints = [9,7,7,9,7,7,9], k = 7
|
||||
<strong>输出:</strong>55
|
||||
<strong>解释:</strong>你必须拿起所有卡牌,可以获得的点数为所有卡牌的点数之和。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>cardPoints = [1,1000,1], k = 1
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>你无法拿到中间那张卡牌,所以可以获得的最大点数为 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>cardPoints = [1,79,80,1,1,1,200,1], k = 3
|
||||
<strong>输出:</strong>202
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= cardPoints.length <= 10^5</code></li>
|
||||
<li><code>1 <= cardPoints[i] <= 10^4</code></li>
|
||||
<li><code>1 <= k <= cardPoints.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>给你一个整数数组 <code>nums</code>,请你找出并返回能被三整除的元素最大和。</p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [3,6,5,1,8]
|
||||
<strong>输出:</strong>18
|
||||
<strong>解释:</strong>选出数字 3, 6, 1 和 8,它们的和是 18(可被 3 整除的最大和)。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [4]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>4 不能被 3 整除,所以无法选出数字,返回 0。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,2,3,4,4]
|
||||
<strong>输出:</strong>12
|
||||
<strong>解释:</strong>选出数字 1, 3, 4 以及 4,它们的和是 12(可被 3 整除的最大和)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 4 * 10^4</code></li>
|
||||
<li><code>1 <= nums[i] <= 10^4</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个正整数数组 <code>arr</code>,考虑所有满足以下条件的二叉树:</p>
|
||||
|
||||
<ul>
|
||||
<li>每个节点都有 0 个或是 2 个子节点。</li>
|
||||
<li>数组 <code>arr</code> 中的值与树的中序遍历中每个叶节点的值一一对应。(知识回顾:如果一个节点有 0 个子节点,那么该节点为叶节点。)</li>
|
||||
<li>每个非叶节点的值等于其左子树和右子树中叶节点的最大值的乘积。</li>
|
||||
</ul>
|
||||
|
||||
<p>在所有这样的二叉树中,返回每个非叶节点的值的最小可能总和。这个和的值是一个 32 位整数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>arr = [6,2,4]
|
||||
<strong>输出:</strong>32
|
||||
<strong>解释:</strong>
|
||||
有两种可能的树,第一种的非叶节点的总和为 36,第二种非叶节点的总和为 32。
|
||||
|
||||
24 24
|
||||
/ \ / \
|
||||
12 4 6 8
|
||||
/ \ / \
|
||||
6 2 2 4</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= arr.length <= 40</code></li>
|
||||
<li><code>1 <= arr[i] <= 15</code></li>
|
||||
<li>答案保证是一个 32 位带符号整数,即小于 <code>2^31</code>。</li>
|
||||
</ul>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user