mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 15:31:43 +08:00
update
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<p>给你两个正整数 <code>x</code> 和 <code>y</code> 。</p>
|
||||
|
||||
<p>一次操作中,你可以执行以下四种操作之一:</p>
|
||||
|
||||
<ol>
|
||||
<li>如果 <code>x</code> 是 <code>11</code> 的倍数,将 <code>x</code> 除以 <code>11</code> 。</li>
|
||||
<li>如果 <code>x</code> 是 <code>5</code> 的倍数,将 <code>x</code> 除以 <code>5</code> 。</li>
|
||||
<li>将 <code>x</code> 减 <code>1</code> 。</li>
|
||||
<li>将 <code>x</code> 加 <code>1</code> 。</li>
|
||||
</ol>
|
||||
|
||||
<p>请你返回让 <code>x</code> 和 <code>y</code> 相等的 <strong>最少</strong> 操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>x = 26, y = 1
|
||||
<b>输出:</b>3
|
||||
<b>解释</b><strong>:</strong>我们可以通过以下操作将 26 变为 1 :
|
||||
1. 将 x 减 1
|
||||
2. 将 x 除以 5
|
||||
3. 将 x 除以 5
|
||||
将 26 变为 1 最少需要 3 次操作。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>x = 54, y = 2
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>我们可以通过以下操作将 54 变为 2 :
|
||||
1. 将 x 加 1
|
||||
2. 将 x 除以 11
|
||||
3. 将 x 除以 5
|
||||
4. 将 x 加 1
|
||||
将 54 变为 2 最少需要 4 次操作。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>x = 25, y = 30
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>我们可以通过以下操作将 25 变为 30 :
|
||||
1. 将 x 加 1
|
||||
2. 将 x 加 1
|
||||
3. 将 x 加 1
|
||||
4. 将 x 加 1
|
||||
5. 将 x 加 1
|
||||
将 25 变为 30 最少需要 5 次操作。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x, y <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个正整数 <code>k</code> 。</p>
|
||||
|
||||
<p>你可以对数组执行以下操作 <strong>任意次</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>选择数组里的 <strong>任意</strong> 一个元素,并将它的 <strong>二进制</strong> 表示 <strong>翻转</strong> 一个数位,翻转数位表示将 <code>0</code> 变成 <code>1</code> 或者将 <code>1</code> 变成 <code>0</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>你的目标是让数组里 <strong>所有</strong> 元素的按位异或和得到 <code>k</code> ,请你返回达成这一目标的 <strong>最少 </strong>操作次数。</p>
|
||||
|
||||
<p><strong>注意</strong>,你也可以将一个数的前导 0 翻转。比方说,数字 <code>(101)<sub>2</sub></code> 翻转第四个数位,得到 <code>(1101)<sub>2</sub></code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,1,3,4], k = 1
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>我们可以执行以下操作:
|
||||
- 选择下标为 2 的元素,也就是 3 == (011)<sub>2</sub> ,我们翻转第一个数位得到 (010)<sub>2</sub> == 2 。数组变为 [2,1,2,4] 。
|
||||
- 选择下标为 0 的元素,也就是 2 == (010)<sub>2</sub> ,我们翻转第三个数位得到 (110)<sub>2</sub> == 6 。数组变为 [6,1,2,4] 。
|
||||
最终数组的所有元素异或和为 (6 XOR 1 XOR 2 XOR 4) == 1 == k 。
|
||||
无法用少于 2 次操作得到异或和等于 k 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,0,2,0], k = 0
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>数组所有元素的异或和为 (2 XOR 0 XOR 2 XOR 0) == 0 == k 。所以不需要进行任何操作。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,80 @@
|
||||
<p>给你一个长度为 <strong>偶数</strong> <code>n</code> ,下标从 <strong>0</strong> 开始的字符串 <code>s</code> 。</p>
|
||||
|
||||
<p>同时给你一个下标从 <strong>0</strong> 开始的二维整数数组 <code>queries</code> ,其中 <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>, d<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<p>对于每个查询 <code>i</code> ,你需要执行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>将下标在范围 <code>0 <= a<sub>i</sub> <= b<sub>i</sub> < n / 2</code> 内的 <strong>子字符串</strong> <code>s[a<sub>i</sub>:b<sub>i</sub>]</code> 中的字符重新排列。</li>
|
||||
<li>将下标在范围 <code>n / 2 <= c<sub>i</sub> <= d<sub>i</sub> < n</code> 内的 <strong>子字符串</strong> <code>s[c<sub>i</sub>:d<sub>i</sub>]</code> 中的字符重新排列。</li>
|
||||
</ul>
|
||||
|
||||
<p>对于每个查询,你的任务是判断执行操作后能否让 <code>s</code> 变成一个 <strong>回文串</strong> 。</p>
|
||||
|
||||
<p>每个查询与其他查询都是 <strong>独立的</strong> 。</p>
|
||||
|
||||
<p>请你返回一个下标从 <strong>0</strong> 开始的数组<em> </em><code>answer</code> ,如果第 <code>i</code> 个查询执行操作后,可以将 <code>s</code> 变为一个回文串,那么<em> </em><code>answer[i] = true</code>,否则为<em> </em><code>false</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>子字符串</strong> 指的是一个字符串中一段连续的字符序列。</li>
|
||||
<li><code>s[x:y]</code> 表示 <code>s</code> 中从下标 <code>x</code> 到 <code>y</code> 且两个端点 <strong>都包含</strong> 的子字符串。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "abcabc", queries = [[1,1,3,5],[0,2,5,5]]
|
||||
<b>输出:</b>[true,true]
|
||||
<b>解释:</b>这个例子中,有 2 个查询:
|
||||
第一个查询:
|
||||
- a<sub>0</sub> = 1, b<sub>0</sub> = 1, c<sub>0</sub> = 3, d<sub>0</sub> = 5
|
||||
- 你可以重新排列 s[1:1] => a<em><strong>b</strong></em>cabc 和 s[3:5] => abc<em><strong>abc</strong></em> 。
|
||||
- 为了让 s 变为回文串,s[3:5] 可以重新排列得到 => abc<strong><em>cba </em></strong>。
|
||||
- 现在 s 是一个回文串。所以 answer[0] = true 。
|
||||
第二个查询:
|
||||
- a<sub>1</sub> = 0, b<sub>1</sub> = 2, c<sub>1</sub> = 5, d<sub>1</sub> = 5.
|
||||
- 你可以重新排列 s[0:2] => <em><strong>abc</strong></em>abc 和 s[5:5] => abcab<strong><em>c</em></strong> 。
|
||||
- 为了让 s 变为回文串,s[0:2] 可以重新排列得到 => <em><strong>cba</strong></em>abc 。
|
||||
- 现在 s 是一个回文串,所以 answer[1] = true 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "abbcdecbba", queries = [[0,2,7,9]]
|
||||
<b>输出:</b>[false]
|
||||
<b>解释:</b>这个示例中,只有一个查询。
|
||||
a<sub>0</sub> = 0, b<sub>0</sub> = 2, c<sub>0</sub> = 7, d<sub>0</sub> = 9.
|
||||
你可以重新排列 s[0:2] => <em><strong>abb</strong></em>cdecbba 和 s[7:9] => abbcdec<em><strong>bba</strong></em> 。
|
||||
无法通过重新排列这些子字符串使 s 变为一个回文串,因为 s[3:6] 不是一个回文串。
|
||||
所以 answer[0] = false 。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "acbcab", queries = [[1,2,4,5]]
|
||||
<b>输出:</b>[true]
|
||||
<strong>解释:</strong>这个示例中,只有一个查询。
|
||||
a<sub>0</sub> = 1, b<sub>0</sub> = 2, c<sub>0</sub> = 4, d<sub>0</sub> = 5.
|
||||
你可以重新排列 s[1:2] => a<em><strong>cb</strong></em>cab 和 s[4:5] => acbc<strong><em>ab</em></strong> 。
|
||||
为了让 s 变为回文串,s[1:2] 可以重新排列得到 => a<em><strong>bc</strong></em>cab<code> </code>。
|
||||
然后 s[4:5] 重新排列得到 abcc<em><strong>ba</strong></em> 。
|
||||
现在 s 是一个回文串,所以 answer[0] = true 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n == s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>queries[i].length == 4</code></li>
|
||||
<li><code>a<sub>i</sub> == queries[i][0], b<sub>i</sub> == queries[i][1]</code></li>
|
||||
<li><code>c<sub>i</sub> == queries[i][2], d<sub>i</sub> == queries[i][3]</code></li>
|
||||
<li><code>0 <= a<sub>i</sub> <= b<sub>i</sub> < n / 2</code></li>
|
||||
<li><code>n / 2 <= c<sub>i</sub> <= d<sub>i</sub> < n </code></li>
|
||||
<li><code>n</code> 是一个偶数。</li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>如果一个前缀 <code>nums[0..i]</code> 满足对于 <code>1 <= j <= i</code> 的所有元素都有 <code>nums[j] = nums[j - 1] + 1</code> ,那么我们称这个前缀是一个 <strong>顺序前缀</strong> 。特殊情况是,只包含 <code>nums[0]</code> 的前缀也是一个 <strong>顺序前缀</strong> 。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中没有出现过的 <strong>最小</strong> 整数 <code>x</code> ,满足 <code>x</code> 大于等于 <strong>最长</strong> 顺序前缀的和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,2,5]
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>nums 的最长顺序前缀是 [1,2,3] ,和为 6 ,6 不在数组中,所以 6 是大于等于最长顺序前缀和的最小整数。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,4,5,1,12,14,13]
|
||||
<b>输出:</b>15
|
||||
<b>解释:</b>nums 的最长顺序前缀是 [3,4,5] ,和为 12 ,12、13 和 14 都在数组中,但 15 不在,所以 15 是大于等于最长顺序前缀和的最小整数。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>给你一个下标从<strong> 0</strong> 开始的二维整数数组 <code>dimensions</code>。</p>
|
||||
|
||||
<p>对于所有下标 <code>i</code>(<code>0 <= i < dimensions.length</code>),<code>dimensions[i][0]</code> 表示矩形 <span style="font-size: 13.3333px;"> <code>i</code></span> 的长度,而 <code>dimensions[i][1]</code> 表示矩形 <span style="font-size: 13.3333px;"> <code>i</code></span> 的宽度。</p>
|
||||
|
||||
<p>返回对角线最 <strong>长 </strong>的矩形的<strong> 面积 </strong>。如果存在多个对角线长度相同的矩形,返回面积最<strong> 大 </strong>的矩形的面积。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>dimensions = [[9,3],[8,6]]
|
||||
<strong>输出:</strong>48
|
||||
<strong>解释:</strong>
|
||||
下标 = 0,长度 = 9,宽度 = 3。对角线长度 = sqrt(9 * 9 + 3 * 3) = sqrt(90) ≈<!-- notionvc: 882cf44c-3b17-428e-9c65-9940810216f1 --> 9.487。
|
||||
下标 = 1,长度 = 8,宽度 = 6。对角线长度 = sqrt(8 * 8 + 6 * 6) = sqrt(100) = 10。
|
||||
因此,下标为 1 的矩形对角线更长,所以返回面积 = 8 * 6 = 48。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>dimensions = [[3,4],[4,3]]
|
||||
<strong>输出:</strong>12
|
||||
<strong>解释:</strong>两个矩形的对角线长度相同,为 5,所以最大面积 = 12。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= dimensions.length <= 100</code></li>
|
||||
<li><code>dimensions[i].length == 2</code></li>
|
||||
<li><code>1 <= dimensions[i][0], dimensions[i][1] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,72 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> 和一个整数 <code>k</code>。</p>
|
||||
|
||||
<p>你需要执行以下分割操作,直到字符串 <code>s </code>变为 <strong>空</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>选择 <code>s</code> 的最长<strong>前缀</strong>,该前缀最多包含 <code>k </code>个 <strong>不同 </strong>字符。</li>
|
||||
<li><strong>删除 </strong>这个前缀,并将分割数量加一。如果有剩余字符,它们在 <code>s</code> 中保持原来的顺序。</li>
|
||||
</ul>
|
||||
|
||||
<p>执行操作之 <strong>前</strong> ,你可以将 <code>s</code> 中 <strong>至多一处 </strong>下标的对应字符更改为另一个小写英文字母。</p>
|
||||
|
||||
<p>在最优选择情形下改变至多一处下标对应字符后,用整数表示并返回操作结束时得到的最大分割数量。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "accca", k = 2
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>在此示例中,为了最大化得到的分割数量,可以将 s[2] 改为 'b'。
|
||||
s 变为 "acbca"。
|
||||
按照以下方式执行操作,直到 s 变为空:
|
||||
- 选择最长且至多包含 2 个不同字符的前缀,"<em><strong>ac</strong></em>bca"。
|
||||
- 删除该前缀,s 变为 "bca"。现在分割数量为 1。
|
||||
- 选择最长且至多包含 2 个不同字符的前缀,"<em><strong>bc</strong></em>a"。
|
||||
- 删除该前缀,s 变为 "a"。现在分割数量为 2。
|
||||
- 选择最长且至多包含 2 个不同字符的前缀,"<strong><em>a</em></strong>"。
|
||||
- 删除该前缀,s 变为空。现在分割数量为 3。
|
||||
因此,答案是 3。
|
||||
可以证明,分割数量不可能超过 3。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aabaab", k = 3
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>在此示例中,为了最大化得到的分割数量,可以保持 s 不变。
|
||||
按照以下方式执行操作,直到 s 变为空:
|
||||
- 选择最长且至多包含 3 个不同字符的前缀,"<em><strong>aabaab</strong></em>"。
|
||||
- 删除该前缀,s 变为空。现在分割数量为 1。
|
||||
因此,答案是 1。
|
||||
可以证明,分割数量不可能超过 1。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "xxyz", k = 1
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>在此示例中,为了最大化得到的分割数量,可以将 s[1] 改为 'a'。
|
||||
s 变为 "xayz"。
|
||||
按照以下方式执行操作,直到 s 变为空:
|
||||
- 选择最长且至多包含 1 个不同字符的前缀,"<em><strong>x</strong></em>ayz"。
|
||||
- 删除该前缀,s 变为 "ayz"。现在分割数量为 1。
|
||||
- 选择最长且至多包含 1 个不同字符的前缀,"<em><strong>a</strong></em>yz"。
|
||||
- 删除该前缀,s 变为 "yz",现在分割数量为 2。
|
||||
- 选择最长且至多包含 1 个不同字符的前缀,"<em><strong>y</strong></em>z"。
|
||||
- 删除该前缀,s 变为 "z"。现在分割数量为 3。
|
||||
- 选择最且至多包含 1 个不同字符的前缀,"<em>z</em>"。
|
||||
- 删除该前缀,s 变为空。现在分割数量为 4。
|
||||
因此,答案是 4。
|
||||
可以证明,分割数量不可能超过 4。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
<li><code>1 <= k <= 26</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你一个仅由小写英文字母组成的字符串 <code>s</code> 。</p>
|
||||
|
||||
<p>如果一个字符串仅由单一字符组成,那么它被称为 <strong>特殊 </strong>字符串。例如,字符串 <code>"abc"</code> 不是特殊字符串,而字符串 <code>"ddd"</code>、<code>"zz"</code> 和 <code>"f"</code> 是特殊字符串。</p>
|
||||
|
||||
<p>返回在 <code>s</code> 中出现 <strong>至少三次 </strong>的<strong> 最长特殊子字符串 </strong>的长度,如果不存在出现至少三次的特殊子字符串,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>子字符串 </strong>是字符串中的一个连续<strong> 非空 </strong>字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aaaa"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>出现三次的最长特殊子字符串是 "aa" :子字符串 "<em><strong>aa</strong></em>aa"、"a<em><strong>aa</strong></em>a" 和 "aa<em><strong>aa</strong></em>"。
|
||||
可以证明最大长度是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcdef"
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>不存在出现至少三次的特殊子字符串。因此返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcaba"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>出现三次的最长特殊子字符串是 "a" :子字符串 "<em><strong>a</strong></em>bcaba"、"abc<em><strong>a</strong></em>ba" 和 "abcab<em><strong>a</strong></em>"。
|
||||
可以证明最大长度是 1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 50</code></li>
|
||||
<li><code>s</code> 仅由小写英文字母组成。</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你一个仅由小写英文字母组成的字符串 <code>s</code> 。</p>
|
||||
|
||||
<p>如果一个字符串仅由单一字符组成,那么它被称为 <strong>特殊 </strong>字符串。例如,字符串 <code>"abc"</code> 不是特殊字符串,而字符串 <code>"ddd"</code>、<code>"zz"</code> 和 <code>"f"</code> 是特殊字符串。</p>
|
||||
|
||||
<p>返回在 <code>s</code> 中出现 <strong>至少三次 </strong>的<strong> 最长特殊子字符串 </strong>的长度,如果不存在出现至少三次的特殊子字符串,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>子字符串 </strong>是字符串中的一个连续<strong> 非空 </strong>字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aaaa"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>出现三次的最长特殊子字符串是 "aa" :子字符串 "<em><strong>aa</strong></em>aa"、"a<em><strong>aa</strong></em>a" 和 "aa<em><strong>aa</strong></em>"。
|
||||
可以证明最大长度是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcdef"
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>不存在出现至少三次的特殊子字符串。因此返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcaba"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>出现三次的最长特殊子字符串是 "a" :子字符串 "<em><strong>a</strong></em>bcaba"、"abc<em><strong>a</strong></em>ba" 和 "abcab<em><strong>a</strong></em>"。
|
||||
可以证明最大长度是 1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 仅由小写英文字母组成。</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>现有一个下标从 <strong>0</strong> 开始的 <code>8 x 8</code> 棋盘,上面有 <code>3</code> 枚棋子。</p>
|
||||
|
||||
<p>给你 <code>6</code> 个整数 <code>a</code> 、<code>b</code> 、<code>c</code> 、<code>d</code> 、<code>e</code> 和 <code>f</code> ,其中:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>(a, b)</code> 表示白色车的位置。</li>
|
||||
<li><code>(c, d)</code> 表示白色象的位置。</li>
|
||||
<li><code>(e, f)</code> 表示黑皇后的位置。</li>
|
||||
</ul>
|
||||
|
||||
<p>假定你只能移动白色棋子,返回捕获黑皇后所需的<strong>最少</strong>移动次数。</p>
|
||||
|
||||
<p><strong>请注意</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>车可以向垂直或水平方向移动任意数量的格子,但不能跳过其他棋子。</li>
|
||||
<li>象可以沿对角线方向移动任意数量的格子,但不能跳过其他棋子。</li>
|
||||
<li>如果车或象能移向皇后所在的格子,则认为它们可以捕获皇后。</li>
|
||||
<li>皇后不能移动。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/ex1.png" style="width: 600px; height: 600px; padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>a = 1, b = 1, c = 8, d = 8, e = 2, f = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>将白色车先移动到 (1, 3) ,然后移动到 (2, 3) 来捕获黑皇后,共需移动 2 次。
|
||||
由于起始时没有任何棋子正在攻击黑皇后,要想捕获黑皇后,移动次数不可能少于 2 次。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/ex2.png" style="width: 600px; height: 600px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>a = 5, b = 3, c = 3, d = 4, e = 5, f = 2
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>可以通过以下任一方式移动 1 次捕获黑皇后:
|
||||
- 将白色车移动到 (5, 2) 。
|
||||
- 将白色象移动到 (5, 2) 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a, b, c, d, e, f <= 8</code></li>
|
||||
<li>两枚棋子不会同时出现在同一个格子上。</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>给你一个<strong> 正整数 </strong>数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>你需要检查是否可以从数组中选出 <strong>两个或更多 </strong>元素,满足这些元素的按位或运算( <code>OR</code>)结果的二进制表示中 <strong>至少</strong><strong> </strong>存在一个尾随零。</p>
|
||||
|
||||
<p>例如,数字 <code>5</code> 的二进制表示是 <code>"101"</code>,不存在尾随零,而数字 <code>4</code> 的二进制表示是 <code>"100"</code>,存在两个尾随零。</p>
|
||||
|
||||
<p>如果可以选择两个或更多元素,其按位或运算结果存在尾随零,返回 <code>true</code>;否则,返回<em> </em><code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4,5]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110" ,存在一个尾随零。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,4,8,16]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110",存在一个尾随零。
|
||||
其他按位或运算结果存在尾随零的可能选择方案包括:(2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), 以及 (2, 4, 8, 16) 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,3,5,7,9]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>不存在按位或运算结果存在尾随零的选择方案。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>给你两个下标从 <code>0</code> 开始的整数数组 <code>nums1</code> 和 <code>nums2</code> ,它们的长度都是偶数<code> n</code> 。</p>
|
||||
|
||||
<p>你必须从 <code>nums1</code> 中移除 <code>n / 2</code> 个元素,同时从 <code>nums2</code> 中也移除 <code>n / 2</code> 个元素。移除之后,你将 <code>nums1</code> 和 <code>nums2</code> 中剩下的元素插入到集合 <code>s</code> 中。</p>
|
||||
|
||||
<p>返回集合 <code>s</code>可能的<strong> 最多 </strong>包含多少元素。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [1,2,1,2], nums2 = [1,1,1,1]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>从 nums1 和 nums2 中移除两个 1 。移除后,数组变为 nums1 = [2,2] 和 nums2 = [1,1] 。因此,s = {1,2} 。
|
||||
可以证明,在移除之后,集合 s 最多可以包含 2 个元素。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>从 nums1 中移除 2、3 和 6 ,同时从 nums2 中移除两个 3 和一个 2 。移除后,数组变为 nums1 = [1,4,5] 和 nums2 = [2,3,2] 。因此,s = {1,2,3,4,5} 。
|
||||
可以证明,在移除之后,集合 s 最多可以包含 5 个元素。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>从 nums1 中移除 1、2 和 3 ,同时从 nums2 中移除 4、5 和 6 。移除后,数组变为 nums1 = [1,2,3] 和 nums2 = [4,5,6] 。因此,s = {1,2,3,4,5,6} 。
|
||||
可以证明,在移除之后,集合 s 最多可以包含 6 个元素。 </pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>n</code>是偶数。</li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你三个整数 <code>start</code> ,<code>finish</code> 和 <code>limit</code> 。同时给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> ,表示一个 <strong>正</strong> 整数。</p>
|
||||
|
||||
<p>如果一个 <strong>正</strong> 整数 <code>x</code> 末尾部分是 <code>s</code> (换句话说,<code>s</code> 是 <code>x</code> 的 <strong>后缀</strong>),且 <code>x</code> 中的每个数位至多是 <code>limit</code> ,那么我们称 <code>x</code> 是 <strong>强大的</strong> 。</p>
|
||||
|
||||
<p>请你返回区间 <code>[start..finish]</code> 内强大整数的 <strong>总数目</strong> 。</p>
|
||||
|
||||
<p>如果一个字符串 <code>x</code> 是 <code>y</code> 中某个下标开始(<strong>包括</strong> <code>0</code> ),到下标为 <code>y.length - 1</code> 结束的子字符串,那么我们称 <code>x</code> 是 <code>y</code> 的一个后缀。比方说,<code>25</code> 是 <code>5125</code> 的一个后缀,但不是 <code>512</code> 的后缀。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>start = 1, finish = 6000, limit = 4, s = "124"
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>区间 [1..6000] 内的强大数字为 124 ,1124 ,2124 ,3124 和 4124 。这些整数的各个数位都 <= 4 且 "124" 是它们的后缀。注意 5124 不是强大整数,因为第一个数位 5 大于 4 。
|
||||
这个区间内总共只有这 5 个强大整数。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>start = 15, finish = 215, limit = 6, s = "10"
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>区间 [15..215] 内的强大整数为 110 和 210 。这些整数的各个数位都 <= 6 且 "10" 是它们的后缀。
|
||||
这个区间总共只有这 2 个强大整数。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>start = 1000, finish = 2000, limit = 4, s = "3000"
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>区间 [1000..2000] 内的整数都小于 3000 ,所以 "3000" 不可能是这个区间内任何整数的后缀。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= start <= finish <= 10<sup>15</sup></code></li>
|
||||
<li><code>1 <= limit <= 9</code></li>
|
||||
<li><code>1 <= s.length <= floor(log<sub>10</sub>(finish)) + 1</code></li>
|
||||
<li><code>s</code> 数位中每个数字都小于等于 <code>limit</code> 。</li>
|
||||
<li><code>s</code> 不包含任何前导 0 。</li>
|
||||
</ul>
|
Reference in New Issue
Block a user