mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始长度为 <code>3</code> 的整数数组 <code>nums</code> ,需要用它们来构造三角形。</p>
|
||||
|
||||
<ul>
|
||||
<li>如果一个三角形的所有边长度相等,那么这个三角形称为 <strong>equilateral</strong> 。</li>
|
||||
<li>如果一个三角形恰好有两条边长度相等,那么这个三角形称为 <strong>isosceles</strong> 。</li>
|
||||
<li>如果一个三角形三条边的长度互不相同,那么这个三角形称为 <strong>scalene</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果这个数组无法构成一个三角形,请你返回字符串 <code>"none"</code> ,否则返回一个字符串表示这个三角形的类型。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,3,3]
|
||||
<b>输出:</b>"equilateral"
|
||||
<b>解释:</b>由于三条边长度相等,所以可以构成一个等边三角形,返回 "equilateral" 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,4,5]
|
||||
<b>输出:</b>"scalene"
|
||||
<b>解释:</b>
|
||||
nums[0] + nums[1] = 3 + 4 = 7 ,大于 nums[2] = 5<code> 。</code>
|
||||
nums[0] + nums[2] = 3 + 5 = 8 ,大于 nums[1] = 4 。
|
||||
nums[1] + nums[2] = 4 + 5 = 9 ,大于 nums[0] = 3 。
|
||||
由于任意两边纸盒都大于第三边,所以可以构成一个三角形。
|
||||
因为三条边的长度互不相等,所以返回 "scalene" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums.length == 3</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,64 @@
|
||||
<p>给你一个 <code>n x 2</code> 的二维数组 <code>points</code> ,它表示二维平面上的一些点坐标,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<p>我们定义 x 轴的正方向为 <strong>右</strong> (<strong>x 轴递增的方向</strong>),x 轴的负方向为 <strong>左</strong> (<strong>x 轴递减的方向</strong>)。类似的,我们定义 y 轴的正方向为 <strong>上</strong> (<strong>y 轴递增的方向</strong>),y 轴的负方向为 <strong>下</strong> (<strong>y 轴递减的方向</strong>)。</p>
|
||||
|
||||
<p>你需要安排这 <code>n</code> 个人的站位,这 <code>n</code> 个人中包括 liupengsay 和小羊肖恩 。你需要确保每个点处 <strong>恰好</strong> 有 <strong>一个</strong> 人。同时,liupengsay 想跟小羊肖恩单独玩耍,所以 liupengsay 会以 liupengsay<b> </b>的坐标为 <strong>左上角</strong> ,小羊肖恩的坐标为 <strong>右下角</strong> 建立一个矩形的围栏(<strong>注意</strong>,围栏可能 <strong>不</strong> 包含任何区域,也就是说围栏可能是一条线段)。如果围栏的 <strong>内部</strong> 或者 <strong>边缘</strong> 上有任何其他人,liupengsay 都会难过。</p>
|
||||
|
||||
<p>请你在确保 liupengsay <strong>不会</strong> 难过的前提下,返回 liupengsay 和小羊肖恩可以选择的 <strong>点对</strong> 数目。</p>
|
||||
|
||||
<p><b>注意</b>,liupengsay 建立的围栏必须确保 liupengsay 的位置是矩形的左上角,小羊肖恩的位置是矩形的右下角。比方说,以 <code>(1, 1)</code> ,<code>(1, 3)</code> ,<code>(3, 1)</code> 和 <code>(3, 3)</code> 为矩形的四个角,给定下图的两个输入,liupengsay 都不能建立围栏,原因如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>图一中,liupengsay 在 <code>(3, 3)</code> 且小羊肖恩在 <code>(1, 1)</code> ,liupengsay 的位置不是左上角且小羊肖恩的位置不是右下角。</li>
|
||||
<li>图二中,liupengsay 在 <code>(1, 3)</code> 且小羊肖恩在 <code>(1, 1)</code> ,小羊肖恩的位置不是在围栏的右下角。</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png" style="width: 750px; height: 308px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 376px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[1,1],[2,2],[3,3]]
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>没有办法可以让 liupengsay 的围栏以 liupengsay 的位置为左上角且小羊肖恩的位置为右下角。所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><strong class="example"><a href="https://pic.leetcode.cn/1706880313-YelabI-example2.jpeg"><img alt="" src="https://pic.leetcode.cn/1706880313-YelabI-example2.jpeg" style="width: 900px; height: 247px;" /></a></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[6,2],[4,4],[2,6]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>总共有 2 种方案安排 liupengsay 和小羊肖恩的位置,使得 liupengsay 不会难过:
|
||||
- liupengsay 站在 (4, 4) ,小羊肖恩站在 (6, 2) 。
|
||||
- liupengsay 站在 (2, 6) ,小羊肖恩站在 (4, 4) 。
|
||||
不能安排 liupengsay 站在 (2, 6) 且小羊肖恩站在 (6, 2) ,因为站在 (4, 4) 的人处于围栏内。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<p><strong class="example"><a href="https://pic.leetcode.cn/1706880311-mtPGYC-example3.jpeg"><img alt="" src="https://pic.leetcode.cn/1706880311-mtPGYC-example3.jpeg" style="width: 900px; height: 247px;" /></a></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[3,1],[1,3],[1,1]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>总共有 2 种方案安排 liupengsay 和小羊肖恩的位置,使得 liupengsay 不会难过:
|
||||
- liupengsay 站在 (1, 1) ,小羊肖恩站在 (3, 1) 。
|
||||
- liupengsay 站在 (1, 3) ,小羊肖恩站在 (1, 1) 。
|
||||
不能安排 liupengsay 站在 (1, 3) 且小羊肖恩站在 (3, 1) ,因为站在 (1, 1) 的人处于围栏内。
|
||||
注意围栏是可以不包含任何面积的,上图中第一和第二个围栏都是合法的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 50</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>0 <= points[i][0], points[i][1] <= 50</code></li>
|
||||
<li><code>points[i]</code> 点对两两不同。</li>
|
||||
</ul>
|
@@ -0,0 +1,64 @@
|
||||
<p>给你一个 <code>n x 2</code> 的二维数组 <code>points</code> ,它表示二维平面上的一些点坐标,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 。</p>
|
||||
|
||||
<p>我们定义 x 轴的正方向为 <strong>右</strong> (<strong>x 轴递增的方向</strong>),x 轴的负方向为 <strong>左</strong> (<strong>x 轴递减的方向</strong>)。类似的,我们定义 y 轴的正方向为 <strong>上</strong> (<strong>y 轴递增的方向</strong>),y 轴的负方向为 <strong>下</strong> (<strong>y 轴递减的方向</strong>)。</p>
|
||||
|
||||
<p>你需要安排这 <code>n</code> 个人的站位,这 <code>n</code> 个人中包括 liupengsay 和小羊肖恩 。你需要确保每个点处 <strong>恰好</strong> 有 <strong>一个</strong> 人。同时,liupengsay 想跟小羊肖恩单独玩耍,所以 liupengsay 会以 liupengsay<b> </b>的坐标为 <strong>左上角</strong> ,小羊肖恩的坐标为 <strong>右下角</strong> 建立一个矩形的围栏(<strong>注意</strong>,围栏可能 <strong>不</strong> 包含任何区域,也就是说围栏可能是一条线段)。如果围栏的 <strong>内部</strong> 或者 <strong>边缘</strong> 上有任何其他人,liupengsay 都会难过。</p>
|
||||
|
||||
<p>请你在确保 liupengsay <strong>不会</strong> 难过的前提下,返回 liupengsay 和小羊肖恩可以选择的 <strong>点对</strong> 数目。</p>
|
||||
|
||||
<p><b>注意</b>,liupengsay 建立的围栏必须确保 liupengsay 的位置是矩形的左上角,小羊肖恩的位置是矩形的右下角。比方说,以 <code>(1, 1)</code> ,<code>(1, 3)</code> ,<code>(3, 1)</code> 和 <code>(3, 3)</code> 为矩形的四个角,给定下图的两个输入,liupengsay 都不能建立围栏,原因如下:</p>
|
||||
|
||||
<ul>
|
||||
<li>图一中,liupengsay 在 <code>(3, 3)</code> 且小羊肖恩在 <code>(1, 1)</code> ,liupengsay 的位置不是左上角且小羊肖恩的位置不是右下角。</li>
|
||||
<li>图二中,liupengsay 在 <code>(1, 3)</code> 且小羊肖恩在 <code>(1, 1)</code> ,小羊肖恩的位置不是在围栏的右下角。</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png" style="width: 750px; height: 308px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 376px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[1,1],[2,2],[3,3]]
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>没有办法可以让 liupengsay 的围栏以 liupengsay 的位置为左上角且小羊肖恩的位置为右下角。所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<p><strong class="example"><a href="https://pic.leetcode.cn/1706880313-YelabI-example2.jpeg"><img alt="" src="https://pic.leetcode.cn/1706880313-YelabI-example2.jpeg" style="width: 900px; height: 250px;" /></a></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[6,2],[4,4],[2,6]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>总共有 2 种方案安排 liupengsay 和小羊肖恩的位置,使得 liupengsay 不会难过:
|
||||
- liupengsay 站在 (4, 4) ,小羊肖恩站在 (6, 2) 。
|
||||
- liupengsay 站在 (2, 6) ,小羊肖恩站在 (4, 4) 。
|
||||
不能安排 liupengsay 站在 (2, 6) 且小羊肖恩站在 (6, 2) ,因为站在 (4, 4) 的人处于围栏内。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<p><strong class="example"><a href="https://pic.leetcode.cn/1706880311-mtPGYC-example3.jpeg"><img alt="" src="https://pic.leetcode.cn/1706880311-mtPGYC-example3.jpeg" style="width: 911px; height: 250px;" /></a></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[3,1],[1,3],[1,1]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>总共有 2 种方案安排 liupengsay 和小羊肖恩的位置,使得 liupengsay 不会难过:
|
||||
- liupengsay 站在 (1, 1) ,小羊肖恩站在 (3, 1) 。
|
||||
- liupengsay 站在 (1, 3) ,小羊肖恩站在 (1, 1) 。
|
||||
不能安排 liupengsay 站在 (1, 3) 且小羊肖恩站在 (3, 1) ,因为站在 (1, 1) 的人处于围栏内。
|
||||
注意围栏是可以不包含任何面积的,上图中第一和第二个围栏都是合法的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>-10<sup>9</sup> <= points[i][0], points[i][1] <= 10<sup>9</sup></code></li>
|
||||
<li><code>points[i]</code> 点对两两不同。</li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>在每一秒,你必须执行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>移除 <code>word</code> 的前 <code>k</code> 个字符。</li>
|
||||
<li>在 <code>word</code> 的末尾添加 <code>k</code> 个任意字符。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意 </strong>添加的字符不必和移除的字符相同。但是,必须在每一秒钟都执行 <strong>两种 </strong>操作。</p>
|
||||
|
||||
<p>返回将 <code>word</code> 恢复到其 <strong>初始 </strong>状态所需的 <strong>最短 </strong>时间(该时间必须大于零)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abacaba", k = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
第 1 秒,移除 word 的前缀 "aba",并在末尾添加 "bac" 。因此,word 变为 "cababac"。
|
||||
第 2 秒,移除 word 的前缀 "cab",并在末尾添加 "aba" 。因此,word 变为 "abacaba" 并恢复到始状态。
|
||||
可以证明,2 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abacaba", k = 4
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:
|
||||
</strong>第 1 秒,移除 word 的前缀 "abac",并在末尾添加 "caba" 。因此,word 变为 "abacaba" 并恢复到初始状态。
|
||||
可以证明,1 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abcbabcd", k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
每一秒,我们都移除 word 的前 2 个字符,并在 word 末尾添加相同的字符。
|
||||
4 秒后,word 变为 "abcbabcd" 并恢复到初始状态。
|
||||
可以证明,4 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 50</code></li>
|
||||
<li><code>1 <= k <= word.length</code></li>
|
||||
<li><code>word</code>仅由小写英文字母组成。</li>
|
||||
</ul>
|
@@ -0,0 +1,56 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>在每一秒,你必须执行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>移除 <code>word</code> 的前 <code>k</code> 个字符。</li>
|
||||
<li>在 <code>word</code> 的末尾添加 <code>k</code> 个任意字符。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意 </strong>添加的字符不必和移除的字符相同。但是,必须在每一秒钟都执行 <strong>两种 </strong>操作。</p>
|
||||
|
||||
<p>返回将 <code>word</code> 恢复到其 <strong>初始 </strong>状态所需的 <strong>最短 </strong>时间(该时间必须大于零)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abacaba", k = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
第 1 秒,移除 word 的前缀 "aba",并在末尾添加 "bac" 。因此,word 变为 "cababac"。
|
||||
第 2 秒,移除 word 的前缀 "cab",并在末尾添加 "aba" 。因此,word 变为 "abacaba" 并恢复到始状态。
|
||||
可以证明,2 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abacaba", k = 4
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:
|
||||
</strong>第 1 秒,移除 word 的前缀 "abac",并在末尾添加 "caba" 。因此,word 变为 "abacaba" 并恢复到初始状态。
|
||||
可以证明,1 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "abcbabcd", k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
每一秒,我们都移除 word 的前 2 个字符,并在 word 末尾添加相同的字符。
|
||||
4 秒后,word 变为 "abcbabcd" 并恢复到初始状态。
|
||||
可以证明,4 秒是 word 恢复到其初始状态所需的最短时间。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= word.length</code></li>
|
||||
<li><code>word</code>仅由小写英文字母组成。</li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始、大小为 <code>m x n</code> 的网格 <code>image</code> ,表示一个灰度图像,其中 <code>image[i][j]</code> 表示在范围 <code>[0..255]</code> 内的某个像素强度。另给你一个<strong> 非负 </strong>整数 <code>threshold</code> 。</p>
|
||||
|
||||
<p>如果 <code>image[a][b]</code> 和 <code>image[c][d]</code> 满足 <code>|a - c| + |b - d| == 1</code> ,则称这两个像素是<strong> 相邻像素</strong> 。</p>
|
||||
|
||||
<p><strong>区域 </strong>是一个 <code>3 x 3</code> 的子网格,且满足区域中任意两个 <strong>相邻</strong> 像素之间,像素强度的<strong> 绝对差 </strong><strong> 小于或等于 </strong><code>threshold</code> 。</p>
|
||||
|
||||
<p><strong>区域</strong> 内的所有像素都认为属于该区域,而一个像素 <strong>可以 </strong>属于 <strong>多个</strong> 区域。</p>
|
||||
|
||||
<p>你需要计算一个下标从 <strong>0</strong> 开始、大小为 <code>m x n</code> 的网格 <code>result</code> ,其中 <code>result[i][j]</code> 是 <code>image[i][j]</code> 所属区域的 <strong>平均 </strong>强度,<strong>向下取整 </strong>到最接近的整数。如果 <code>image[i][j]</code> 属于多个区域,<code>result[i][j]</code> 是这些区域的<strong> </strong><strong>“取整后的平均强度”</strong> 的<strong> 平均值</strong>,也 <strong>向下取整 </strong>到最接近的整数。如果 <code>image[i][j]</code> 不属于任何区域,则 <code>result[i][j]</code><strong> 等于 </strong><code>image[i][j]</code> 。</p>
|
||||
|
||||
<p>返回网格 <code>result</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/example0corrected.png" style="width: 832px; height: 275px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>image = [[5,6,7,10],[8,9,10,10],[11,12,13,10]], threshold = 3
|
||||
<strong>输出:</strong>[[9,9,9,9],[9,9,9,9],[9,9,9,9]]
|
||||
<strong>解释:</strong>图像中存在两个区域,如图片中的阴影区域所示。第一个区域的平均强度为 9 ,而第二个区域的平均强度为 9.67 ,向下取整为 9 。两个区域的平均强度为 (9 + 9) / 2 = 9 。由于所有像素都属于区域 1 、区域 2 或两者,因此 result 中每个像素的强度都为 9 。
|
||||
注意,在计算多个区域的平均值时使用了向下取整的值,因此使用区域 2 的平均强度 9 来进行计算,而不是 9.67 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/12/21/example1corrected.png" style="width: 805px; height: 377px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>image = [[10,20,30],[15,25,35],[20,30,40],[25,35,45]], threshold = 12
|
||||
<strong>输出:</strong>[[25,25,25],[27,27,27],[27,27,27],[30,30,30]]
|
||||
<strong>解释:</strong>图像中存在两个区域,如图片中的阴影区域所示。第一个区域的平均强度为 25 ,而第二个区域的平均强度为 30 。两个区域的平均强度为 (25 + 30) / 2 = 27.5 ,向下取整为 27 。图像中第 0 行的所有像素属于区域 1 ,因此 result 中第 0 行的所有像素为 25 。同理,result 中第 3 行的所有像素为 30 。图像中第 1 行和第 2 行的像素属于区域 1 和区域 2 ,因此它们在 result 中的值为 27 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>image = [[5,6,7],[8,9,10],[11,12,13]], threshold = 1
|
||||
<strong>输出:</strong>[[5,6,7],[8,9,10],[11,12,13]]
|
||||
<strong>解释:</strong>图像中不存在任何区域,因此对于所有像素,result[i][j] == image[i][j] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= n, m <= 500</code></li>
|
||||
<li><code>0 <= image[i][j] <= 255</code></li>
|
||||
<li><code>0 <= threshold <= 255</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个长度为 <code>n</code> 的数组 <code>nums</code> 和一个 <strong>正</strong> 整数 <code>k</code> 。</p>
|
||||
|
||||
<p>如果 <code>nums</code> 的一个子数组中,第一个元素和最后一个元素 <strong>差的绝对值恰好</strong> 为 <code>k</code> ,我们称这个子数组为 <strong>好</strong> 的。换句话说,如果子数组 <code>nums[i..j]</code> 满足 <code>|nums[i] - nums[j]| == k</code> ,那么它是一个好子数组。</p>
|
||||
|
||||
<p>请你返回 <code>nums</code> 中 <strong>好</strong> 子数组的 <strong>最大</strong> 和,如果没有好子数组,返回<em> </em><code>0</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,4,5,6], k = 1
|
||||
<b>输出:</b>11
|
||||
<b>解释:</b>好子数组中第一个元素和最后一个元素的差的绝对值必须为 1 。好子数组有 [1,2] ,[2,3] ,[3,4] ,[4,5] 和 [5,6] 。最大子数组和为 11 ,对应的子数组为 [5,6] 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [-1,3,2,4,5], k = 3
|
||||
<b>输出:</b>11
|
||||
<b>解释:</b>好子数组中第一个元素和最后一个元素的差的绝对值必须为 3 。好子数组有 [-1,3,2] 和 [2,4,5] 。最大子数组和为 11 ,对应的子数组为 [2,4,5] 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [-1,-2,-3,-4], k = 2
|
||||
<b>输出:</b>-6
|
||||
<b>解释:</b>好子数组中第一个元素和最后一个元素的差的绝对值必须为 2 。好子数组有 [-1,-2,-3] 和 [-2,-3,-4] 。最大子数组和为 -6 ,对应的子数组为 [-1,-2,-3] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>边界上有一只蚂蚁,它有时向 <strong>左 </strong>走,有时向 <strong>右 </strong>走。</p>
|
||||
|
||||
<p>给你一个 <strong>非零</strong> 整数数组 <code>nums</code> 。蚂蚁会按顺序读取 <code>nums</code> 中的元素,从第一个元素开始直到结束。每一步,蚂蚁会根据当前元素的值移动:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>nums[i] < 0</code> ,向 <strong>左</strong> 移动<!-- notionvc: 55fee232-4fc9-445f-952a-f1b979415864 --> <code>-nums[i]</code>单位。</li>
|
||||
<li>如果 <code>nums[i] > 0</code> ,向 <strong>右</strong> 移动 <code>nums[i]</code>单位。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回蚂蚁 <strong>返回 </strong>到边界上的次数。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>边界两侧有无限的空间。</li>
|
||||
<li>只有在蚂蚁移动了 <code>|nums[i]|</code> 单位后才检查它是否位于边界上。换句话说,如果蚂蚁只是在移动过程中穿过了边界,则不会计算在内。<!-- notionvc: 5ff95338-8634-4d02-a085-1e83c0be6fcd --></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,3,-5]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>第 1 步后,蚂蚁距边界右侧 2 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 2 步后,蚂蚁距边界右侧 5 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 3 步后,蚂蚁位于边界上。
|
||||
所以答案是 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,2,-3,-4]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>第 1 步后,蚂蚁距边界右侧 3 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 2 步后,蚂蚁距边界右侧 5 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 3 步后,蚂蚁距边界右侧 2 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
第 4 步后,蚂蚁距边界左侧 2 单位远<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->。
|
||||
蚂蚁从未返回到边界上,所以答案是 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>-10 <= nums[i] <= 10</code></li>
|
||||
<li><code>nums[i] != 0</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user