Skip to content

Commit a697596

Browse files
committed
update
1 parent 9e50b3c commit a697596

23 files changed

+15743
-13660
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 力扣题库(完整版)
22

3-
> 最后更新日期: **2024.01.13**
3+
> 最后更新日期: **2024.01.30**
44
>
55
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
66

Diff for: leetcode-cn/origin-data.json

+7,717-7,633
Large diffs are not rendered by default.

Diff for: leetcode-cn/originData/alice-and-bob-playing-flower-game.json

+170
Large diffs are not rendered by default.

Diff for: leetcode-cn/originData/find-the-maximum-number-of-elements-in-subset.json

+185
Large diffs are not rendered by default.

Diff for: leetcode-cn/originData/minimize-or-of-remaining-elements-using-operations.json

+188
Large diffs are not rendered by default.

Diff for: leetcode-cn/originData/number-of-changing-keys.json

+170
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>Alice 和 Bob 在一个长满鲜花的环形草地玩一个回合制游戏。环形的草地上有一些鲜花,Alice 到&nbsp;Bob 之间顺时针有 <code>x</code>&nbsp;朵鲜花,逆时针有 <code>y</code>&nbsp;朵鲜花。</p>
2+
3+
<p>游戏过程如下:</p>
4+
5+
<ol>
6+
<li>Alice 先行动。</li>
7+
<li>每一次行动中,当前玩家必须选择顺时针或者逆时针,然后在这个方向上摘一朵鲜花。</li>
8+
<li>一次行动结束后,如果所有鲜花都被摘完了,那么 <strong>当前</strong>&nbsp;玩家抓住对手并赢得游戏的胜利。</li>
9+
</ol>
10+
11+
<p>给你两个整数&nbsp;<code>n</code>&nbsp;和&nbsp;<code>m</code>&nbsp;,你的任务是求出满足以下条件的所有&nbsp;<code>(x, y)</code>&nbsp;对:</p>
12+
13+
<ul>
14+
<li>按照上述规则,Alice 必须赢得游戏。</li>
15+
<li>Alice 顺时针方向上的鲜花数目&nbsp;<code>x</code>&nbsp;必须在区间&nbsp;<code>[1,n]</code>&nbsp;之间。</li>
16+
<li>Alice 逆时针方向上的鲜花数目 <code>y</code>&nbsp;必须在区间&nbsp;<code>[1,m]</code>&nbsp;之间。</li>
17+
</ul>
18+
19+
<p>请你返回满足题目描述的数对&nbsp;<code>(x, y)</code>&nbsp;的数目。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong class="example">示例 1:</strong></p>
24+
25+
<pre>
26+
<b>输入:</b>n = 3, m = 2
27+
<b>输出:</b>3
28+
<b>解释:</b>以下数对满足题目要求:(1,2) ,(3,2) ,(2,1) 。
29+
</pre>
30+
31+
<p><strong class="example">示例 2:</strong></p>
32+
33+
<pre>
34+
<b>输入:</b>n = 1, m = 1
35+
<b>输出:</b>0
36+
<b>解释:</b>没有数对满足题目要求。
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= n, m &lt;= 10<sup>5</sup></code></li>
45+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>给你一个<strong> 正整数 </strong>数组 <code>nums</code></p>
2+
3+
<p>你需要从数组中选出一个满足下述条件的<span data-keyword="subset">子集</span></p>
4+
5+
<ul>
6+
<li>你可以将选中的元素放置在一个下标从 <strong>0</strong> 开始的数组中,并使其遵循以下模式:<code>[x, x<sup>2</sup>, x<sup>4</sup>, ..., x<sup>k/2</sup>, x<sup>k</sup>, x<sup>k/2</sup>, ..., x<sup>4</sup>, x<sup>2</sup>, x]</code><strong>注意</strong><code>k</code> 可以是任何 <strong>非负</strong> 的 2 的幂)。例如,<code>[2, 4, 16, 4, 2]</code><code>[3, 9, 3]</code> 都符合这一模式,而 <code>[2, 4, 8, 4, 2]</code> 则不符合。</li>
7+
</ul>
8+
9+
<p>返回满足这些条件的子集中,元素数量的 <strong>最大值 </strong><em></em></p>
10+
11+
<p>&nbsp;</p>
12+
13+
<p><strong class="example">示例 1:</strong></p>
14+
15+
<pre>
16+
<strong>输入:</strong>nums = [5,4,1,2,2]
17+
<strong>输出:</strong>3
18+
<strong>解释:</strong>选择子集 {4,2,2} ,将其放在数组 [2,4,2] 中,它遵循该模式,且 2<sup>2</sup> == 4 。因此答案是 3 。
19+
</pre>
20+
21+
<p><strong class="example">示例 2:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>nums = [1,3,2,4]
25+
<strong>输出:</strong>1
26+
<strong>解释:</strong>选择子集 {1},将其放在数组 [1] 中,它遵循该模式。因此答案是 1 。注意我们也可以选择子集 {2} 、{4} 或 {3} ,可能存在多个子集都能得到相同的答案。
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
31+
<p><strong>提示:</strong></p>
32+
33+
<ul>
34+
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
35+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
36+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>给你一个下标从<strong> 0</strong> 开始的字符串 <code>s</code> ,该字符串由用户输入。按键变更的定义是:使用与上次使用的按键不同的键。例如 <code>s = "ab"</code> 表示按键变更一次,而 <code>s = "bBBb"</code> 不存在按键变更。</p>
2+
3+
<p>返回用户输入过程中按键变更的次数。</p>
4+
5+
<p><strong>注意:</strong><code>shift</code><code>caps lock</code> 等修饰键不计入按键变更,也就是说,如果用户先输入字母 <code>'a'</code> 然后输入字母 <code>'A'</code> ,不算作按键变更。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong class="example">示例 1:</strong></p>
10+
11+
<pre>
12+
<strong>输入:</strong>s = "aAbBcC"
13+
<strong>输出:</strong>2
14+
<strong>解释:</strong>
15+
从 s[0] = 'a' 到 s[1] = 'A',不存在按键变更,因为不计入 caps lock 或 shift 。
16+
从 s[1] = 'A' 到 s[2] = 'b',按键变更。
17+
从 s[2] = 'b' 到 s[3] = 'B',不存在按键变更,因为不计入 caps lock 或 shift 。
18+
从 s[3] = 'B' 到 s[4] = 'c',按键变更。
19+
从 s[4] = 'c' 到 s[5] = 'C',不存在按键变更,因为不计入 caps lock 或 shift 。
20+
</pre>
21+
22+
<p><strong class="example">示例 2:</strong></p>
23+
24+
<pre>
25+
<strong>输入:</strong>s = "AaAaAaaA"
26+
<strong>输出:</strong>0
27+
<strong>解释:</strong> 不存在按键变更,因为这个过程中只按下字母 'a' 和 'A' ,不需要进行按键变更。<!-- notionvc: 8849fe75-f31e-41dc-a2e0-b7d33d8427d2 -->
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
32+
<p><strong>提示:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
36+
<li><code>s</code> 仅由英文大写字母和小写字母组成。</li>
37+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;。</p>
2+
3+
<p>一次操作中,你可以选择 <code>nums</code>&nbsp;中满足&nbsp;<code>0 &lt;= i &lt; nums.length - 1</code>&nbsp;的一个下标 <code>i</code>&nbsp;,并将&nbsp;<code>nums[i]</code> 和&nbsp;<code>nums[i + 1]</code>&nbsp;替换为数字&nbsp;<code>nums[i] &amp; nums[i + 1]</code>&nbsp;,其中&nbsp;<code>&amp;</code>&nbsp;表示按位&nbsp;<code>AND</code>&nbsp;操作。</p>
4+
5+
<p>请你返回 <strong>至多</strong>&nbsp;<code>k</code>&nbsp;次操作以内,使 <code>nums</code>&nbsp;中所有剩余元素按位 <code>OR</code>&nbsp;结果的 <strong>最小值</strong>&nbsp;。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong class="example">示例 1:</strong></p>
10+
11+
<pre>
12+
<b>输入:</b>nums = [3,5,3,2,7], k = 2
13+
<b>输出:</b>3
14+
<b>解释:</b>执行以下操作:
15+
1. 将 nums[0] 和 nums[1] 替换为 (nums[0] &amp; nums[1]) ,得到 nums 为 [1,3,2,7] 。
16+
2. 将 nums[2] 和 nums[3] 替换为 (nums[2] &amp; nums[3]) ,得到 nums 为 [1,3,2] 。
17+
最终数组的按位或值为 3 。
18+
3 是 k 次操作以内,可以得到的剩余元素的最小按位或值。</pre>
19+
20+
<p><strong class="example">示例 2:</strong></p>
21+
22+
<pre>
23+
<b>输入:</b>nums = [7,3,15,14,2,8], k = 4
24+
<b>输出:</b>2
25+
<b>解释:</b>执行以下操作:
26+
1. 将 nums[0] 和 nums[1] 替换为 (nums[0] &amp; nums[1]) ,得到 nums 为 [3,15,14,2,8] 。
27+
2. 将 nums[0] 和 nums[1] 替换为 (nums[0] &amp; nums[1]) ,得到 nums 为 [3,14,2,8] 。
28+
3. 将 nums[0] 和 nums[1] 替换为 (nums[0] &amp; nums[1]) ,得到 nums 为 [2,2,8] 。
29+
4. 将 nums[1] 和 nums[2] 替换为 (nums[1] &amp; nums[2]) ,得到 nums 为 [2,0] 。
30+
最终数组的按位或值为 2 。
31+
2 是 k 次操作以内,可以得到的剩余元素的最小按位或值。
32+
</pre>
33+
34+
<p><strong class="example">示例 3:</strong></p>
35+
36+
<pre>
37+
<b>输入:</b>nums = [10,7,10,3,9,14,9,4], k = 1
38+
<b>输出:</b>15
39+
<b>解释:</b>不执行任何操作,nums 的按位或值为 15 。
40+
15 是 k 次操作以内,可以得到的剩余元素的最小按位或值。
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
49+
<li><code>0 &lt;= nums[i] &lt; 2<sup>30</sup></code></li>
50+
<li><code>0 &lt;= k &lt; nums.length</code></li>
51+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<p>Alice and Bob are playing a turn-based game on a circular field surrounded by flowers. The circle represents the field, and there are <code>x</code> flowers in the clockwise direction between Alice and Bob, and <code>y</code> flowers in the anti-clockwise direction between them.</p>
2+
3+
<p>The game proceeds as follows:</p>
4+
5+
<ol>
6+
<li>Alice takes the first turn.</li>
7+
<li>In each turn, a player must choose either the clockwise or anti-clockwise direction and pick one flower from that side.</li>
8+
<li>At the end of the turn, if there are no flowers left at all, the <strong>current</strong> player captures their opponent and wins the game.</li>
9+
</ol>
10+
11+
<p>Given two integers, <code>n</code> and <code>m</code>, the task is to compute the number of possible pairs <code>(x, y)</code> that satisfy the conditions:</p>
12+
13+
<ul>
14+
<li>Alice must win the game according to the described rules.</li>
15+
<li>The number of flowers <code>x</code> in the clockwise direction must be in the range <code>[1,n]</code>.</li>
16+
<li>The number of flowers <code>y</code> in the anti-clockwise direction must be in the range <code>[1,m]</code>.</li>
17+
</ul>
18+
19+
<p>Return <em>the number of possible pairs</em> <code>(x, y)</code> <em>that satisfy the conditions mentioned in the statement</em>.</p>
20+
21+
<p>&nbsp;</p>
22+
<p><strong class="example">Example 1:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> n = 3, m = 2
26+
<strong>Output:</strong> 3
27+
<strong>Explanation:</strong> The following pairs satisfy conditions described in the statement: (1,2), (3,2), (2,1).
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> n = 1, m = 1
34+
<strong>Output:</strong> 0
35+
<strong>Explanation:</strong> No pairs satisfy the conditions described in the statement.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= n, m &lt;= 10<sup>5</sup></code></li>
43+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
2+
3+
<p>You need to select a <span data-keyword="subset">subset</span> of <code>nums</code> which satisfies the following condition:</p>
4+
5+
<ul>
6+
<li>You can place the selected elements in a <strong>0-indexed</strong> array such that it follows the pattern: <code>[x, x<sup>2</sup>, x<sup>4</sup>, ..., x<sup>k/2</sup>, x<sup>k</sup>, x<sup>k/2</sup>, ..., x<sup>4</sup>, x<sup>2</sup>, x]</code> (<strong>Note</strong> that <code>k</code> can be be any <strong>non-negative</strong> power of <code>2</code>). For example, <code>[2, 4, 16, 4, 2]</code> and <code>[3, 9, 3]</code> follow the pattern while <code>[2, 4, 8, 4, 2]</code> does not.</li>
7+
</ul>
8+
9+
<p>Return <em>the <strong>maximum</strong> number of elements in a subset that satisfies these conditions.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums = [5,4,1,2,2]
16+
<strong>Output:</strong> 3
17+
<strong>Explanation:</strong> We can select the subset {4,2,2}, which can be placed in the array as [2,4,2] which follows the pattern and 2<sup>2</sup> == 4. Hence the answer is 3.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [1,3,2,4]
24+
<strong>Output:</strong> 1
25+
<strong>Explanation:</strong> We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {4}, or {3}, there may be multiple subsets which provide the same answer.
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
33+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
34+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>You are given a <strong>0-indexed </strong>string <code>s</code> typed by a user. Changing a key is defined as using a key different from the last used key. For example, <code>s = &quot;ab&quot;</code> has a change of a key while <code>s = &quot;bBBb&quot;</code> does not have any.</p>
2+
3+
<p>Return <em>the number of times the user had to change the key. </em></p>
4+
5+
<p><strong>Note: </strong>Modifiers like <code>shift</code> or <code>caps lock</code> won&#39;t be counted in changing the key that is if a user typed the letter <code>&#39;a&#39;</code> and then the letter <code>&#39;A&#39;</code> then it will not be considered as a changing of key.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> s = &quot;aAbBcC&quot;
12+
<strong>Output:</strong> 2
13+
<strong>Explanation:</strong>
14+
From s[0] = &#39;a&#39; to s[1] = &#39;A&#39;, there is no change of key as caps lock or shift is not counted.
15+
From s[1] = &#39;A&#39; to s[2] = &#39;b&#39;, there is a change of key.
16+
From s[2] = &#39;b&#39; to s[3] = &#39;B&#39;, there is no change of key as caps lock or shift is not counted.
17+
From s[3] = &#39;B&#39; to s[4] = &#39;c&#39;, there is a change of key.
18+
From s[4] = &#39;c&#39; to s[5] = &#39;C&#39;, there is no change of key as caps lock or shift is not counted.
19+
20+
</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s = &quot;AaAaAaaA&quot;
26+
<strong>Output:</strong> 0
27+
<strong>Explanation:</strong> There is no change of key since only the letters &#39;a&#39; and &#39;A&#39; are<!-- notionvc: 8849fe75-f31e-41dc-a2e0-b7d33d8427d2 --> pressed which does not require change of key.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
35+
<li><code>s</code> consists of only upper case and lower case English letters.</li>
36+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
2+
3+
<p>In one operation, you can pick any index <code>i</code> of <code>nums</code> such that <code>0 &lt;= i &lt; nums.length - 1</code> and replace <code>nums[i]</code> and <code>nums[i + 1]</code> with a single occurrence of <code>nums[i] &amp; nums[i + 1]</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p>
4+
5+
<p>Return <em>the <strong>minimum</strong> possible value of the bitwise </em><code>OR</code><em> of the remaining elements of</em> <code>nums</code> <em>after applying <strong>at most</strong></em> <code>k</code> <em>operations</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums = [3,5,3,2,7], k = 2
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong> Let&#39;s do the following operations:
14+
1. Replace nums[0] and nums[1] with (nums[0] &amp; nums[1]) so that nums becomes equal to [1,3,2,7].
15+
2. Replace nums[2] and nums[3] with (nums[2] &amp; nums[3]) so that nums becomes equal to [1,3,2].
16+
The bitwise-or of the final array is 3.
17+
It can be shown that 3 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> nums = [7,3,15,14,2,8], k = 4
23+
<strong>Output:</strong> 2
24+
<strong>Explanation:</strong> Let&#39;s do the following operations:
25+
1. Replace nums[0] and nums[1] with (nums[0] &amp; nums[1]) so that nums becomes equal to [3,15,14,2,8].
26+
2. Replace nums[0] and nums[1] with (nums[0] &amp; nums[1]) so that nums becomes equal to [3,14,2,8].
27+
3. Replace nums[0] and nums[1] with (nums[0] &amp; nums[1]) so that nums becomes equal to [2,2,8].
28+
4. Replace nums[1] and nums[2] with (nums[1] &amp; nums[2]) so that nums becomes equal to [2,0].
29+
The bitwise-or of the final array is 2.
30+
It can be shown that 2 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
31+
</pre>
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> nums = [10,7,10,3,9,14,9,4], k = 1
37+
<strong>Output:</strong> 15
38+
<strong>Explanation:</strong> Without applying any operations, the bitwise-or of nums is 15.
39+
It can be shown that 15 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
47+
<li><code>0 &lt;= nums[i] &lt; 2<sup>30</sup></code></li>
48+
<li><code>0 &lt;= k &lt; nums.length</code></li>
49+
</ul>

0 commit comments

Comments
 (0)