1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-05 07:21:40 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2023-07-03 20:48:37 +08:00
parent e5097bc0f6
commit 25bf8d9df8
79 changed files with 20800 additions and 13311 deletions

View File

@@ -0,0 +1,46 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;<code>nums</code>&nbsp;的一个子数组如果满足以下条件,那么它是 <strong>不间断</strong> 的:</p>
<ul>
<li><code>i</code><code>i + 1</code>&nbsp;...<code>j</code><sub> </sub>&nbsp;表示子数组中的下标。对于所有满足&nbsp;<code>i &lt;= i<sub>1</sub>, i<sub>2</sub> &lt;= j</code>&nbsp;的下标对,都有 <code>0 &lt;= |nums[i<sub>1</sub>] - nums[i<sub>2</sub>]| &lt;= 2</code>&nbsp;</li>
</ul>
<p>请你返回 <strong>不间断</strong> 子数组的总数目。</p>
<p>子数组是一个数组中一段连续 <strong>非空</strong>&nbsp;的元素序列。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>nums = [5,4,2,4]
<strong>输出:</strong>8
<b>解释:</b>
大小为 1 的不间断子数组:[5], [4], [2], [4] 。
大小为 2 的不间断子数组:[5,4], [4,2], [2,4] 。
大小为 3 的不间断子数组:[4,2,4] 。
没有大小为 4 的不间断子数组。
不间断子数组的总数目为 4 + 3 + 1 = 8 。
除了这些以外,没有别的不间断子数组。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>nums = [1,2,3]
<b>输出:</b>6
<b>解释:</b>
大小为 1 的不间断子数组:[1], [2], [3] 。
大小为 2 的不间断子数组:[1,2], [2,3] 。
大小为 3 的不间断子数组:[1,2,3] 。
不间断子数组的总数目为 3 + 2 + 1 = 6 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,36 @@
<p>给你一个整数 <code>n</code> 。如果两个整数 <code>x</code><code>y</code> 满足下述条件,则认为二者形成一个质数对:</p>
<ul>
<li><code>1 &lt;= x &lt;= y &lt;= n</code></li>
<li><code>x + y == n</code></li>
<li><code>x</code><code>y</code> 都是质数</li>
</ul>
<p>请你以二维有序列表的形式返回符合题目要求的所有 <code>[x<sub>i</sub>, y<sub>i</sub>]</code> ,列表需要按 <code>x<sub>i</sub></code><strong>非递减顺序</strong> 排序。如果不存在符合要求的质数对,则返回一个空数组。</p>
<p><strong>注意:</strong>质数是大于 <code>1</code> 的自然数,并且只有两个因子,即它本身和 <code>1</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>n = 10
<strong>输出:</strong>[[3,7],[5,5]]
<strong>解释:</strong>在这个例子中,存在满足条件的两个质数对。
这两个质数对分别是 [3,7] 和 [5,5],按照题面描述中的方式排序后返回。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>n = 2
<strong>输出:</strong>[]
<strong>解释:</strong>可以证明不存在和为 2 的质数对,所以返回一个空数组。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@@ -0,0 +1,61 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的数组&nbsp;<code>words</code>&nbsp;,它包含 <code>n</code>&nbsp;个字符串。</p>
<p>定义 <strong>连接</strong>&nbsp;操作&nbsp;<code>join(x, y)</code>&nbsp;表示将字符串&nbsp;<code>x</code>&nbsp;<code>y</code>&nbsp;连在一起,得到&nbsp;<code>xy</code>&nbsp;。如果&nbsp;<code>x</code>&nbsp;的最后一个字符与&nbsp;<code>y</code>&nbsp;的第一个字符相等,连接后两个字符中的一个会被&nbsp;<strong>删除</strong>&nbsp;</p>
<p>比方说&nbsp;<code>join("ab", "ba") = "aba"</code>&nbsp;&nbsp;<code>join("ab", "cde") = "abcde"</code>&nbsp;</p>
<p>你需要执行&nbsp;<code>n - 1</code>&nbsp;&nbsp;<strong>连接</strong>&nbsp;操作。令&nbsp;<code>str<sub>0</sub> = words[0]</code>&nbsp;,从&nbsp;<code>i = 1</code> 直到&nbsp;<code>i = n - 1</code>&nbsp;,对于第&nbsp;<code>i</code>&nbsp;个操作,你可以执行以下操作之一:</p>
<ul>
<li>&nbsp;<code>str<sub>i</sub> = join(str<sub>i - 1</sub>, words[i])</code></li>
<li>&nbsp;<code>str<sub>i</sub> = join(words[i], str<sub>i - 1</sub>)</code></li>
</ul>
<p>你的任务是使&nbsp;<code>str<sub>n - 1</sub></code>&nbsp;的长度<strong>&nbsp;最小&nbsp;</strong></p>
<p>请你返回一个整数,表示&nbsp;<code>str<sub>n - 1</sub></code>&nbsp;的最小长度。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>words = ["aa","ab","bc"]
<b>输出:</b>4
<strong>解释:</strong>这个例子中,我们按以下顺序执行连接操作,得到 <code>str<sub>2</sub></code> 的最小长度:
<code>str<sub>0</sub> = "aa"</code>
<code>str<sub>1</sub> = join(str<sub>0</sub>, "ab") = "aab"
</code><code>str<sub>2</sub> = join(str<sub>1</sub>, "bc") = "aabc"</code>
<code>str<sub>2</sub></code> 的最小长度为 4 。</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>words = ["ab","b"]
<b>输出:</b>2
<b>解释:</b>这个例子中str<sub>0</sub> = "ab",可以得到两个不同的 str<sub>1</sub>
join(str<sub>0</sub>, "b") = "ab" 或者 join("b", str<sub>0</sub>) = "bab" 。
第一个字符串 "ab" 的长度最短,所以答案为 2 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>words = ["aaa","c","aba"]
<b>输出:</b>6
<b>解释:</b>这个例子中,我们按以下顺序执行连接操作,得到 <code>str<sub>2</sub>&nbsp;的最小长度:</code>
<code>str<sub>0</sub> = "</code>aaa"
<code>str<sub>1</sub> = join(str<sub>0</sub>, "c") = "aaac"</code>
<code>str<sub>2</sub> = join("aba", str<sub>1</sub>) = "abaaac"</code>
<code>str<sub>2</sub></code> 的最小长度为 6 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= words.length &lt;= 1000</code></li>
<li><code>1 &lt;= words[i].length &lt;= 50</code></li>
<li><code>words[i]</code>&nbsp;中只包含小写英文字母。</li>
</ul>

View File

@@ -0,0 +1,36 @@
<p>给你一个二元数组 <code>nums</code></p>
<p>如果数组中的某个子数组 <strong>恰好</strong> 只存在 <strong></strong> 个值为 <code>1</code> 的元素,则认为该子数组是一个 <strong>好子数组</strong></p>
<p>请你统计将数组 <code>nums</code> 划分成若干 <strong>好子数组</strong> 的方法数,并以整数形式返回。由于数字可能很大,返回其对 <code>10<sup>9</sup> + 7</code> <strong>取余 </strong>之后的结果。</p>
<p>子数组是数组中的一个连续 <strong>非空</strong> 元素序列。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [0,1,0,0,1]
<strong>输出:</strong>3
<strong>解释:</strong>存在 3 种可以将 nums 划分成若干好子数组的方式:
- [0,1] [0,0,1]
- [0,1,0] [0,1]
- [0,1,0,0] [1]
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [0,1,0]
<strong>输出:</strong>1
<strong>解释:</strong>存在 1 种可以将 nums 划分成若干好子数组的方式:
- [0,1,0]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= nums[i] &lt;= 1</code></li>
</ul>

View File

@@ -0,0 +1,38 @@
<p>给你两个整数:<code>num1</code><code>num2</code></p>
<p>在一步操作中,你需要从范围&nbsp;<code>[0, 60]</code> 中选出一个整数 <code>i</code> ,并从 <code>num1</code> 减去 <code>2<sup>i</sup> + num2</code></p>
<p>请你计算,要想使 <code>num1</code> 等于 <code>0</code> 需要执行的最少操作数,并以整数形式返回。</p>
<p>如果无法使 <code>num1</code> 等于 <code>0</code> ,返回 <code>-1</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>num1 = 3, num2 = -2
<strong>输出:</strong>3
<strong>解释:</strong>可以执行下述步骤使 3 等于 0
- 选择 i = 2 ,并从 3 减去 2<sup>2</sup> + (-2) num1 = 3 - (4 + (-2)) = 1 。
- 选择 i = 2 ,并从 1 减去 2<sup>2</sup> + (-2) num1 = 1 - (4 + (-2)) = -1 。
- 选择 i = 0 ,并从 -1 减去 2<sup>0</sup>&nbsp;+ (-2) num1 = (-1) - (1 + (-2)) = 0 。
可以证明 3 是需要执行的最少操作数。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>num1 = 5, num2 = 7
<strong>输出:</strong>-1
<strong>解释:</strong>可以证明,执行操作无法使 5 等于 0 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= num1 &lt;= 10<sup>9</sup></code></li>
<li><code>-10<sup>9</sup>&nbsp;&lt;= num2 &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,48 @@
<p>一个长度为 <code>n</code>&nbsp;下标从 <strong>0</strong>&nbsp;开始的整数数组 <code>arr</code>&nbsp;<strong>不平衡数字</strong>&nbsp;定义为,在&nbsp;<code>sarr = sorted(arr)</code>&nbsp;数组中,满足以下条件的下标数目:</p>
<ul>
<li><code>0 &lt;= i &lt; n - 1</code>&nbsp;,和</li>
<li><code>sarr[i+1] - sarr[i] &gt; 1</code></li>
</ul>
<p>这里,<code>sorted(arr)</code>&nbsp;表示将数组 <code>arr</code>&nbsp;排序后得到的数组。</p>
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;,请你返回它所有&nbsp;<strong>子数组</strong>&nbsp;&nbsp;<strong>不平衡数字</strong>&nbsp;之和。</p>
<p>子数组指的是一个数组中连续一段 <strong>非空</strong>&nbsp;的元素序列。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [2,3,1,4]
<b>输出:</b>3
<b>解释:</b>总共有 3 个子数组有非 0 不平衡数字:
- 子数组 [3, 1] ,不平衡数字为 1 。
- 子数组 [3, 1, 4] ,不平衡数字为 1 。
- 子数组 [1, 4] ,不平衡数字为 1 。
其他所有子数组的不平衡数字都是 0 ,所以所有子数组的不平衡数字之和为 3 。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>nums = [1,3,3,3,5]
<b>输出:</b>8
<b>解释:</b>总共有 7 个子数组有非 0 不平衡数字:
- 子数组 [1, 3] ,不平衡数字为 1 。
- 子数组 [1, 3, 3] ,不平衡数字为 1 。
- 子数组 [1, 3, 3, 3] ,不平衡数字为 1 。
- 子数组 [1, 3, 3, 3, 5] ,不平衡数字为 2 。
- 子数组 [3, 3, 3, 5] ,不平衡数字为 1 。
- 子数组 [3, 3, 5] ,不平衡数字为 1 。
- 子数组 [3, 5] ,不平衡数字为 1 。
其他所有子数组的不平衡数字都是 0 ,所以所有子数组的不平衡数字之和为 8 。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
</ul>

View File

@@ -0,0 +1,54 @@
<p>给你一个下标从 <strong>0</strong>&nbsp;开始的数组&nbsp;<code>words</code>&nbsp;,数组中包含 <strong>互不相同</strong>&nbsp;的字符串。</p>
<p>如果字符串&nbsp;<code>words[i]</code>&nbsp;与字符串 <code>words[j]</code>&nbsp;满足以下条件,我们称它们可以匹配:</p>
<ul>
<li>字符串&nbsp;<code>words[i]</code>&nbsp;等于&nbsp;<code>words[j]</code>&nbsp;的反转字符串。</li>
<li><code>0 &lt;= i &lt; j &lt; words.length</code></li>
</ul>
<p>请你返回数组 <code>words</code>&nbsp;中的&nbsp;<strong>最大</strong>&nbsp;匹配数目。</p>
<p>注意,每个字符串最多匹配一次。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>words = ["cd","ac","dc","ca","zz"]
<b>输出:</b>2
<strong>解释:</strong>在此示例中,我们可以通过以下方式匹配 2 对字符串:
- 我们将第 0 个字符串与第 2 个字符串匹配,因为 word[0] 的反转字符串是 "dc" 并且等于 words[2]。
- 我们将第 1 个字符串与第 3 个字符串匹配,因为 word[1] 的反转字符串是 "ca" 并且等于 words[3]。
可以证明最多匹配数目是 2 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>words = ["ab","ba","cc"]
<b>输出:</b>1
<b>解释:</b>在此示例中,我们可以通过以下方式匹配 1 对字符串:
- 我们将第 0 个字符串与第 1 个字符串匹配,因为 words[1] 的反转字符串 "ab" 与 words[0] 相等。
可以证明最多匹配数目是 1 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>words = ["aa","ab"]
<b>输出:</b>0
<strong>解释:</strong>这个例子中,无法匹配任何字符串。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= words.length &lt;= 50</code></li>
<li><code>words[i].length == 2</code></li>
<li><code>words</code>&nbsp;包含的字符串互不相同。</li>
<li><code>words[i]</code>&nbsp;只包含小写英文字母。</li>
</ul>

View File

@@ -0,0 +1,50 @@
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>threshold</code></p>
<p>请你从 <code>nums</code> 的子数组中找出以下标 <code>l</code> 开头、下标 <code>r</code> 结尾 <code>(0 &lt;= l &lt;= r &lt; nums.length)</code> 且满足以下条件的 <strong>最长子数组</strong> </p>
<ul>
<li><code>nums[l] % 2 == 0</code></li>
<li>对于范围&nbsp;<code>[l, r - 1]</code> 内的所有下标 <code>i</code> <code>nums[i] % 2 != nums[i + 1] % 2</code></li>
<li>对于范围&nbsp;<code>[l, r]</code> 内的所有下标 <code>i</code> <code>nums[i] &lt;= threshold</code></li>
</ul>
<p>以整数形式返回满足题目要求的最长子数组的长度。</p>
<p><strong>注意:子数组</strong> 是数组中的一个连续非空元素序列。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [3,2,5,4], threshold = 5
<strong>输出:</strong>3
<strong>解释:</strong>在这个示例中,我们选择从 l = 1 开始、到 r = 3 结束的子数组 =&gt; [2,5,4] ,满足上述条件。
因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [1,2], threshold = 2
<strong>输出:</strong>1
<strong>解释:</strong>
在这个示例中,我们选择从 l = 1 开始、到 r = 1 结束的子数组 =&gt; [2] 。
该子数组满足上述全部条件。可以证明 1 是满足题目要求的最大长度。
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>nums = [2,3,4,5], threshold = 4
<strong>输出:</strong>3
<strong>解释:</strong>
在这个示例中,我们选择从 l = 0 开始、到 r = 2 结束的子数组 =&gt; [2,3,4] 。
该子数组满足上述全部条件。
因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100 </code></li>
<li><code>1 &lt;= nums[i] &lt;= 100 </code></li>
<li><code>1 &lt;= threshold &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,55 @@
<p>现有 <code>n</code> 个机器人,编号从 <strong>1</strong> 开始,每个机器人包含在路线上的位置、健康度和移动方向。</p>
<p>给你下标从 <strong>0</strong> 开始的两个整数数组 <code>positions</code><code>healths</code> 和一个字符串 <code>directions</code><code>directions[i]</code><strong>'L'</strong> 表示 <strong>向左</strong><strong>'R'</strong> 表示 <strong>向右</strong>)。 <code>positions</code> 中的所有整数 <strong>互不相同</strong></p>
<p>所有机器人以 <strong>相同速度</strong> <strong>同时</strong> 沿给定方向在路线上移动。如果两个机器人移动到相同位置,则会发生 <strong>碰撞</strong></p>
<p>如果两个机器人发生碰撞,则将 <strong>健康度较低</strong> 的机器人从路线中 <strong>移除</strong> ,并且另一个机器人的健康度 <strong>减少 1</strong> 。幸存下来的机器人将会继续沿着与之前 <strong>相同</strong> 的方向前进。如果两个机器人的健康度相同,则将二者都从路线中移除。</p>
<p>请你确定全部碰撞后幸存下的所有机器人的 <strong>健康度</strong> ,并按照原来机器人编号的顺序排列。即机器人 1 (如果幸存)的最终健康度,机器人 2 (如果幸存)的最终健康度等。 如果不存在幸存的机器人,则返回空数组。</p>
<p>在不再发生任何碰撞后,请你以数组形式,返回所有剩余机器人的健康度(按机器人输入中的编号顺序)。</p>
<p><strong>注意:</strong>位置&nbsp; <code>positions</code> 可能是乱序的。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p>
<pre>
<strong>输入:</strong>positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = "RRRRR"
<strong>输出:</strong>[2,17,9,15,10]
<strong>解释:</strong>在本例中不存在碰撞,因为所有机器人向同一方向移动。所以,从第一个机器人开始依序返回健康度,[2, 17, 9, 15, 10] 。
</pre>
<p><strong>示例 2</strong></p>
<p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p>
<pre>
<strong>输入:</strong>positions = [3,5,2,6], healths = [10,10,15,12], directions = "RLRL"
<strong>输出:</strong>[14]
<strong>解释:</strong>本例中发生 2 次碰撞。首先,机器人 1 和机器人 2 将会碰撞,因为二者健康度相同,二者都将被从路线中移除。接下来,机器人 3 和机器人 4 将会发生碰撞,由于机器人 4 的健康度更小,则它会被移除,而机器人 3 的健康度变为 15 - 1 = 14 。仅剩机器人 3 ,所以返回 [14] 。
</pre>
<p><strong>示例 3</strong></p>
<p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p>
<pre>
<strong>输入:</strong>positions = [1,2,5,6], healths = [10,10,11,11], directions = "RLRL"
<strong>输出:</strong>[]
<strong>解释:</strong>机器人 1 和机器人 2 将会碰撞,因为二者健康度相同,二者都将被从路线中移除。机器人 3 和机器人 4 将会碰撞,因为二者健康度相同,二者都将被从路线中移除。所以返回空数组 [] 。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code><code>directions[i] == 'R'</code></li>
<li><code>positions</code> 中的所有值互不相同</li>
</ul>

View File

@@ -0,0 +1,35 @@
<p>给你三个整数&nbsp;<code>x</code>&nbsp;<code>y</code>&nbsp;&nbsp;<code>z</code>&nbsp;</p>
<p>这三个整数表示你有&nbsp;<code>x</code>&nbsp;&nbsp;<code>"AA"</code>&nbsp;字符串,<code>y</code>&nbsp;&nbsp;<code>"BB"</code>&nbsp;字符串,和&nbsp;<code>z</code>&nbsp;&nbsp;<code>"AB"</code>&nbsp;字符串。你需要选择这些字符串中的部分字符串(可以全部选择也可以一个都不选择),将它们按顺序连接得到一个新的字符串。新字符串不能包含子字符串&nbsp;<code>"AAA"</code>&nbsp;或者&nbsp;<code>"BBB"</code>&nbsp;</p>
<p>请你返回新字符串的最大可能长度。</p>
<p><strong>子字符串</strong>&nbsp;是一个字符串中一段连续 <strong>非空</strong>&nbsp;的字符序列。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>x = 2, y = 5, z = 1
<b>输出:</b>12
<strong>解释: </strong>我们可以按顺序连接 "BB" "AA" "BB" "AA" "BB" 和 "AB" ,得到新字符串 "BBAABBAABBAB" 。
字符串长度为 12 ,无法得到一个更长的符合题目要求的字符串。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>x = 3, y = 2, z = 2
<b>输出:</b>14
<b>解释:</b>我们可以按顺序连接 "AB" "AB" "AA" "BB" "AA" "BB" 和 "AA" ,得到新字符串 "ABABAABBAABBAA" 。
字符串长度为 14 ,无法得到一个更长的符合题目要求的字符串。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= x, y, z &lt;= 50</code></li>
</ul>

View File

@@ -0,0 +1,44 @@
<p>给你一个整数&nbsp;<code>n</code>&nbsp;,表示服务器的总数目,再给你一个下标从 <strong>0</strong>&nbsp;开始的 <strong>二维</strong>&nbsp;整数数组&nbsp;<code>logs</code>&nbsp;,其中&nbsp;<code>logs[i] = [server_id, time]</code>&nbsp;表示 id 为&nbsp;<code>server_id</code>&nbsp;的服务器在&nbsp;<code>time</code>&nbsp;时收到了一个请求。</p>
<p>同时给你一个整数&nbsp;<code>x</code>&nbsp;和一个下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>queries</code>&nbsp;</p>
<p>请你返回一个长度等于&nbsp;<code>queries.length</code>&nbsp;的数组&nbsp;<code>arr</code>&nbsp;,其中&nbsp;<code>arr[i]</code>&nbsp;表示在时间区间&nbsp;<code>[queries[i] - x, queries[i]]</code>&nbsp;内没有收到请求的服务器数目。</p>
<p>注意时间区间是个闭区间。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]
<b>输出:</b>[1,2]
<b>解释:</b>
对于 queries[0]id 为 1 和 2 的服务器在区间 [5, 10] 内收到了请求,所以只有服务器 3 没有收到请求。
对于 queries[1]id 为 2 的服务器在区间 [6,11] 内收到了请求,所以 id 为 1 和 3 的服务器在这个时间段内没有收到请求。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4]
<b>输出:</b>[0,1]
<b>解释:</b>
对于 queries[0]:区间 [1, 3] 内所有服务器都收到了请求。
对于 queries[1]:只有 id 为 3 的服务器在区间 [2,4] 内没有收到请求。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= logs.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>logs[i].length == 2</code></li>
<li><code>1 &lt;= logs[i][0] &lt;= n</code></li>
<li><code>1 &lt;= logs[i][1] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= x &lt;= 10<sup>5</sup></code></li>
<li><code>x &lt;&nbsp;queries[i]&nbsp;&lt;= 10<sup>6</sup></code></li>
</ul>

View File

@@ -0,0 +1,41 @@
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。如果下标对 <code>i</code><code>j</code> 满足 <code>0 ≤ i &lt; j &lt; nums.length</code> ,如果&nbsp;<code>nums[i]</code><strong>第一个数字</strong><code>nums[j]</code><strong>最后一个数字</strong> <strong>互质</strong> ,则认为 <code>nums[i]</code><code>nums[j]</code> 是一组 <strong>美丽下标对</strong></p>
<p>返回 <code>nums</code><strong>美丽下标对</strong> 的总数目。</p>
<p>对于两个整数 <code>x</code><code>y</code> ,如果不存在大于 1 的整数可以整除它们,则认为 <code>x</code><code>y</code> <strong>互质</strong> 。换而言之,如果 <code>gcd(x, y) == 1</code> ,则认为 <code>x</code><code>y</code> 互质,其中 <code>gcd(x, y)</code><code>x</code><code>k</code> <strong>最大公因数</strong></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [2,5,1,4]
<strong>输出:</strong>5
<strong>解释:</strong>nums 中共有 5 组美丽下标对:
i = 0 和 j = 1 nums[0] 的第一个数字是 2 nums[1] 的最后一个数字是 5 。2 和 5 互质,因此 gcd(2,5) == 1 。
i = 0 和 j = 2 nums[0] 的第一个数字是 2 nums[1] 的最后一个数字是 1 。2 和 5 互质,因此 gcd(2,1) == 1 。
i = 1 和 j = 2 nums[0] 的第一个数字是 5 nums[1] 的最后一个数字是 1 。2 和 5 互质,因此 gcd(5,1) == 1 。
i = 1 和 j = 3 nums[0] 的第一个数字是 5 nums[1] 的最后一个数字是 4 。2 和 5 互质,因此 gcd(5,4) == 1 。
i = 2 和 j = 3 nums[0] 的第一个数字是 1 nums[1] 的最后一个数字是 4 。2 和 5 互质,因此 gcd(1,4) == 1 。
因此,返回 5 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [11,21,12]
<strong>输出:</strong>2
<strong>解释:</strong>共有 2 组美丽下标对:
i = 0 和 j = 1 nums[0] 的第一个数字是 1 nums[1] 的最后一个数字是 1 。gcd(1,1) == 1 。
i = 0 和 j = 2 nums[0] 的第一个数字是 1 nums[1] 的最后一个数字是 2 。gcd(1,2) == 1 。
因此,返回 2 。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 9999</code></li>
<li><code>nums[i] % 10 != 0</code></li>
</ul>