mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-10-20 04:26:48 +08:00
更新国内版新增题目
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和两个整数 <code>k</code> 和 <code>p</code> ,找出并返回满足要求的不同的子数组数,要求子数组中最多 <code>k</code> 个可被 <code>p</code> 整除的元素。</p>
|
||||
|
||||
<p>如果满足下述条件之一,则认为数组 <code>nums1</code> 和 <code>nums2</code> 是 <strong>不同</strong> 数组:</p>
|
||||
|
||||
<ul>
|
||||
<li>两数组长度 <strong>不同</strong> ,或者</li>
|
||||
<li>存在 <strong>至少 </strong>一个下标 <code>i</code> 满足 <code>nums1[i] != nums2[i]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>子数组</strong> 定义为:数组中的连续元素组成的一个 <strong>非空</strong> 序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [<em><strong>2</strong></em>,3,3,<em><strong>2</strong></em>,<em><strong>2</strong></em>], k = 2, p = 2
|
||||
<strong>输出:</strong>11
|
||||
<strong>解释:</strong>
|
||||
位于下标 0、3 和 4 的元素都可以被 p = 2 整除。
|
||||
共计 11 个不同子数组都满足最多含 k = 2 个可以被 2 整除的元素:
|
||||
[2]、[2,3]、[2,3,3]、[2,3,3,2]、[3]、[3,3]、[3,3,2]、[3,3,2,2]、[3,2]、[3,2,2] 和 [2,2] 。
|
||||
注意,尽管子数组 [2] 和 [3] 在 nums 中出现不止一次,但统计时只计数一次。
|
||||
子数组 [2,3,3,2,2] 不满足条件,因为其中有 3 个元素可以被 2 整除。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4], k = 4, p = 1
|
||||
<strong>输出:</strong>10
|
||||
<strong>解释:</strong>
|
||||
nums 中的所有元素都可以被 p = 1 整除。
|
||||
此外,nums 中的每个子数组都满足最多 4 个元素可以被 1 整除。
|
||||
因为所有子数组互不相同,因此满足所有限制条件的子数组总数为 10 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 200</code></li>
|
||||
<li><code>1 <= nums[i], p <= 200</code></li>
|
||||
<li><code>1 <= k <= nums.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>字符串的 <strong>引力</strong> 定义为:字符串中 <strong>不同</strong> 字符的数量。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>"abbca"</code> 的引力为 <code>3</code> ,因为其中有 <code>3</code> 个不同字符 <code>'a'</code>、<code>'b'</code> 和 <code>'c'</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个字符串 <code>s</code> ,返回 <strong>其所有子字符串的总引力</strong> <strong>。</strong></p>
|
||||
|
||||
<p><strong>子字符串</strong> 定义为:字符串中的一个连续字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "abbca"
|
||||
<strong>输出:</strong>28
|
||||
<strong>解释:</strong>"abbca" 的子字符串有:
|
||||
- 长度为 1 的子字符串:"a"、"b"、"b"、"c"、"a" 的引力分别为 1、1、1、1、1,总和为 5 。
|
||||
- 长度为 2 的子字符串:"ab"、"bb"、"bc"、"ca" 的引力分别为 2、1、2、2 ,总和为 7 。
|
||||
- 长度为 3 的子字符串:"abb"、"bbc"、"bca" 的引力分别为 2、2、3 ,总和为 7 。
|
||||
- 长度为 4 的子字符串:"abbc"、"bbca" 的引力分别为 3、3 ,总和为 6 。
|
||||
- 长度为 5 的子字符串:"abbca" 的引力为 3 ,总和为 3 。
|
||||
引力总和为 5 + 7 + 7 + 6 + 3 = 28 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>s = "code"
|
||||
<strong>输出:</strong>20
|
||||
<strong>解释:</strong>"code" 的子字符串有:
|
||||
- 长度为 1 的子字符串:"c"、"o"、"d"、"e" 的引力分别为 1、1、1、1 ,总和为 4 。
|
||||
- 长度为 2 的子字符串:"co"、"od"、"de" 的引力分别为 2、2、2 ,总和为 6 。
|
||||
- 长度为 3 的子字符串:"cod"、"ode" 的引力分别为 3、3 ,总和为 6 。
|
||||
- 长度为 4 的子字符串:"code" 的引力为 4 ,总和为 4 。
|
||||
引力总和为 4 + 6 + 6 + 4 = 20 。
|
||||
</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,26 @@
|
||||
<p>给你一个整数数组 <code>cards</code> ,其中 <code>cards[i]</code> 表示第 <code>i</code> 张卡牌的 <strong>值</strong> 。如果两张卡牌的值相同,则认为这一对卡牌 <strong>匹配</strong> 。</p>
|
||||
|
||||
<p>返回你必须拿起的最小连续卡牌数,以使在拿起的卡牌中有一对匹配的卡牌。如果无法得到一对匹配的卡牌,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>cards = [3,4,2,3,4,7]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>拿起卡牌 [3,4,2,3] 将会包含一对值为 3 的匹配卡牌。注意,拿起 [4,2,3,4] 也是最优方案。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>cards = [1,0,5,3]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>无法找出含一对匹配卡牌的一组连续卡牌。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= cards.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= cards[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始长度为 <code>n</code> 的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>下标 <code>i</code> 处的 <strong>平均差</strong> 指的是 <code>nums</code> 中 <strong>前</strong> <code>i + 1</code> 个元素平均值和 <strong>后</strong> <code>n - i - 1</code> 个元素平均值的 <strong>绝对差</strong> 。两个平均值都需要 <strong>向下取整</strong> 到最近的整数。</p>
|
||||
|
||||
<p>请你返回产生 <strong>最小平均差</strong> 的下标。如果有多个下标最小平均差相等,请你返回 <strong>最小</strong> 的一个下标。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>两个数的<strong> 绝对差</strong> 是两者差的绝对值。</li>
|
||||
<li> <code>n</code> 个元素的平均值是 <code>n</code> 个元素之 <strong>和</strong> 除以(整数除法) <code>n</code> 。</li>
|
||||
<li><code>0</code> 个元素的平均值视为 <code>0</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [2,5,3,9,5,3]
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>
|
||||
- 下标 0 处的平均差为:|2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3 。
|
||||
- 下标 1 处的平均差为:|(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2 。
|
||||
- 下标 2 处的平均差为:|(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2 。
|
||||
- 下标 3 处的平均差为:|(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0 。
|
||||
- 下标 4 处的平均差为:|(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1 。
|
||||
- 下标 5 处的平均差为:|(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4 。
|
||||
下标 3 处的平均差为最小平均差,所以返回 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [0]
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>
|
||||
唯一的下标是 0 ,所以我们返回 0 。
|
||||
下标 0 处的平均差为:|0 / 1 - 0| = |0 - 0| = 0 。
|
||||
</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>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个表示某个正整数的字符串 <code>number</code> 和一个字符 <code>digit</code> 。</p>
|
||||
|
||||
<p>从 <code>number</code> 中 <strong>恰好</strong> 移除 <strong>一个</strong> 等于 <code>digit</code> 的字符后,找出并返回按 <strong>十进制</strong> 表示 <strong>最大</strong> 的结果字符串。生成的测试用例满足 <code>digit</code> 在 <code>number</code> 中出现至少一次。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>number = "123", digit = "3"
|
||||
<strong>输出:</strong>"12"
|
||||
<strong>解释:</strong>"123" 中只有一个 '3' ,在移除 '3' 之后,结果为 "12" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>number = "1231", digit = "1"
|
||||
<strong>输出:</strong>"231"
|
||||
<strong>解释:</strong>可以移除第一个 '1' 得到 "231" 或者移除第二个 '1' 得到 "123" 。
|
||||
由于 231 > 123 ,返回 "231" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>number = "551", digit = "5"
|
||||
<strong>输出:</strong>"51"
|
||||
<strong>解释:</strong>可以从 "551" 中移除第一个或者第二个 '5' 。
|
||||
两种方案的结果都是 "51" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= number.length <= 100</code></li>
|
||||
<li><code>number</code> 由数字 <code>'1'</code> 到 <code>'9'</code> 组成</li>
|
||||
<li><code>digit</code> 是 <code>'1'</code> 到 <code>'9'</code> 中的一个数字</li>
|
||||
<li><code>digit</code> 在 <code>number</code> 中出现至少一次</li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
给你一个整数 <code>n</code> ,统计并返回各位数字都不同的数字 <code>x</code> 的个数,其中 <code>0 <= x < 10<sup>n</sup></code><sup> </sup>。
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2
|
||||
<strong>输出:</strong>91
|
||||
<strong>解释:</strong>答案应为除去 <code>11、22、33、44、55、66、77、88、99 </code>外,在 0 ≤ x < 100 范围内的所有数字。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 0
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 8</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个字符串数组 <code>words</code> 和一个字符串 <code>s</code> ,其中 <code>words[i]</code> 和 <code>s</code> 只包含 <strong>小写英文字母</strong> 。</p>
|
||||
|
||||
<p>请你返回 <code>words</code> 中是字符串 <code>s</code> <strong>前缀 </strong>的 <strong>字符串数目</strong> 。</p>
|
||||
|
||||
<p>一个字符串的 <strong>前缀</strong> 是出现在字符串开头的子字符串。<strong>子字符串</strong> 是一个字符串中的连续一段字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>words = ["a","b","c","ab","bc","abc"], s = "abc"
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>
|
||||
words 中是 s = "abc" 前缀的字符串为:
|
||||
"a" ,"ab" 和 "abc" 。
|
||||
所以 words 中是字符串 s 前缀的字符串数目为 3 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>words = ["a","a"], s = "aa"
|
||||
<b>输出:</b>2
|
||||
<strong>解释:
|
||||
</strong>两个字符串都是 s 的前缀。
|
||||
注意,相同的字符串可能在 words 中出现多次,它们应该被计数多次。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length, s.length <= 10</code></li>
|
||||
<li><code>words[i]</code> 和 <code>s</code> <strong>只</strong> 包含小写英文字母。</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你两个整数 <code>m</code> 和 <code>n</code> 表示一个下标从<strong> 0</strong> 开始的 <code>m x n</code> 网格图。同时给你两个二维整数数组 <code>guards</code> 和 <code>walls</code> ,其中 <code>guards[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> 且 <code>walls[j] = [row<sub>j</sub>, col<sub>j</sub>]</code> ,分别表示第 <code>i</code> 个警卫和第 <code>j</code> 座墙所在的位置。</p>
|
||||
|
||||
<p>一个警卫能看到 4 个坐标轴方向(即东、南、西、北)的 <strong>所有</strong> 格子,除非他们被一座墙或者另外一个警卫 <strong>挡住</strong> 了视线。如果一个格子能被 <strong>至少</strong> 一个警卫看到,那么我们说这个格子被 <strong>保卫</strong> 了。</p>
|
||||
|
||||
<p>请你返回空格子中,有多少个格子是 <strong>没被保卫</strong> 的。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/example1drawio2.png" style="width: 300px; height: 204px;"></p>
|
||||
|
||||
<pre><b>输入:</b>m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
|
||||
<b>输出:</b>7
|
||||
<strong>解释:</strong>上图中,被保卫和没有被保卫的格子分别用红色和绿色表示。
|
||||
总共有 7 个没有被保卫的格子,所以我们返回 7 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/example2drawio.png" style="width: 200px; height: 201px;"></p>
|
||||
|
||||
<pre><b>输入:</b>m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>上图中,没有被保卫的格子用绿色表示。
|
||||
总共有 4 个没有被保卫的格子,所以我们返回 4 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>2 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= guards.length, walls.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>2 <= guards.length + walls.length <= m * n</code></li>
|
||||
<li><code>guards[i].length == walls[j].length == 2</code></li>
|
||||
<li><code>0 <= row<sub>i</sub>, row<sub>j</sub> < m</code></li>
|
||||
<li><code>0 <= col<sub>i</sub>, col<sub>j</sub> < n</code></li>
|
||||
<li><code>guards</code> 和 <code>walls</code> 中所有位置 <strong>互不相同</strong> 。</li>
|
||||
</ul>
|
@@ -0,0 +1,62 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始大小为 <code>m x n</code> 的二维整数数组 <code>grid</code> ,它表示一个网格图。每个格子为下面 3 个值之一:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0</code> 表示草地。</li>
|
||||
<li><code>1</code> 表示着火的格子。</li>
|
||||
<li><code>2</code> 表示一座墙,你跟火都不能通过这个格子。</li>
|
||||
</ul>
|
||||
|
||||
<p>一开始你在最左上角的格子 <code>(0, 0)</code> ,你想要到达最右下角的安全屋格子 <code>(m - 1, n - 1)</code> 。每一分钟,你可以移动到 <strong>相邻</strong> 的草地格子。每次你移动 <strong>之后</strong> ,着火的格子会扩散到所有不是墙的 <strong>相邻</strong> 格子。</p>
|
||||
|
||||
<p>请你返回你在初始位置可以停留的 <strong>最多 </strong>分钟数,且停留完这段时间后你还能安全到达安全屋。如果无法实现,请你返回 <code>-1</code> 。如果不管你在初始位置停留多久,你 <strong>总是</strong> 能到达安全屋,请你返回 <code>10<sup>9</sup></code> 。</p>
|
||||
|
||||
<p>注意,如果你到达安全屋后,火马上到了安全屋,这视为你能够安全到达安全屋。</p>
|
||||
|
||||
<p>如果两个格子有共同边,那么它们为 <strong>相邻</strong> 格子。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/ex1new.jpg" style="width: 650px; height: 404px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[0,2,0,0,0,0,0],[0,0,0,2,2,1,0],[0,2,0,0,1,2,0],[0,0,2,2,2,0,2],[0,0,0,0,0,0,0]]
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>上图展示了你在初始位置停留 3 分钟后的情形。
|
||||
你仍然可以安全到达安全屋。
|
||||
停留超过 3 分钟会让你无法安全到达安全屋。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/ex2new2.jpg" style="width: 515px; height: 150px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]]
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>上图展示了你马上开始朝安全屋移动的情形。
|
||||
火会蔓延到你可以移动的所有格子,所以无法安全到达安全屋。
|
||||
所以返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/03/10/ex3new.jpg" style="width: 174px; height: 150px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[0,0,0],[2,2,0],[1,2,0]]
|
||||
<b>输出:</b>1000000000
|
||||
<b>解释:</b>上图展示了初始网格图。
|
||||
注意,由于火被墙围了起来,所以无论如何你都能安全到达安全屋。
|
||||
所以返回 10<sup>9</sup> 。
|
||||
</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>2 <= m, n <= 300</code></li>
|
||||
<li><code>4 <= m * n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>grid[i][j]</code> 是 <code>0</code> ,<code>1</code> 或者 <code>2</code> 。</li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user