mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 07:21:40 +08:00
update
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的二维整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>返回位于 <code>nums</code> 至少一条 <strong>对角线</strong> 上的最大 <strong>质数</strong> 。如果任一对角线上均不存在质数,返回<em> 0 。</em></p>
|
||||
|
||||
<p>注意:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果某个整数大于 <code>1</code> ,且不存在除 <code>1</code> 和自身之外的正整数因子,则认为该整数是一个质数。</li>
|
||||
<li>如果存在整数 <code>i</code> ,使得 <code>nums[i][i] = val</code> 或者 <code>nums[i][nums.length - i - 1]= val</code> ,则认为整数 <code>val</code> 位于 <code>nums</code> 的一条对角线上。</li>
|
||||
</ul>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/03/06/screenshot-2023-03-06-at-45648-pm.png" style="width: 181px; height: 121px;" /></p>
|
||||
|
||||
<p>在上图中,一条对角线是 <strong>[1,5,9]</strong> ,而另一条对角线是<strong> [3,5,7]</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [[1,2,3],[5,6,7],[9,10,11]]
|
||||
<strong>输出:</strong>11
|
||||
<strong>解释:</strong>数字 1、3、6、9 和 11 是所有 "位于至少一条对角线上" 的数字。由于 11 是最大的质数,故返回 11 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [[1,2,3],[5,17,7],[9,11,10]]
|
||||
<strong>输出:</strong>17
|
||||
<strong>解释:</strong>数字 1、3、9、10 和 17 是所有满足"位于至少一条对角线上"的数字。由于 17 是最大的质数,故返回 17 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 300</code></li>
|
||||
<li><code>nums.length == nums<sub>i</sub>.length</code></li>
|
||||
<li><code>1 <= nums<span style="">[i][j]</span> <= 4*10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>请你编写一个函数,它接收一个 <strong>多维数组 </strong><code>arr</code> 和它的深度 <code>n</code> ,并返回该数组的 <strong>扁平化 </strong>后的结果。</p>
|
||||
|
||||
<p><strong>多维数组 </strong>是一种包含整数或其他 <strong>多维数组 </strong>的递归数据结构。</p>
|
||||
|
||||
<p>数组 <strong>扁平化</strong> 是对数组的一种操作,定义是将原数组部分或全部子数组删除,并替换为该子数组中的实际元素。只有当嵌套的数组深度大于 <code>n</code> 时,才应该执行扁平化操作。第一层数组中元素的深度被认为是 0。</p>
|
||||
|
||||
<p>请在没有使用内置方法 <code>Array.flat</code> 的前提下解决这个问题。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 0
|
||||
<strong>输出</strong>
|
||||
[1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
|
||||
<strong>解释</strong>
|
||||
传递深度 n=0 的多维数组将始终得到原始数组。这是因为 子数组(0) 的最小可能的深度不小于 n=0 。因此,任何子数组都不应该被平面化。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
arr = [1, 2, 3, [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 1
|
||||
<strong>输出</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, [9, 10, 11], 12, 13, 14, 15]
|
||||
|
||||
<strong>解释</strong>
|
||||
以 4 、7 和 13 开头的子数组都被扁平化了,这是因为它们的深度为 0 , 而 0 小于 1 。然而 [9,10,11] 其深度为 1 ,所以未被扁平化。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
arr = [[1, 2, 3], [4, 5, 6], [7, 8, [9, 10, 11], 12], [13, 14, 15]]
|
||||
n = 2
|
||||
<strong>输出</strong>
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
|
||||
|
||||
<strong>解释</strong>
|
||||
所有子数组的最大深度都为 1 。因此,它们都被扁平化了。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr 的元素个数 <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= arr 的子数组个数 <= 10<sup>5</sup></code></li>
|
||||
<li><code>maxDepth <= 1000</code></li>
|
||||
<li><code>-1000 <= each number <= 1000</code></li>
|
||||
<li><code><font face="monospace">0 <= n <= 1000</font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>p</code> 。请你从 <code>nums</code> 中找到 <code>p</code> 个下标对,每个下标对对应数值取差值,你需要使得这 <code>p</code> 个差值的 <strong>最大值</strong> <strong>最小</strong>。同时,你需要确保每个下标在这 <code>p</code> 个下标对中最多出现一次。</p>
|
||||
|
||||
<p>对于一个下标对 <code>i</code> 和 <code>j</code> ,这一对的差值为 <code>|nums[i] - nums[j]|</code> ,其中 <code>|x|</code> 表示 <code>x</code> 的 <strong>绝对值</strong> 。</p>
|
||||
|
||||
<p>请你返回 <code>p</code> 个下标对对应数值 <strong>最大差值</strong> 的 <strong>最小值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [10,1,2,7,1,3], p = 2
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>第一个下标对选择 1 和 4 ,第二个下标对选择 2 和 5 。
|
||||
最大差值为 max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1 。所以我们返回 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [4,2,1,2], p = 1
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>选择下标 1 和 3 构成下标对。差值为 |2 - 2| = 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>9</sup></code></li>
|
||||
<li><code>0 <= p <= (nums.length)/2</code></li>
|
||||
</ul>
|
35
leetcode-cn/problem (Chinese)/等值距离和 [sum-of-distances].html
Normal file
35
leetcode-cn/problem (Chinese)/等值距离和 [sum-of-distances].html
Normal file
@@ -0,0 +1,35 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。现有一个长度等于 <code>nums.length</code> 的数组 <code>arr</code> 。对于满足 <code>nums[j] == nums[i]</code> 且 <code>j != i</code> 的所有 <code>j</code> ,<code>arr[i]</code> 等于所有 <code>|i - j|</code> 之和。如果不存在这样的 <code>j</code> ,则令 <code>arr[i]</code> 等于 <code>0</code> 。</p>
|
||||
|
||||
<p>返回数组<em> </em><code>arr</code><em> 。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,3,1,1,2]
|
||||
<strong>输出:</strong>[5,0,3,4,0]
|
||||
<strong>解释:</strong>
|
||||
i = 0 ,nums[0] == nums[2] 且 nums[0] == nums[3] 。因此,arr[0] = |0 - 2| + |0 - 3| = 5 。
|
||||
i = 1 ,arr[1] = 0 因为不存在值等于 3 的其他下标。
|
||||
i = 2 ,nums[2] == nums[0] 且 nums[2] == nums[3] 。因此,arr[2] = |2 - 0| + |2 - 3| = 3 。
|
||||
i = 3 ,nums[3] == nums[0] 且 nums[3] == nums[2] 。因此,arr[3] = |3 - 0| + |3 - 2| = 4 。
|
||||
i = 4 ,arr[4] = 0 因为不存在值等于 2 的其他下标。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,5,3]
|
||||
<strong>输出:</strong>[0,0,0]
|
||||
<strong>解释:</strong>因为 nums 中的元素互不相同,对于所有 i ,都有 arr[i] = 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>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的 <code>m x n</code> 整数矩阵 <code>grid</code> 。你一开始的位置在 <strong>左上角</strong> 格子 <code>(0, 0)</code> 。</p>
|
||||
|
||||
<p>当你在格子 <code>(i, j)</code> 的时候,你可以移动到以下格子之一:</p>
|
||||
|
||||
<ul>
|
||||
<li>满足 <code>j < k <= grid[i][j] + j</code> 的格子 <code>(i, k)</code> (向右移动),或者</li>
|
||||
<li>满足 <code>i < k <= grid[i][j] + i</code> 的格子 <code>(k, j)</code> (向下移动)。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回到达 <strong>右下角</strong> 格子 <code>(m - 1, n - 1)</code> 需要经过的最少移动格子数,如果无法到达右下角格子,请你返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>上图展示了到达右下角格子经过的 4 个格子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
|
||||
<b>输出:</b>3
|
||||
<strong>解释:</strong>上图展示了到达右下角格子经过的 3 个格子。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;"></p>
|
||||
|
||||
<pre><b>输入:</b>grid = [[2,1,0],[1,0,0]]
|
||||
<b>输出:</b>-1
|
||||
<b>解释:</b>无法到达右下角格子。
|
||||
</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 <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= grid[i][j] < m * n</code></li>
|
||||
<li><code>grid[m - 1][n - 1] == 0</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user