mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 03:11:42 +08:00
add leetcode problem-cn part5
This commit is contained in:
36
算法题(国内版)/problem (Chinese)/01 矩阵 [01-matrix].html
Normal file
36
算法题(国内版)/problem (Chinese)/01 矩阵 [01-matrix].html
Normal file
@@ -0,0 +1,36 @@
|
||||
<p>给定一个由 <code>0</code> 和 <code>1</code> 组成的矩阵 <code>mat</code> ,请输出一个大小相同的矩阵,其中每一个格子是 <code>mat</code> 中对应位置元素到最近的 <code>0</code> 的距离。</p>
|
||||
|
||||
<p>两个相邻元素间的距离为 <code>1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>示例 1:</b></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode-cn.com/1626667201-NCWmuP-image.png" style="width: 150px; " /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>mat =<strong> </strong>[[0,0,0],[0,1,0],[0,0,0]]
|
||||
<strong>输出:</strong>[[0,0,0],[0,1,0],[0,0,0]]
|
||||
</pre>
|
||||
|
||||
<p><b>示例 2:</b></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode-cn.com/1626667205-xFxIeK-image.png" style="width: 150px; " /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>mat =<b> </b>[[0,0,0],[0,1,0],[1,1,1]]
|
||||
<strong>输出:</strong>[[0,0,0],[0,1,0],[1,2,1]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>4</sup></code></li>
|
||||
<li><code>mat[i][j] is either 0 or 1.</code></li>
|
||||
<li><code>mat</code> 中至少有一个 <code>0 </code></li>
|
||||
</ul>
|
39
算法题(国内版)/problem (Chinese)/132 模式 [132-pattern].html
Normal file
39
算法题(国内版)/problem (Chinese)/132 模式 [132-pattern].html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,数组中共有 <code>n</code> 个整数。<strong>132 模式的子序列</strong> 由三个整数 <code>nums[i]</code>、<code>nums[j]</code> 和 <code>nums[k]</code> 组成,并同时满足:<code>i < j < k</code> 和 <code>nums[i] < nums[k] < nums[j]</code> 。</p>
|
||||
|
||||
<p>如果 <code>nums</code> 中存在 <strong>132 模式的子序列</strong> ,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,4]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>序列中不存在 132 模式的子序列。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,1,4,2]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>序列中有 1 个 132 模式的子序列: [1, 4, 2] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-1,3,2,0]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>序列中有 3 个 132 模式的的子序列:[-1, 3, 2]、[-1, 3, 0] 和 [-1, 2, 0] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 2 * 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
45
算法题(国内版)/problem (Chinese)/IPO [ipo].html
Normal file
45
算法题(国内版)/problem (Chinese)/IPO [ipo].html
Normal file
@@ -0,0 +1,45 @@
|
||||
<p>假设 力扣(LeetCode)即将开始 <strong>IPO</strong> 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 <code>k</code> 个不同的项目。帮助 力扣 设计完成最多 <code>k</code> 个不同项目后得到最大总资本的方式。</p>
|
||||
|
||||
<p>给你 <code>n</code> 个项目。对于每个项目 <code>i</code><strong> </strong>,它都有一个纯利润 <code>profits[i]</code> ,和启动该项目需要的最小资本 <code>capital[i]</code> 。</p>
|
||||
|
||||
<p>最初,你的资本为 <code>w</code> 。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。</p>
|
||||
|
||||
<p>总而言之,从给定项目中选择 <strong>最多</strong> <code>k</code> 个不同项目的列表,以 <strong>最大化最终资本</strong> ,并输出最终可获得的最多资本。</p>
|
||||
|
||||
<p>答案保证在 32 位有符号整数范围内。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:
|
||||
</strong>由于你的初始资本为 0,你仅可以从 0 号项目开始。
|
||||
在完成后,你将获得 1 的利润,你的总资本将变为 1。
|
||||
此时你可以选择开始 1 号或 2 号项目。
|
||||
由于你最多可以选择两个项目,所以你需要完成 2 号项目以获得最大的资本。
|
||||
因此,输出最后最大化的资本,为 0 + 1 + 3 = 4。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
|
||||
<strong>输出:</strong>6
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= w <= 10<sup>9</sup></code></li>
|
||||
<li><code>n == profits.length</code></li>
|
||||
<li><code>n == capital.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= profits[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= capital[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
58
算法题(国内版)/problem (Chinese)/LFU 缓存 [lfu-cache].html
Normal file
58
算法题(国内版)/problem (Chinese)/LFU 缓存 [lfu-cache].html
Normal file
@@ -0,0 +1,58 @@
|
||||
<p>请你为 <a href="https://baike.baidu.com/item/%E7%BC%93%E5%AD%98%E7%AE%97%E6%B3%95">最不经常使用(LFU)</a>缓存算法设计并实现数据结构。</p>
|
||||
|
||||
<p>实现 <code>LFUCache</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>LFUCache(int capacity)</code> - 用数据结构的容量 <code>capacity</code> 初始化对象</li>
|
||||
<li><code>int get(int key)</code> - 如果键 <code>key</code> 存在于缓存中,则获取键的值,否则返回 <code>-1</code> 。</li>
|
||||
<li><code>void put(int key, int value)</code> - 如果键 <code>key</code> 已存在,则变更其值;如果键不存在,请插入键值对。当缓存达到其容量 <code>capacity</code> 时,则应该在插入新项之前,移除最不经常使用的项。在此问题中,当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 <strong>最近最久未使用</strong> 的键。</li>
|
||||
</ul>
|
||||
|
||||
<p>为了确定最不常使用的键,可以为缓存中的每个键维护一个 <strong>使用计数器</strong> 。使用计数最小的键是最久未使用的键。</p>
|
||||
|
||||
<p>当一个键首次插入到缓存中时,它的使用计数器被设置为 <code>1</code> (由于 put 操作)。对缓存中的键执行 <code>get</code> 或 <code>put</code> 操作,使用计数器的值将会递增。</p>
|
||||
|
||||
<p>函数 <code>get</code> 和 <code>put</code> 必须以 <code>O(1)</code> 的平均时间复杂度运行。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"]
|
||||
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
|
||||
<strong>输出:</strong>
|
||||
[null, null, null, 1, null, -1, 3, null, -1, 3, 4]
|
||||
|
||||
<strong>解释:</strong>
|
||||
// cnt(x) = 键 x 的使用计数
|
||||
// cache=[] 将显示最后一次使用的顺序(最左边的元素是最近的)
|
||||
LFUCache lfu = new LFUCache(2);
|
||||
lfu.put(1, 1); // cache=[1,_], cnt(1)=1
|
||||
lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
|
||||
lfu.get(1); // 返回 1
|
||||
// cache=[1,2], cnt(2)=1, cnt(1)=2
|
||||
lfu.put(3, 3); // 去除键 2 ,因为 cnt(2)=1 ,使用计数最小
|
||||
// cache=[3,1], cnt(3)=1, cnt(1)=2
|
||||
lfu.get(2); // 返回 -1(未找到)
|
||||
lfu.get(3); // 返回 3
|
||||
// cache=[3,1], cnt(3)=2, cnt(1)=2
|
||||
lfu.put(4, 4); // 去除键 1 ,1 和 3 的 cnt 相同,但 1 最久未使用
|
||||
// cache=[4,3], cnt(4)=1, cnt(3)=2
|
||||
lfu.get(1); // 返回 -1(未找到)
|
||||
lfu.get(3); // 返回 3
|
||||
// cache=[3,4], cnt(4)=1, cnt(3)=3
|
||||
lfu.get(4); // 返回 4
|
||||
// cache=[3,4], cnt(4)=2, cnt(3)=3</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= capacity <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= key <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= value <= 10<sup>9</sup></code></li>
|
||||
<li>最多调用 <code>2 * 10<sup>5</sup></code> 次 <code>get</code> 和 <code>put</code> 方法</li>
|
||||
</ul>
|
@@ -0,0 +1,3 @@
|
||||
<p>TinyURL是一种URL简化服务, 比如:当你输入一个URL <code>https://leetcode.com/problems/design-tinyurl</code> 时,它将返回一个简化的URL <code>http://tinyurl.com/4e9iAk</code>.</p>
|
||||
|
||||
<p>要求:设计一个 TinyURL 的加密 <code>encode</code> 和解密 <code>decode</code> 的方法。你的加密和解密算法如何设计和运作是没有限制的,你只需要保证一个URL可以被加密成一个TinyURL,并且这个TinyURL可以用解密方法恢复成原本的URL。</p>
|
37
算法题(国内版)/problem (Chinese)/一和零 [ones-and-zeroes].html
Normal file
37
算法题(国内版)/problem (Chinese)/一和零 [ones-and-zeroes].html
Normal file
@@ -0,0 +1,37 @@
|
||||
<p>给你一个二进制字符串数组 <code>strs</code> 和两个整数 <code>m</code> 和 <code>n</code> 。</p>
|
||||
|
||||
<div class="MachineTrans-Lines">
|
||||
<p class="MachineTrans-lang-zh-CN">请你找出并返回 <code>strs</code> 的最大子集的长度,该子集中 <strong>最多</strong> 有 <code>m</code> 个 <code>0</code> 和 <code>n</code> 个 <code>1</code> 。</p>
|
||||
|
||||
<p class="MachineTrans-lang-zh-CN">如果 <code>x</code> 的所有元素也是 <code>y</code> 的元素,集合 <code>x</code> 是集合 <code>y</code> 的 <strong>子集</strong> 。</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["10", "0001", "111001", "1", "0"], m = 5, n = 3
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>最多有 5 个 0 和 3 个 1 的最大子集是 {"10","0001","1","0"} ,因此答案是 4 。
|
||||
其他满足题意但较小的子集包括 {"0001","1"} 和 {"10","1","0"} 。{"111001"} 不满足题意,因为它含 4 个 1 ,大于 n 的值 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>strs = ["10", "0", "1"], m = 1, n = 1
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>最大的子集是 {"0", "1"} ,所以答案是 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= strs.length <= 600</code></li>
|
||||
<li><code>1 <= strs[i].length <= 100</code></li>
|
||||
<li><code>strs[i]</code> 仅由 <code>'0'</code> 和 <code>'1'</code> 组成</li>
|
||||
<li><code>1 <= m, n <= 100</code></li>
|
||||
</ul>
|
25
算法题(国内版)/problem (Chinese)/七进制数 [base-7].html
Normal file
25
算法题(国内版)/problem (Chinese)/七进制数 [base-7].html
Normal file
@@ -0,0 +1,25 @@
|
||||
<p>给定一个整数 <code>num</code>,将其转化为 <strong>7 进制</strong>,并以字符串形式输出。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> num = 100
|
||||
<strong>输出:</strong> "202"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> num = -7
|
||||
<strong>输出:</strong> "-10"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-10<sup>7</sup> <= num <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p><code>nums1</code> 中数字 <code>x</code> 的 <strong>下一个更大元素</strong> 是指 <code>x</code> 在 <code>nums2</code> 中对应位置 <strong>右侧</strong> 的 <strong>第一个</strong> 比 <code>x</code><strong> </strong>大的元素。</p>
|
||||
|
||||
<p>给你两个<strong> 没有重复元素</strong> 的数组 <code>nums1</code> 和 <code>nums2</code> ,下标从 <strong>0</strong> 开始计数,其中<code>nums1</code> 是 <code>nums2</code> 的子集。</p>
|
||||
|
||||
<p>对于每个 <code>0 <= i < nums1.length</code> ,找出满足 <code>nums1[i] == nums2[j]</code> 的下标 <code>j</code> ,并且在 <code>nums2</code> 确定 <code>nums2[j]</code> 的 <strong>下一个更大元素</strong> 。如果不存在下一个更大元素,那么本次查询的答案是 <code>-1</code> 。</p>
|
||||
|
||||
<p>返回一个长度为 <code>nums1.length</code> 的数组<em> </em><code>ans</code><em> </em>作为答案,满足<em> </em><code>ans[i]</code><em> </em>是如上所述的 <strong>下一个更大元素</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [4,1,2], nums2 = [1,3,4,2].
|
||||
<strong>输出:</strong>[-1,3,-1]
|
||||
<strong>解释:</strong>nums1 中每个值的下一个更大元素如下所述:
|
||||
- 4 ,用加粗斜体标识,nums2 = [1,3,<strong>4</strong>,2]。不存在下一个更大元素,所以答案是 -1 。
|
||||
- 1 ,用加粗斜体标识,nums2 = [<em><strong>1</strong></em>,3,4,2]。下一个更大元素是 3 。
|
||||
- 2 ,用加粗斜体标识,nums2 = [1,3,4,<em><strong>2</strong></em>]。不存在下一个更大元素,所以答案是 -1 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [2,4], nums2 = [1,2,3,4].
|
||||
<strong>输出:</strong>[3,-1]
|
||||
<strong>解释:</strong>nums1 中每个值的下一个更大元素如下所述:
|
||||
- 2 ,用加粗斜体标识,nums2 = [1,<em><strong>2</strong></em>,3,4]。下一个更大元素是 3 。
|
||||
- 4 ,用加粗斜体标识,nums2 = [1,2,3,<em><strong>4</strong></em>]。不存在下一个更大元素,所以答案是 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length <= nums2.length <= 1000</code></li>
|
||||
<li><code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>nums1</code>和<code>nums2</code>中所有整数 <strong>互不相同</strong></li>
|
||||
<li><code>nums1</code> 中的所有整数同样出现在 <code>nums2</code> 中</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你可以设计一个时间复杂度为 <code>O(nums1.length + nums2.length)</code> 的解决方案吗?</p>
|
@@ -0,0 +1,31 @@
|
||||
<p>给定一个循环数组 <code>nums</code> ( <code>nums[nums.length - 1]</code> 的下一个元素是 <code>nums[0]</code> ),返回 <em><code>nums</code> 中每个元素的 <strong>下一个更大元素</strong></em> 。</p>
|
||||
|
||||
<p>数字 <code>x</code> 的 <strong>下一个更大的元素</strong> 是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [1,2,1]
|
||||
<strong>输出:</strong> [2,-1,2]
|
||||
<strong>解释:</strong> 第一个 1 的下一个更大的数是 2;
|
||||
数字 2 找不到下一个更大的数;
|
||||
第二个 1 的下一个最大的数需要循环搜索,结果也是 2。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [1,2,3,4,3]
|
||||
<strong>输出:</strong> [2,3,4,-1,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
42
算法题(国内版)/problem (Chinese)/两数相加 II [add-two-numbers-ii].html
Normal file
42
算法题(国内版)/problem (Chinese)/两数相加 II [add-two-numbers-ii].html
Normal file
@@ -0,0 +1,42 @@
|
||||
<p>给你两个 <strong>非空 </strong>链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。</p>
|
||||
|
||||
<p>你可以假设除了数字 0 之外,这两个数字都不会以零开头。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode-cn.com/1626420025-fZfzMX-image.png" style="width: 302px; " /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>l1 = [7,2,4,3], l2 = [5,6,4]
|
||||
<strong>输出:</strong>[7,8,0,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>l1 = [2,4,3], l2 = [5,6,4]
|
||||
<strong>输出:</strong>[8,0,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>l1 = [0], l2 = [0]
|
||||
<strong>输出:</strong>[0]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>链表的长度范围为<code> [1, 100]</code></li>
|
||||
<li><code>0 <= node.val <= 9</code></li>
|
||||
<li>输入数据保证链表代表的数字无前导 0</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>如果输入链表不能翻转该如何解决?</p>
|
@@ -0,0 +1,40 @@
|
||||
<p>给你一个含重复值的二叉搜索树(BST)的根节点 <code>root</code> ,找出并返回 BST 中的所有 <a href="https://baike.baidu.com/item/%E4%BC%97%E6%95%B0/44796" target="_blank">众数</a>(即,出现频率最高的元素)。</p>
|
||||
|
||||
<p>如果树中有不止一个众数,可以按 <strong>任意顺序</strong> 返回。</p>
|
||||
|
||||
<p>假定 BST 满足如下定义:</p>
|
||||
|
||||
<ul>
|
||||
<li>结点左子树中所含节点的值 <strong>小于等于</strong> 当前节点的值</li>
|
||||
<li>结点右子树中所含节点的值 <strong>大于等于</strong> 当前节点的值</li>
|
||||
<li>左子树和右子树都是二叉搜索树</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/11/mode-tree.jpg" style="width: 142px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,null,2,2]
|
||||
<strong>输出:</strong>[2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [0]
|
||||
<strong>输出:</strong>[0]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点的数目在范围 <code>[1, 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)</p>
|
@@ -0,0 +1,32 @@
|
||||
<p>给你一个二叉搜索树的根节点 <code>root</code> ,返回 <strong>树中任意两不同节点值之间的最小差值</strong> 。</p>
|
||||
|
||||
<p>差值是一个正数,其数值等于两值之差的绝对值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [4,2,6,1,3]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,0,48,null,null,12,49]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点的数目范围是 <code>[2, 10<sup>4</sup>]</code></li>
|
||||
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>本题与 783 <a href="https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/">https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/</a> 相同</p>
|
@@ -0,0 +1,19 @@
|
||||
<p>给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 :</strong><br>
|
||||
给定二叉树</p>
|
||||
|
||||
<pre> 1
|
||||
/ \
|
||||
2 3
|
||||
/ \
|
||||
4 5
|
||||
</pre>
|
||||
|
||||
<p>返回 <strong>3</strong>, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>两结点之间的路径长度是以它们之间边的数目表示。</p>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你链表的头节点 <code>head</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p><strong>交换</strong> 链表正数第 <code>k</code> 个节点和倒数第 <code>k</code> 个节点的值后,返回链表的头节点(链表 <strong>从 1 开始索引</strong>)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/01/10/linked1.jpg" style="width: 722px; height: 202px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,2,3,4,5], k = 2
|
||||
<strong>输出:</strong>[1,4,3,2,5]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [7,9,6,6,7,8,3,0,9,5], k = 5
|
||||
<strong>输出:</strong>[7,9,6,6,8,7,3,0,9,5]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1], k = 1
|
||||
<strong>输出:</strong>[1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,2], k = 1
|
||||
<strong>输出:</strong>[2,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [1,2,3], k = 2
|
||||
<strong>输出:</strong>[1,2,3]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>链表中节点的数目是 <code>n</code></li>
|
||||
<li><code>1 <= k <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= Node.val <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>给你一个字符串 <code>s</code> ,其中包含字母顺序打乱的用英文单词表示的若干数字(<code>0-9</code>)。按 <strong>升序</strong> 返回原始的数字。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "owoztneoer"
|
||||
<strong>输出:</strong>"012"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "fviefuro"
|
||||
<strong>输出:</strong>"45"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> 为 <code>["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]</code> 这些字符之一</li>
|
||||
<li><code>s</code> 保证是一个符合题目要求的字符串</li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>假设有从 1 到 n 的 n 个整数。用这些整数构造一个数组 <code>perm</code>(<strong>下标从 1 开始</strong>),只要满足下述条件 <strong>之一</strong> ,该数组就是一个 <strong>优美的排列</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>perm[i]</code> 能够被 <code>i</code> 整除</li>
|
||||
<li><code>i</code> 能够被 <code>perm[i]</code> 整除</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个整数 <code>n</code> ,返回可以构造的 <strong>优美排列 </strong>的 <strong>数量</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2
|
||||
<strong>输出:</strong>2
|
||||
<b>解释:</b>
|
||||
第 1 个优美的排列是 [1,2]:
|
||||
- perm[1] = 1 能被 i = 1 整除
|
||||
- perm[2] = 2 能被 i = 2 整除
|
||||
第 2 个优美的排列是 [2,1]:
|
||||
- perm[1] = 2 能被 i = 1 整除
|
||||
- i = 2 能被 perm[2] = 1 整除
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 15</code></li>
|
||||
</ul>
|
41
算法题(国内版)/problem (Chinese)/供暖器 [heaters].html
Normal file
41
算法题(国内版)/problem (Chinese)/供暖器 [heaters].html
Normal file
@@ -0,0 +1,41 @@
|
||||
<p>冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。</p>
|
||||
|
||||
<p>在加热器的加热半径范围内的每个房屋都可以获得供暖。</p>
|
||||
|
||||
<p>现在,给出位于一条水平线上的房屋 <code>houses</code> 和供暖器 <code>heaters</code> 的位置,请你找出并返回可以覆盖所有房屋的最小加热半径。</p>
|
||||
|
||||
<p><strong>说明</strong>:所有供暖器都遵循你的半径标准,加热的半径也一样。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> houses = [1,2,3], heaters = [2]
|
||||
<strong>输出:</strong> 1
|
||||
<strong>解释:</strong> 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> houses = [1,2,3,4], heaters = [1,4]
|
||||
<strong>输出:</strong> 1
|
||||
<strong>解释:</strong> 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>houses = [1,5], heaters = [2]
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= houses.length, heaters.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= houses[i], heaters[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>请你设计一个用于存储字符串计数的数据结构,并能够返回计数最小和最大的字符串。</p>
|
||||
|
||||
<p>实现 <code>AllOne</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>AllOne()</code> 初始化数据结构的对象。</li>
|
||||
<li><code>inc(String key)</code> 字符串 <code>key</code> 的计数增加 <code>1</code> 。如果数据结构中尚不存在 <code>key</code> ,那么插入计数为 <code>1</code> 的 <code>key</code> 。</li>
|
||||
<li><code>dec(String key)</code> 字符串 <code>key</code> 的计数减少 <code>1</code> 。如果 <code>key</code> 的计数在减少后为 <code>0</code> ,那么需要将这个 <code>key</code> 从数据结构中删除。测试用例保证:在减少计数前,<code>key</code> 存在于数据结构中。</li>
|
||||
<li><code>getMaxKey()</code> 返回任意一个计数最大的字符串。如果没有元素存在,返回一个空字符串 <code>""</code> 。</li>
|
||||
<li><code>getMinKey()</code> 返回任意一个计数最小的字符串。如果没有元素存在,返回一个空字符串 <code>""</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>
|
||||
["AllOne", "inc", "inc", "getMaxKey", "getMinKey", "inc", "getMaxKey", "getMinKey"]
|
||||
[[], ["hello"], ["hello"], [], [], ["leet"], [], []]
|
||||
<strong>输出</strong>
|
||||
[null, null, null, "hello", "hello", null, "hello", "leet"]
|
||||
|
||||
<strong>解释</strong>
|
||||
AllOne allOne = new AllOne();
|
||||
allOne.inc("hello");
|
||||
allOne.inc("hello");
|
||||
allOne.getMaxKey(); // 返回 "hello"
|
||||
allOne.getMinKey(); // 返回 "hello"
|
||||
allOne.inc("leet");
|
||||
allOne.getMaxKey(); // 返回 "hello"
|
||||
allOne.getMinKey(); // 返回 "leet"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= key.length <= 10</code></li>
|
||||
<li><code>key</code> 由小写英文字母组成</li>
|
||||
<li>测试用例保证:在每次调用 <code>dec</code> 时,数据结构中总存在 <code>key</code></li>
|
||||
<li>最多调用 <code>inc</code>、<code>dec</code>、<code>getMaxKey</code> 和 <code>getMinKey</code> 方法 <code>5 * 10<sup>4</sup></code> 次</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>给你一个二叉树的根结点 <code>root</code> ,请返回出现次数最多的子树元素和。如果有多个元素出现的次数相同,返回所有出现次数最多的子树元素和(不限顺序)。</p>
|
||||
|
||||
<p>一个结点的 <strong>「子树元素和」</strong> 定义为以该结点为根的二叉树上所有结点的元素之和(包括结点本身)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [5,2,-3]
|
||||
<strong>输出:</strong> [2,-3,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [5,2,-5]
|
||||
<b>输出:</b> [2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>节点数在 <code>[1, 10<sup>4</sup>]</code> 范围内</li>
|
||||
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>给你一个 <strong>只包含正整数 </strong>的 <strong>非空 </strong>数组 <code>nums</code> 。请你判断是否可以将这个数组分割成两个子集,使得两个子集的元素和相等。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,5,11,5]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>数组可以分割成 [1, 5, 5] 和 [11] 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3,5]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>数组不能分割成两个元素和相等的子集。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 200</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
36
算法题(国内版)/problem (Chinese)/分发饼干 [assign-cookies].html
Normal file
36
算法题(国内版)/problem (Chinese)/分发饼干 [assign-cookies].html
Normal file
@@ -0,0 +1,36 @@
|
||||
<p>假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。</p>
|
||||
|
||||
<p>对每个孩子 <code>i</code>,都有一个胃口值 <code>g[i]</code><sub>,</sub>这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 <code>j</code>,都有一个尺寸 <code>s[j]</code><sub> </sub>。如果 <code>s[j] >= g[i]</code>,我们可以将这个饼干 <code>j</code> 分配给孩子 <code>i</code> ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。</p>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> g = [1,2,3], s = [1,1]
|
||||
<strong>输出:</strong> 1
|
||||
<strong>解释:</strong>
|
||||
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
|
||||
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
|
||||
所以你应该输出1。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> g = [1,2], s = [1,2,3]
|
||||
<strong>输出:</strong> 2
|
||||
<strong>解释:</strong>
|
||||
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
|
||||
你拥有的饼干数量和尺寸都足以让所有孩子满足。
|
||||
所以你应该输出2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= g.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= g[i], s[j] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>给定一个二叉搜索树的根节点 <strong>root </strong>和一个值 <strong>key</strong>,删除二叉搜索树中的 <strong>key </strong>对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。</p>
|
||||
|
||||
<p>一般来说,删除节点可分为两个步骤:</p>
|
||||
|
||||
<ol>
|
||||
<li>首先找到需要删除的节点;</li>
|
||||
<li>如果找到了,删除它。</li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2020/09/04/del_node_1.jpg" style="width: 800px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [5,3,6,2,4,null,7], key = 3
|
||||
<strong>输出:</strong>[5,4,6,2,null,null,7]
|
||||
<strong>解释:</strong>给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。
|
||||
一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。
|
||||
另一个正确答案是 [5,2,6,null,4,null,7]。
|
||||
|
||||
<img src="https://assets.leetcode.com/uploads/2020/09/04/del_node_supp.jpg" style="width: 350px;" />
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [5,3,6,2,4,null,7], key = 0
|
||||
<strong>输出:</strong> [5,3,6,2,4,null,7]
|
||||
<strong>解释:</strong> 二叉树不包含值为 0 的节点
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [], key = 0
|
||||
<strong>输出:</strong> []</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>节点数的范围 <code>[0, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li>节点值唯一</li>
|
||||
<li><code>root</code> 是合法的二叉搜索树</li>
|
||||
<li><code>-10<sup>5</sup> <= key <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong> 要求算法时间复杂度为 O(h),h 为树的高度。</p>
|
49
算法题(国内版)/problem (Chinese)/压缩字符串 [string-compression].html
Normal file
49
算法题(国内版)/problem (Chinese)/压缩字符串 [string-compression].html
Normal file
@@ -0,0 +1,49 @@
|
||||
<p>给你一个字符数组 <code>chars</code> ,请使用下述算法压缩:</p>
|
||||
|
||||
<p>从一个空字符串 <code>s</code> 开始。对于 <code>chars</code> 中的每组 <strong>连续重复字符</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>如果这一组长度为 <code>1</code> ,则将字符追加到 <code>s</code> 中。</li>
|
||||
<li>否则,需要向 <code>s</code> 追加字符,后跟这一组的长度。</li>
|
||||
</ul>
|
||||
|
||||
<p>压缩后得到的字符串 <code>s</code> <strong>不应该直接返回</strong> ,需要转储到字符数组 <code>chars</code> 中。需要注意的是,如果组长度为 <code>10</code> 或 <code>10</code> 以上,则在 <code>chars</code> 数组中会被拆分为多个字符。</p>
|
||||
|
||||
<p>请在 <strong>修改完输入数组后</strong> ,返回该数组的新长度。</p>
|
||||
|
||||
<p>你必须设计并实现一个只使用常量额外空间的算法来解决此问题。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>chars = ["a","a","b","b","c","c","c"]
|
||||
<strong>输出:</strong>返回 6 ,输入数组的前 6 个字符应该是:["a","2","b","2","c","3"]
|
||||
<strong>解释:</strong>"aa" 被 "a2" 替代。"bb" 被 "b2" 替代。"ccc" 被 "c3" 替代。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>chars = ["a"]
|
||||
<strong>输出:</strong>返回 1 ,输入数组的前 1 个字符应该是:["a"]
|
||||
<strong>解释:</strong>唯一的组是“a”,它保持未压缩,因为它是一个字符。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
|
||||
<strong>输出:</strong>返回 4 ,输入数组的前 4 个字符应该是:["a","b","1","2"]。
|
||||
<strong>解释:</strong>由于字符 "a" 不重复,所以不会被压缩。"bbbbbbbbbbbb" 被 “b12” 替代。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= chars.length <= 2000</code></li>
|
||||
<li><code>chars[i]</code> 可以是小写英文字母、大写英文字母、数字或符号</li>
|
||||
</ul>
|
32
算法题(国内版)/problem (Chinese)/反转字符串 II [reverse-string-ii].html
Normal file
32
算法题(国内版)/problem (Chinese)/反转字符串 II [reverse-string-ii].html
Normal file
@@ -0,0 +1,32 @@
|
||||
<p>给定一个字符串 <code>s</code> 和一个整数 <code>k</code>,从字符串开头算起,每计数至 <code>2k</code> 个字符,就反转这 <code>2k</code> 字符中的前 <code>k</code> 个字符。</p>
|
||||
|
||||
<ul>
|
||||
<li>如果剩余字符少于 <code>k</code> 个,则将剩余字符全部反转。</li>
|
||||
<li>如果剩余字符小于 <code>2k</code> 但大于或等于 <code>k</code> 个,则反转前 <code>k</code> 个字符,其余字符保持原样。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcdefg", k = 2
|
||||
<strong>输出:</strong>"bacdfeg"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcd", k = 2
|
||||
<strong>输出:</strong>"bacd"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> 仅由小写英文组成</li>
|
||||
<li><code>1 <= k <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
45
算法题(国内版)/problem (Chinese)/可怜的小猪 [poor-pigs].html
Normal file
45
算法题(国内版)/problem (Chinese)/可怜的小猪 [poor-pigs].html
Normal file
@@ -0,0 +1,45 @@
|
||||
<p>有<code> buckets</code> 桶液体,其中 <strong>正好有一桶</strong> 含有毒药,其余装的都是水。它们从外观看起来都一样。为了弄清楚哪只水桶含有毒药,你可以喂一些猪喝,通过观察猪是否会死进行判断。不幸的是,你只有 <code>minutesToTest</code> 分钟时间来确定哪桶液体是有毒的。</p>
|
||||
|
||||
<p>喂猪的规则如下:</p>
|
||||
|
||||
<ol>
|
||||
<li>选择若干活猪进行喂养</li>
|
||||
<li>可以允许小猪同时饮用任意数量的桶中的水,并且该过程不需要时间。</li>
|
||||
<li>小猪喝完水后,必须有 <code>minutesToDie</code> 分钟的冷却时间。在这段时间里,你只能观察,而不允许继续喂猪。</li>
|
||||
<li>过了 <code>minutesToDie</code> 分钟后,所有喝到毒药的猪都会死去,其他所有猪都会活下来。</li>
|
||||
<li>重复这一过程,直到时间用完。</li>
|
||||
</ol>
|
||||
|
||||
<p>给你桶的数目 <code>buckets</code> ,<code>minutesToDie</code> 和 <code>minutesToTest</code> ,返回 <em>在规定时间内判断哪个桶有毒所需的 <strong>最小</strong> 猪数</em> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>buckets = 1000, minutesToDie = 15, minutesToTest = 60
|
||||
<strong>输出:</strong>5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>buckets = 4, minutesToDie = 15, minutesToTest = 15
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>buckets = 4, minutesToDie = 15, minutesToTest = 30
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= buckets <= 1000</code></li>
|
||||
<li><code>1 <= minutesToDie <= minutesToTest <= 100</code></li>
|
||||
</ul>
|
39
算法题(国内版)/problem (Chinese)/四数相加 II [4sum-ii].html
Normal file
39
算法题(国内版)/problem (Chinese)/四数相加 II [4sum-ii].html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>给你四个整数数组 <code>nums1</code>、<code>nums2</code>、<code>nums3</code> 和 <code>nums4</code> ,数组长度都是 <code>n</code> ,请你计算有多少个元组 <code>(i, j, k, l)</code> 能满足:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i, j, k, l < n</code></li>
|
||||
<li><code>nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
两个元组如下:
|
||||
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
|
||||
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p> <strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length</code></li>
|
||||
<li><code>n == nums2.length</code></li>
|
||||
<li><code>n == nums3.length</code></li>
|
||||
<li><code>n == nums4.length</code></li>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>-2<sup>28</sup> <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2<sup>28</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给定平面上<em> </em><code>n</code><em> </em>对 <strong>互不相同</strong> 的点 <code>points</code> ,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 。<strong>回旋镖</strong> 是由点 <code>(i, j, k)</code> 表示的元组 ,其中 <code>i</code> 和 <code>j</code> 之间的距离和 <code>i</code> 和 <code>k</code> 之间的欧式距离相等(<strong>需要考虑元组的顺序</strong>)。</p>
|
||||
|
||||
<p>返回平面上所有回旋镖的数量。</p>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[0,0],[1,0],[2,0]]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>两个回旋镖为 <strong>[[1,0],[0,0],[2,0]]</strong> 和 <strong>[[1,0],[2,0],[0,0]]</strong>
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[1,1],[2,2],[3,3]]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[1,1]]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == points.length</code></li>
|
||||
<li><code>1 <= n <= 500</code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
<li>所有点都 <strong>互不相同</strong></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给定一棵二叉树的根节点 <code>root</code> ,请找出该二叉树中每一层的最大值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg" style="height: 172px; width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>root = [1,3,2,5,3,null,9]
|
||||
<strong>输出: </strong>[1,3,9]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>root = [1,2,3]
|
||||
<strong>输出: </strong>[1,3]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树的节点个数的范围是 <code>[0,10<sup>4</sup>]</code></li>
|
||||
<li><meta charset="UTF-8" /><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,35 @@
|
||||
<p><a href="https://baike.baidu.com/item/%E5%A4%8D%E6%95%B0/254365?fr=aladdin" target="_blank">复数</a> 可以用字符串表示,遵循 <code>"<strong>实部</strong>+<strong>虚部</strong>i"</code> 的形式,并满足下述条件:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>实部</code> 是一个整数,取值范围是 <code>[-100, 100]</code></li>
|
||||
<li><code>虚部</code> 也是一个整数,取值范围是 <code>[-100, 100]</code></li>
|
||||
<li><code>i<sup>2</sup> == -1</code></li>
|
||||
</ul>
|
||||
|
||||
<p>给你两个字符串表示的复数 <code>num1</code> 和 <code>num2</code> ,请你遵循复数表示形式,返回表示它们乘积的字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num1 = "1+1i", num2 = "1+1i"
|
||||
<strong>输出:</strong>"0+2i"
|
||||
<strong>解释:</strong>(1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i ,你需要将它转换为 0+2i 的形式。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num1 = "1+-1i", num2 = "1+-1i"
|
||||
<strong>输出:</strong>"0+-2i"
|
||||
<strong>解释:</strong>(1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i ,你需要将它转换为 0+-2i 的形式。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>num1</code> 和 <code>num2</code> 都是有效的复数表示。</li>
|
||||
</ul>
|
@@ -0,0 +1,36 @@
|
||||
<p>有一个 <code>m × n</code> 的矩形岛屿,与 <strong>太平洋</strong> 和 <strong>大西洋</strong> 相邻。 <strong>“太平洋” </strong>处于大陆的左边界和上边界,而 <strong>“大西洋”</strong> 处于大陆的右边界和下边界。</p>
|
||||
|
||||
<p>这个岛被分割成一个由若干方形单元格组成的网格。给定一个 <code>m x n</code> 的整数矩阵 <code>heights</code> , <code>heights[r][c]</code> 表示坐标 <code>(r, c)</code> 上单元格 <strong>高于海平面的高度</strong> 。</p>
|
||||
|
||||
<p>岛上雨水较多,如果相邻单元格的高度 <strong>小于或等于</strong> 当前单元格的高度,雨水可以直接向北、南、东、西流向相邻单元格。水可以从海洋附近的任何单元格流入海洋。</p>
|
||||
|
||||
<p>返回 <em>网格坐标 <code>result</code> 的 <strong>2D列表</strong> ,其中 <code>result[i] = [r<sub>i</sub>, c<sub>i</sub>]</code> 表示雨水可以从单元格 <code>(ri, ci)</code> 流向 <strong>太平洋和大西洋</strong></em> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
|
||||
<strong>输出:</strong> [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> heights = [[2,1],[1,2]]
|
||||
<strong>输出:</strong> [[0,0],[0,1],[1,0],[1,1]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == heights.length</code></li>
|
||||
<li><code>n == heights[r].length</code></li>
|
||||
<li><code>1 <= m, n <= 200</code></li>
|
||||
<li><code>0 <= heights[r][c] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>给定整数 <code>n</code> 和 <code>k</code>,返回 <code>[1, n]</code> 中字典序第 <code>k</code> 小的数字。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>n = 13, k = 2
|
||||
<strong>输出: </strong>10
|
||||
<strong>解释: </strong>字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9],所以第二小的数字是 10。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> n = 1, k = 1
|
||||
<strong>输出:</strong> 1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,10 @@
|
||||
<p>统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。</p>
|
||||
|
||||
<p>请注意,你可以假定字符串里不包括任何不可打印的字符。</p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> "Hello, my name is John"
|
||||
<strong>输出:</strong> 5
|
||||
<strong>解释: </strong>这里的单词是指连续的不是空格的字符,所以 "Hello," 算作 1 个单词。
|
||||
</pre>
|
38
算法题(国内版)/problem (Chinese)/字符串相加 [add-strings].html
Normal file
38
算法题(国内版)/problem (Chinese)/字符串相加 [add-strings].html
Normal file
@@ -0,0 +1,38 @@
|
||||
<p>给定两个字符串形式的非负整数 <code>num1</code> 和<code>num2</code> ,计算它们的和并同样以字符串形式返回。</p>
|
||||
|
||||
<p>你不能使用任何內建的用于处理大整数的库(比如 <code>BigInteger</code>), 也不能直接将输入的字符串转换为整数形式。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num1 = "11", num2 = "123"
|
||||
<strong>输出:</strong>"134"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num1 = "456", num2 = "77"
|
||||
<strong>输出:</strong>"533"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num1 = "0", num2 = "0"
|
||||
<strong>输出:</strong>"0"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num1.length, num2.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>num1</code> 和<code>num2</code> 都只包含数字 <code>0-9</code></li>
|
||||
<li><code>num1</code> 和<code>num2</code> 都不包含任何前导零</li>
|
||||
</ul>
|
28
算法题(国内版)/problem (Chinese)/完美数 [perfect-number].html
Normal file
28
算法题(国内版)/problem (Chinese)/完美数 [perfect-number].html
Normal file
@@ -0,0 +1,28 @@
|
||||
<p>对于一个 <strong>正整数</strong>,如果它和除了它自身以外的所有 <strong>正因子</strong> 之和相等,我们称它为 <strong>「完美数」</strong>。</p>
|
||||
|
||||
<p>给定一个 <strong>整数 </strong><code>n</code>, 如果是完美数,返回 <code>true</code>;否则返回 <code>false</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 28
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>28 = 1 + 2 + 4 + 7 + 14
|
||||
1, 2, 4, 7, 和 14 是 28 的所有正因子。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 7
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给定一个许可密钥字符串 <code>s</code>,仅由字母、数字字符和破折号组成。字符串由 <code>n</code> 个破折号分成 <code>n + 1</code> 组。你也会得到一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>我们想要重新格式化字符串 <code>s</code>,使每一组包含 <code>k</code> 个字符,除了第一组,它可以比 <code>k</code> 短,但仍然必须包含至少一个字符。此外,两组之间必须插入破折号,并且应该将所有小写字母转换为大写字母。</p>
|
||||
|
||||
<p>返回 <em>重新格式化的许可密钥</em> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>S = "5F3Z-2e-9-w", k = 4
|
||||
<strong>输出:</strong>"5F3Z-2E9W"
|
||||
<strong>解释:</strong>字符串 S 被分成了两个部分,每部分 4 个字符;
|
||||
注意,两个额外的破折号需要删掉。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>S = "2-5g-3-J", k = 2
|
||||
<strong>输出:</strong>"2-5G-3J"
|
||||
<strong>解释:</strong>字符串 S 被分成了 3 个部分,按照前面的规则描述,第一部分的字符可以少于给定的数量,其余部分皆为 2 个字符。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 只包含字母、数字和破折号 <code>'-'</code>.</li>
|
||||
<li><code>1 <= k <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
29
算法题(国内版)/problem (Chinese)/对角线遍历 [diagonal-traverse].html
Normal file
29
算法题(国内版)/problem (Chinese)/对角线遍历 [diagonal-traverse].html
Normal file
@@ -0,0 +1,29 @@
|
||||
<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>mat</code> ,请以对角线遍历的顺序,用一个数组返回这个矩阵中的所有元素。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/diag1-grid.jpg" style="width: 334px; height: 334px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>mat = [[1,2,3],[4,5,6],[7,8,9]]
|
||||
<strong>输出:</strong>[1,2,4,7,5,3,6,8,9]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>mat = [[1,2],[3,4]]
|
||||
<strong>输出:</strong>[1,2,3,4]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= mat[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
44
算法题(国内版)/problem (Chinese)/寻找右区间 [find-right-interval].html
Normal file
44
算法题(国内版)/problem (Chinese)/寻找右区间 [find-right-interval].html
Normal file
@@ -0,0 +1,44 @@
|
||||
<p>给你一个区间数组 <code>intervals</code> ,其中 <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> ,且每个 <code>start<sub>i</sub></code> 都 <strong>不同</strong> 。</p>
|
||||
|
||||
<p>区间 <code>i</code> 的 <strong>右侧区间</strong> 可以记作区间 <code>j</code> ,并满足 <code>start<sub>j</sub></code><code> >= end<sub>i</sub></code> ,且 <code>start<sub>j</sub></code> <strong>最小化 </strong>。</p>
|
||||
|
||||
<p>返回一个由每个区间 <code>i</code> 的 <strong>右侧区间</strong> 的最小起始位置组成的数组。如果某个区间 <code>i</code> 不存在对应的 <strong>右侧区间</strong> ,则下标 <code>i</code> 处的值设为 <code>-1</code> 。</p>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>intervals = [[1,2]]
|
||||
<strong>输出:</strong>[-1]
|
||||
<strong>解释:</strong>集合中只有一个区间,所以输出-1。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>intervals = [[3,4],[2,3],[1,2]]
|
||||
<strong>输出:</strong>[-1,0,1]
|
||||
<strong>解释:</strong>对于 [3,4] ,没有满足条件的“右侧”区间。
|
||||
对于 [2,3] ,区间[3,4]具有最小的“右”起点;
|
||||
对于 [1,2] ,区间[2,3]具有最小的“右”起点。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>intervals = [[1,4],[2,3],[3,4]]
|
||||
<strong>输出:</strong>[-1,2,-1]
|
||||
<strong>解释:</strong>对于区间 [1,4] 和 [3,4] ,没有满足条件的“右侧”区间。
|
||||
对于 [2,3] ,区间 [3,4] 有最小的“右”起点。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= intervals.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>intervals[i].length == 2</code></li>
|
||||
<li><code>-10<sup>6</sup> <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
<li>每个间隔的起点都 <strong>不相同</strong></li>
|
||||
</ul>
|
41
算法题(国内版)/problem (Chinese)/岛屿的周长 [island-perimeter].html
Normal file
41
算法题(国内版)/problem (Chinese)/岛屿的周长 [island-perimeter].html
Normal file
@@ -0,0 +1,41 @@
|
||||
<p>给定一个 <code>row x col</code> 的二维网格地图 <code>grid</code> ,其中:<code>grid[i][j] = 1</code> 表示陆地, <code>grid[i][j] = 0</code> 表示水域。</p>
|
||||
|
||||
<p>网格中的格子 <strong>水平和垂直</strong> 方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。</p>
|
||||
|
||||
<p>岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/island.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
|
||||
<strong>输出:</strong>16
|
||||
<strong>解释:</strong>它的周长是上面图片中的 16 个黄色的边</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1]]
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,0]]
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>row == grid.length</code></li>
|
||||
<li><code>col == grid[i].length</code></li>
|
||||
<li><code>1 <= row, col <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> 为 <code>0</code> 或 <code>1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>序列化是将数据结构或对象转换为一系列位的过程,以便它可以存储在文件或内存缓冲区中,或通过网络连接链路传输,以便稍后在同一个或另一个计算机环境中重建。</p>
|
||||
|
||||
<p>设计一个算法来序列化和反序列化<strong> 二叉搜索树</strong> 。 对序列化/反序列化算法的工作方式没有限制。 您只需确保二叉搜索树可以序列化为字符串,并且可以将该字符串反序列化为最初的二叉搜索树。</p>
|
||||
|
||||
<p><strong>编码的字符串应尽可能紧凑。</strong></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [2,1,3]
|
||||
<strong>输出:</strong>[2,1,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = []
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数范围是 <code>[0, 10<sup>4</sup>]</code></li>
|
||||
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li>题目数据 <strong>保证</strong> 输入的树是一棵二叉搜索树。</li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p> </p>
|
||||
如果一个密码满足下述所有条件,则认为这个密码是强密码:
|
||||
|
||||
<ul>
|
||||
<li>由至少 <code>6</code> 个,至多 <code>20</code> 个字符组成。</li>
|
||||
<li>至少包含 <strong>一个小写 </strong>字母,<strong>一个大写</strong> 字母,和 <strong>一个数字</strong> 。</li>
|
||||
<li>同一字符 <strong>不能 </strong>连续出现三次 (比如 <code>"...aaa..."</code> 是不允许的, 但是 <code>"...aa...a..."</code> 如果满足其他条件也可以算是强密码)。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个字符串 <code>password</code> ,返回 <em>将 <code>password</code> 修改到满足强密码条件需要的最少修改步数。如果 <code>password</code> 已经是强密码,则返回 <code>0</code> 。</em></p>
|
||||
|
||||
<p>在一步修改操作中,你可以:</p>
|
||||
|
||||
<ul>
|
||||
<li>插入一个字符到 <code>password</code> ,</li>
|
||||
<li>从 <code>password</code> 中删除一个字符,或</li>
|
||||
<li>用另一个字符来替换 <code>password</code> 中的某个字符。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>password = "a"
|
||||
<strong>输出:</strong>5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>password = "aA1"
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>password = "1337C0d3"
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= password.length <= 50</code></li>
|
||||
<li><code>password</code> 由字母、数字、点 <code>'.'</code> 或者感叹号 <code>'!'</code></li>
|
||||
</ul>
|
45
算法题(国内版)/problem (Chinese)/我能赢吗 [can-i-win].html
Normal file
45
算法题(国内版)/problem (Chinese)/我能赢吗 [can-i-win].html
Normal file
@@ -0,0 +1,45 @@
|
||||
<p>在 "100 game" 这个游戏中,两名玩家轮流选择从 <code>1</code> 到 <code>10</code> 的任意整数,累计整数和,先使得累计整数和 <strong>达到或超过</strong> 100 的玩家,即为胜者。</p>
|
||||
|
||||
<p>如果我们将游戏规则改为 “玩家 <strong>不能</strong> 重复使用整数” 呢?</p>
|
||||
|
||||
<p>例如,两个玩家可以轮流从公共整数池中抽取从 1 到 15 的整数(不放回),直到累计整数和 >= 100。</p>
|
||||
|
||||
<p>给定两个整数 <code>maxChoosableInteger</code> (整数池中可选择的最大数)和 <code>desiredTotal</code>(累计和),若先出手的玩家是否能稳赢则返回 <code>true</code> ,否则返回 <code>false</code> 。假设两位玩家游戏时都表现 <strong>最佳</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>maxChoosableInteger = 10, desiredTotal = 11
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:
|
||||
</strong>无论第一个玩家选择哪个整数,他都会失败。
|
||||
第一个玩家可以选择从 1 到 10 的整数。
|
||||
如果第一个玩家选择 1,那么第二个玩家只能选择从 2 到 10 的整数。
|
||||
第二个玩家可以通过选择整数 10(那么累积和为 11 >= desiredTotal),从而取得胜利.
|
||||
同样地,第一个玩家选择任意其他整数,第二个玩家都会赢。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>maxChoosableInteger = 10, desiredTotal = 0
|
||||
<b>输出:</b>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>maxChoosableInteger = 10, desiredTotal = 1
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= maxChoosableInteger <= 20</code></li>
|
||||
<li><code>0 <= desiredTotal <= 300</code></li>
|
||||
</ul>
|
53
算法题(国内版)/problem (Chinese)/扫雷游戏 [minesweeper].html
Normal file
53
算法题(国内版)/problem (Chinese)/扫雷游戏 [minesweeper].html
Normal file
@@ -0,0 +1,53 @@
|
||||
<p>让我们一起来玩扫雷游戏!</p>
|
||||
|
||||
<p>给你一个大小为 <code>m x n</code> 二维字符矩阵 <code>board</code> ,表示扫雷游戏的盘面,其中:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'M'</code> 代表一个 <strong>未挖出的</strong> 地雷,</li>
|
||||
<li><code>'E'</code> 代表一个<strong> 未挖出的 </strong>空方块,</li>
|
||||
<li><code>'B'</code><strong> </strong>代表没有相邻(上,下,左,右,和所有4个对角线)地雷的<strong> 已挖出的 </strong>空白方块,</li>
|
||||
<li><strong>数字</strong>(<code>'1'</code> 到 <code>'8'</code>)表示有多少地雷与这块<strong> 已挖出的</strong> 方块相邻,</li>
|
||||
<li><code>'X'</code> 则表示一个<strong> 已挖出的</strong> 地雷。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个整数数组 <code>click</code> ,其中 <code>click = [click<sub>r</sub>, click<sub>c</sub>]</code> 表示在所有<strong> 未挖出的 </strong>方块(<code>'M'</code> 或者 <code>'E'</code>)中的下一个点击位置(<code>click<sub>r</sub></code> 是行下标,<code>click<sub>c</sub></code> 是列下标)。</p>
|
||||
|
||||
<p>根据以下规则,返回相应位置被点击后对应的盘面:</p>
|
||||
|
||||
<ol>
|
||||
<li>如果一个地雷(<code>'M'</code>)被挖出,游戏就结束了- 把它改为 <code>'X'</code> 。</li>
|
||||
<li>如果一个<strong> 没有相邻地雷 </strong>的空方块(<code>'E'</code>)被挖出,修改它为(<code>'B'</code>),并且所有和其相邻的<strong> 未挖出 </strong>方块都应该被递归地揭露。</li>
|
||||
<li>如果一个<strong> 至少与一个地雷相邻</strong> 的空方块(<code>'E'</code>)被挖出,修改它为数字(<code>'1'</code> 到 <code>'8'</code> ),表示相邻地雷的数量。</li>
|
||||
<li>如果在此次点击中,若无更多方块可被揭露,则返回盘面。</li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/12/minesweeper_example_1.png" style="width: 500px; max-width: 400px; height: 269px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>board = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]], click = [3,0]
|
||||
<strong>输出:</strong>[["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/12/minesweeper_example_2.png" style="width: 500px; max-width: 400px; height: 275px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2]
|
||||
<strong>输出:</strong>[["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == board.length</code></li>
|
||||
<li><code>n == board[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 50</code></li>
|
||||
<li><code>board[i][j]</code> 为 <code>'M'</code>、<code>'E'</code>、<code>'B'</code> 或数字 <code>'1'</code> 到 <code>'8'</code> 中的一个</li>
|
||||
<li><code>click.length == 2</code></li>
|
||||
<li><code>0 <= click<sub>r</sub> < m</code></li>
|
||||
<li><code>0 <= click<sub>c</sub> < n</code></li>
|
||||
<li><code>board[click<sub>r</sub>][click<sub>c</sub>]</code> 为 <code>'M'</code> 或 <code>'E'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>给定两个字符串 <code>s</code> 和 <code>p</code>,找到 <code>s</code><strong> </strong>中所有 <code>p</code><strong> </strong>的 <strong>异位词 </strong>的子串,返回这些子串的起始索引。不考虑答案输出的顺序。</p>
|
||||
|
||||
<p><strong>异位词 </strong>指由相同字母重排列形成的字符串(包括相同的字符串)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>s = "cbaebabacd", p = "abc"
|
||||
<strong>输出: </strong>[0,6]
|
||||
<strong>解释:</strong>
|
||||
起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词。
|
||||
起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词。
|
||||
</pre>
|
||||
|
||||
<p><strong> 示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>s = "abab", p = "ab"
|
||||
<strong>输出: </strong>[0,1,2]
|
||||
<strong>解释:</strong>
|
||||
起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词。
|
||||
起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词。
|
||||
起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length, p.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> 和 <code>p</code> 仅包含小写字母</li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>给你一个含 <code>n</code> 个整数的数组 <code>nums</code> ,其中 <code>nums[i]</code> 在区间 <code>[1, n]</code> 内。请你找出所有在 <code>[1, n]</code> 范围内但没有出现在 <code>nums</code> 中的数字,并以数组的形式返回结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,3,2,7,8,2,3,1]
|
||||
<strong>输出:</strong>[5,6]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1]
|
||||
<strong>输出:</strong>[2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= n</code></li>
|
||||
</ul>
|
||||
|
||||
<p><strong>进阶:</strong>你能在不使用额外空间且时间复杂度为<em> </em><code>O(n)</code><em> </em>的情况下解决这个问题吗? 你可以假定返回的数组不算在额外空间内。</p>
|
@@ -0,0 +1,32 @@
|
||||
<p>给定一个二叉树的 <strong>根节点</strong> <code>root</code>,请找出该二叉树的 <strong>最底层 最左边 </strong>节点的值。</p>
|
||||
|
||||
<p>假设二叉树中至少有一个节点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 182px; " /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>root = [2,1,3]
|
||||
<strong>输出: </strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 242px; " /><strong> </strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>[1,2,3,4,null,5,6,null,null,7]
|
||||
<strong>输出: </strong>7
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树的节点个数的范围是 <code>[1,10<sup>4</sup>]</code></li>
|
||||
<li><meta charset="UTF-8" /><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code> </li>
|
||||
</ul>
|
@@ -0,0 +1,50 @@
|
||||
<p>给出二叉<strong> 搜索 </strong>树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 <code>node</code> 的新值等于原树中大于或等于 <code>node.val</code> 的值之和。</p>
|
||||
|
||||
<p>提醒一下,二叉搜索树满足下列约束条件:</p>
|
||||
|
||||
<ul>
|
||||
<li>节点的左子树仅包含键<strong> 小于 </strong>节点键的节点。</li>
|
||||
<li>节点的右子树仅包含键<strong> 大于</strong> 节点键的节点。</li>
|
||||
<li>左右子树也必须是二叉搜索树。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意:</strong>本题和 1038: <a href="https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/">https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/</a> 相同</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/05/03/tree.png" style="height: 364px; width: 534px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
|
||||
<strong>输出:</strong>[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [0,null,1]
|
||||
<strong>输出:</strong>[1,null,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [1,0,2]
|
||||
<strong>输出:</strong>[3,3,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>root = [3,2,4,1]
|
||||
<strong>输出:</strong>[7,9,4,10]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中的节点数介于 <code>0</code> 和 <code>10<sup>4</sup></code><sup> </sup>之间。</li>
|
||||
<li>每个节点的值介于 <code>-10<sup>4</sup></code> 和 <code>10<sup>4</sup></code> 之间。</li>
|
||||
<li>树中的所有值 <strong>互不相同</strong> 。</li>
|
||||
<li>给定的树为二叉搜索树。</li>
|
||||
</ul>
|
29
算法题(国内版)/problem (Chinese)/排列硬币 [arranging-coins].html
Normal file
29
算法题(国内版)/problem (Chinese)/排列硬币 [arranging-coins].html
Normal file
@@ -0,0 +1,29 @@
|
||||
<p>你总共有 <code>n</code><em> </em>枚硬币,并计划将它们按阶梯状排列。对于一个由 <code>k</code> 行组成的阶梯,其第 <code>i</code><em> </em>行必须正好有 <code>i</code><em> </em>枚硬币。阶梯的最后一行 <strong>可能</strong> 是不完整的。</p>
|
||||
|
||||
<p>给你一个数字 <code>n</code><em> </em>,计算并返回可形成 <strong>完整阶梯行</strong> 的总行数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/arrangecoins1-grid.jpg" style="width: 253px; height: 253px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>因为第三行不完整,所以返回 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/arrangecoins2-grid.jpg" style="width: 333px; height: 333px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 8
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>因为第四行不完整,所以返回 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
41
算法题(国内版)/problem (Chinese)/提莫攻击 [teemo-attacking].html
Normal file
41
算法题(国内版)/problem (Chinese)/提莫攻击 [teemo-attacking].html
Normal file
@@ -0,0 +1,41 @@
|
||||
<p>在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄。他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。</p>
|
||||
|
||||
<p>当提莫攻击艾希,艾希的中毒状态正好持续 <code>duration</code> 秒。</p>
|
||||
|
||||
<p>正式地讲,提莫在 <code>t</code> 发起发起攻击意味着艾希在时间区间 <code>[t, t + duration - 1]</code>(含 <code>t</code> 和 <code>t + duration - 1</code>)处于中毒状态。如果提莫在中毒影响结束 <strong>前</strong> 再次攻击,中毒状态计时器将会 <strong>重置</strong> ,在新的攻击之后,中毒影响将会在 <code>duration</code> 秒后结束。</p>
|
||||
|
||||
<p>给你一个 <strong>非递减</strong> 的整数数组 <code>timeSeries</code> ,其中 <code>timeSeries[i]</code> 表示提莫在 <code>timeSeries[i]</code> 秒时对艾希发起攻击,以及一个表示中毒持续时间的整数 <code>duration</code> 。</p>
|
||||
|
||||
<p>返回艾希处于中毒状态的 <strong>总</strong> 秒数。</p>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>timeSeries = [1,4], duration = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>提莫攻击对艾希的影响如下:
|
||||
- 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
|
||||
- 第 4 秒,提莫再次攻击艾希,艾希中毒状态又持续 2 秒,即第 4 秒和第 5 秒。
|
||||
艾希在第 1、2、4、5 秒处于中毒状态,所以总中毒秒数是 4 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>timeSeries = [1,2], duration = 2
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>提莫攻击对艾希的影响如下:
|
||||
- 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
|
||||
- 第 2 秒,提莫再次攻击艾希,并重置中毒计时器,艾希中毒状态需要持续 2 秒,即第 2 秒和第 3 秒。
|
||||
艾希在第 1、2、3 秒处于中毒状态,所以总中毒秒数是 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= timeSeries.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code></li>
|
||||
<li><code>timeSeries</code> 按 <strong>非递减</strong> 顺序排列</li>
|
||||
</ul>
|
40
算法题(国内版)/problem (Chinese)/数字的补数 [number-complement].html
Normal file
40
算法题(国内版)/problem (Chinese)/数字的补数 [number-complement].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>对整数的二进制表示取反(<code>0</code> 变 <code>1</code> ,<code>1</code> 变 <code>0</code>)后,再转换为十进制表示,可以得到这个整数的补数。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,整数 <code>5</code> 的二进制表示是 <code>"101"</code> ,取反后得到 <code>"010"</code> ,再转回十进制表示得到补数 <code>2</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个整数 <code>num</code> ,输出它的补数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ol>
|
||||
</ol>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 5
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>5 的二进制表示为 101(没有前导零位),其补数为 010。所以你需要输出 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 1
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>1 的二进制表示为 1(没有前导零位),其补数为 0。所以你需要输出 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num < 2<sup>31</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>本题与 1009 <a href="https://leetcode-cn.com/problems/complement-of-base-10-integer/">https://leetcode-cn.com/problems/complement-of-base-10-integer/</a> 相同</p>
|
@@ -0,0 +1,53 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,返回<em> </em><code>nums[i] XOR nums[j]</code> 的最大运算结果,其中 <code>0 ≤ i ≤ j < n</code> 。</p>
|
||||
|
||||
<p><strong>进阶:</strong>你可以在 <code>O(n)</code> 的时间解决这个问题吗?</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3,10,5,25,2,8]
|
||||
<strong>输出:</strong>28
|
||||
<strong>解释:</strong>最大运算结果是 5 XOR 25 = 28.</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,4]
|
||||
<strong>输出:</strong>6
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [8,10,2]
|
||||
<strong>输出:</strong>10
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [14,70,53,83,49,91,36,80,92,51,66,70]
|
||||
<strong>输出:</strong>127
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
@@ -0,0 +1,47 @@
|
||||
<p>给定一个整数数组和一个整数 <code><strong>k</strong></code>,你需要在数组里找到<strong> 不同的 </strong>k-diff 数对,并返回不同的 <strong>k-diff 数对</strong> 的数目。</p>
|
||||
|
||||
<p>这里将 <strong>k-diff</strong> 数对定义为一个整数对 <code>(nums[i], nums[j])</code>,并满足下述全部条件:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j < nums.length</code></li>
|
||||
<li><code>|nums[i] - nums[j]| == k</code></li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意</strong>,<code>|val|</code> 表示 <code>val</code> 的绝对值。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [3, 1, 4, 1, 5], k = 2
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>数组中有两个 2-diff 数对, (1, 3) 和 (3, 5)。
|
||||
尽管数组中有两个1,但我们只应返回不同的数对的数量。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1, 2, 3, 4, 5], k = 1
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>数组中有四个 1-diff 数对, (1, 2), (2, 3), (3, 4) 和 (4, 5)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1, 3, 1, 5, 4], k = 0
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>数组中只有一个 0-diff 数对,(1, 1)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> ,其中 <code>nums</code> 的所有整数都在范围 <code>[1, n]</code> 内,且每个整数出现 <strong>一次</strong> 或 <strong>两次</strong> 。请你找出所有出现 <strong>两次</strong> 的整数,并以数组形式返回。</p>
|
||||
|
||||
<p>你必须设计并实现一个时间复杂度为 <code>O(n)</code> 且仅使用常量额外空间的算法解决此问题。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,3,2,7,8,2,3,1]
|
||||
<strong>输出:</strong>[2,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,2]
|
||||
<strong>输出:</strong>[1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1]
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= n</code></li>
|
||||
<li><code>nums</code> 中的每个元素出现 <strong>一次</strong> 或 <strong>两次</strong></li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给定一个区间的集合 <code>intervals</code> ,其中 <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 。返回 <em>需要移除区间的最小数量,使剩余区间互不重叠 </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> intervals = [[1,2],[2,3],[3,4],[1,3]]
|
||||
<strong>输出:</strong> 1
|
||||
<strong>解释:</strong> 移除 [1,3] 后,剩下的区间没有重叠。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> intervals = [ [1,2], [1,2], [1,2] ]
|
||||
<strong>输出:</strong> 2
|
||||
<strong>解释:</strong> 你需要移除两个 [1,2] 来使剩下的区间没有重叠。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> intervals = [ [1,2], [2,3] ]
|
||||
<strong>输出:</strong> 0
|
||||
<strong>解释:</strong> 你不需要移除任何区间,因为它们已经是无重叠的了。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= intervals.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>intervals[i].length == 2</code></li>
|
||||
<li><code>-5 * 10<sup>4</sup> <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>4</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你一个字符串 <code>s</code> 和一个整数 <code>k</code> 。你可以选择字符串中的任一字符,并将其更改为任何其他大写英文字符。该操作最多可执行 <code>k</code> 次。</p>
|
||||
|
||||
<p>在执行上述操作后,返回包含相同字母的最长子字符串的长度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "ABAB", k = 2
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>用两个'A'替换为两个'B',反之亦然。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "AABABBA", k = 1
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>
|
||||
将中间的一个'A'替换为'B',字符串变为 "AABBBBA"。
|
||||
子串 "BBBB" 有最长重复字母, 答案为 4。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 仅由大写英文字母组成</li>
|
||||
<li><code>0 <= k <= s.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>给定一个整数 n ,返回 <em>可表示为两个 <code>n</code> 位整数乘积的 <strong>最大回文整数</strong></em> 。因为答案可能非常大,所以返回它对 <code>1337</code> <strong>取余</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 2
|
||||
<b>输出:</b>987
|
||||
<strong>解释:</strong>99 x 91 = 9009, 9009 % 1337 = 987
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> n = 1
|
||||
<strong>输出:</strong> 9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 8</code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>给定一个二进制数组 <code>nums</code> , 计算其中最大连续 <code>1</code> 的个数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,0,1,1,1]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>开头的两位和最后的三位都是连续 1 ,所以最大连续 1 的个数是 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,0,1,1,0,1]
|
||||
<b>输出:</b>2
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums[i]</code> 不是 <code>0</code> 就是 <code>1</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>一条基因序列由一个带有8个字符的字符串表示,其中每个字符都属于 <code>"A"</code>, <code>"C"</code>, <code>"G"</code>, <code>"T"</code>中的任意一个。</p>
|
||||
|
||||
<p>假设我们要调查一个基因序列的变化。<strong>一次</strong>基因变化意味着这个基因序列中的<strong>一个</strong>字符发生了变化。</p>
|
||||
|
||||
<p>例如,基因序列由<code>"AACCGGTT"</code> 变化至 <code>"AACCGGTA" </code>即发生了一次基因变化。</p>
|
||||
|
||||
<p>与此同时,每一次基因变化的结果,都需要是一个合法的基因串,即该结果属于一个基因库。</p>
|
||||
|
||||
<p>现在给定3个参数 — start, end, bank,分别代表起始基因序列,目标基因序列及基因库,请找出能够使起始基因序列变化为目标基因序列所需的最少变化次数。如果无法实现目标变化,请返回 -1。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>起始基因序列默认是合法的,但是它并不一定会出现在基因库中。</li>
|
||||
<li>如果一个起始基因序列需要多次变化,那么它每一次变化之后的基因序列都必须是合法的。</li>
|
||||
<li>假定起始基因序列与目标基因序列是不一样的。</li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
start: "AACCGGTT"
|
||||
end: "AACCGGTA"
|
||||
bank: ["AACCGGTA"]
|
||||
|
||||
返回值: 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
start: "AACCGGTT"
|
||||
end: "AAACGGTA"
|
||||
bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]
|
||||
|
||||
返回值: 2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
start: "AAAAACCC"
|
||||
end: "AACCCCCC"
|
||||
bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
|
||||
|
||||
返回值: 3
|
||||
</pre>
|
38
算法题(国内版)/problem (Chinese)/最小好进制 [smallest-good-base].html
Normal file
38
算法题(国内版)/problem (Chinese)/最小好进制 [smallest-good-base].html
Normal file
@@ -0,0 +1,38 @@
|
||||
<p>以字符串的形式给出 <code>n</code> , 以字符串的形式返回<em> <code>n</code> 的最小 <strong>好进制</strong> </em> 。</p>
|
||||
|
||||
<p>如果 <code>n</code> 的 <code>k(k>=2)</code> 进制数的所有数位全为1,则称 <code>k(k>=2)</code> 是 <code>n</code> 的一个 <strong>好进制 </strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = "13"
|
||||
<strong>输出:</strong>"3"
|
||||
<strong>解释:</strong>13 的 3 进制是 111。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = "4681"
|
||||
<strong>输出:</strong>"8"
|
||||
<strong>解释:</strong>4681 的 8 进制是 11111。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = "1000000000000000000"
|
||||
<strong>输出:</strong>"999999999999999999"
|
||||
<strong>解释:</strong>1000000000000000000 的 999999999999999999 进制是 11。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n</code> 的取值范围是 <code>[3, 10<sup>18</sup>]</code></li>
|
||||
<li><code>n</code> 没有前导 0</li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组,每次操作将会使 <code>n - 1</code> 个元素增加 <code>1</code> 。返回让数组所有元素相等的最小操作次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
只需要3次操作(注意每次操作会增加两个元素的值):
|
||||
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li>答案保证符合 <strong>32-bit</strong> 整数</li>
|
||||
</ul>
|
@@ -0,0 +1,26 @@
|
||||
<p>给定一个 24 小时制(小时:分钟 <strong>"HH:MM"</strong>)的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>timePoints = ["23:59","00:00"]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>timePoints = ["00:00","23:59","00:00"]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>timePoints[i]</code> 格式为 <strong>"HH:MM"</strong></li>
|
||||
</ul>
|
@@ -0,0 +1,16 @@
|
||||
<p>给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1。 您可以假设数组的长度最多为10000。</p>
|
||||
|
||||
<p><strong>例如:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
[1,2,3]
|
||||
|
||||
<strong>输出:</strong>
|
||||
2
|
||||
|
||||
<strong>说明:
|
||||
</strong>只有两个动作是必要的(记得每一步仅可使其中一个元素加1或减1):
|
||||
|
||||
[1,2,3] => [2,2,3] => [2,2,2]
|
||||
</pre>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你一个字符串 <code>s</code> ,找出其中最长的回文子序列,并返回该序列的长度。</p>
|
||||
|
||||
<p>子序列定义为:不改变剩余字符顺序的情况下,删除某些字符或者不删除任何字符形成的一个序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "bbbab"
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>一个可能的最长回文子序列为 "bbbb" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "cbbd"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>一个可能的最长回文子序列为 "bb" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> 仅由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>给定字符串列表 <code>strs</code> ,返回 <em>它们中 <strong>最长的特殊序列</strong></em> 。如果最长特殊序列不存在,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>最长特殊序列</strong> 定义如下:该序列为某字符串 <strong>独有的最长子序列(即不能是其他字符串的子序列)</strong>。</p>
|
||||
|
||||
<p> <code>s</code> 的 <strong>子序列</strong>可以通过删去字符串 <code>s</code> 中的某些字符实现。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>"abc"</code> 是 <code>"aebdc"</code> 的子序列,因为您可以删除<code>"a<u>e</u>b<u>d</u>c"</code>中的下划线字符来得到 <code>"abc"</code> 。<code>"aebdc"</code>的子序列还包括<code>"aebdc"</code>、 <code>"aeb"</code> 和 <font color="#c7254e" face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size: 12.6px; background-color: rgb(249, 242, 244);">""</span></font> (空字符串)。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> strs = ["aba","cdc","eae"]
|
||||
<strong>输出:</strong> 3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> strs = ["aaa","aaa","aa"]
|
||||
<strong>输出:</strong> -1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= strs.length <= 50</code></li>
|
||||
<li><code>1 <= strs[i].length <= 10</code></li>
|
||||
<li><code>strs[i]</code> 只包含小写英文字母</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>给你两个字符串 <code>a</code> 和 <code>b</code>,请返回 <em>这两个字符串中 <strong>最长的特殊序列</strong> </em> 的长度。如果不存在,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p><strong>「最长特殊序列」</strong> 定义如下:该序列为 <strong>某字符串独有的最长子序列(即不能是其他字符串的子序列)</strong> 。</p>
|
||||
|
||||
<p>字符串 <code>s</code> 的子序列是在从 <code>s</code> 中删除任意数量的字符后可以获得的字符串。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>"abc"</code> 是 <code>"aebdc"</code> 的子序列,因为删除 <code>"a<em><strong>e</strong></em>b<strong><em>d</em></strong>c"</code> 中斜体加粗的字符可以得到 <code>"abc"</code> 。 <code>"aebdc"</code> 的子序列还包括 <code>"aebdc"</code> 、 <code>"aeb"</code> 和 <code>""</code> (空字符串)。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> a = "aba", b = "cdc"
|
||||
<strong>输出:</strong> 3
|
||||
<strong>解释:</strong> 最长特殊序列可为 "aba" (或 "cdc"),两者均为自身的子序列且不是对方的子序列。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>a = "aaa", b = "bbb"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong> 最长特殊序列是 "aaa" 和 "bbb" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>a = "aaa", b = "aaa"
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong> 字符串 a 的每个子序列也是字符串 b 的每个子序列。同样,字符串 b 的每个子序列也是字符串 a 的子序列。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a.length, b.length <= 100</code></li>
|
||||
<li><code>a</code> 和 <code>b</code> 由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>给你一个仅由整数组成的有序数组,其中每个元素都会出现两次,唯有一个数只会出现一次。</p>
|
||||
|
||||
<p>请你找出并返回只出现一次的那个数。</p>
|
||||
|
||||
<p>你设计的解决方案必须满足 <code>O(log n)</code> 时间复杂度和 <code>O(1)</code> 空间复杂度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [1,1,2,3,3,4,4,8,8]
|
||||
<strong>输出:</strong> 2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [3,3,7,7,10,11,11]
|
||||
<strong>输出:</strong> 10
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><meta charset="UTF-8" /></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,41 @@
|
||||
<p>作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的。 所以,现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面。要求:</p>
|
||||
|
||||
<ol>
|
||||
<li>你设计的矩形页面必须等于给定的目标面积。</li>
|
||||
<li>宽度 <code>W</code> 不应大于长度 <code>L</code> ,换言之,要求 <code>L >= W </code>。</li>
|
||||
<li>长度 <code>L</code> 和宽度 <code>W</code> 之间的差距应当尽可能小。</li>
|
||||
</ol>
|
||||
|
||||
<p>返回一个 <em>数组</em> <code>[L, W]</code>,其中 <em><code>L</code> 和 <code>W</code> 是你按照顺序设计的网页的长度和宽度</em>。<br />
|
||||
</p>
|
||||
|
||||
<p><strong>示例1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> 4
|
||||
<strong>输出:</strong> [2, 2]
|
||||
<strong>解释:</strong> 目标面积是 4, 所有可能的构造方案有 [1,4], [2,2], [4,1]。
|
||||
但是根据要求2,[1,4] 不符合要求; 根据要求3,[2,2] 比 [4,1] 更能符合要求. 所以输出长度 L 为 2, 宽度 W 为 2。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> area = 37
|
||||
<strong>输出:</strong> [37,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> area = 122122
|
||||
<strong>输出:</strong> [427,286]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= area <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给定一个字符串 <code>s</code> ,根据字符出现的 <strong>频率</strong> 对其进行 <strong>降序排序</strong> 。一个字符出现的 <strong>频率</strong> 是它出现在字符串中的次数。</p>
|
||||
|
||||
<p>返回 <em>已排序的字符串 </em>。如果有多个答案,返回其中任何一个。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>s = "tree"
|
||||
<strong>输出: </strong>"eert"
|
||||
<strong>解释: </strong>'e'出现两次,'r'和't'都只出现一次。
|
||||
因此'e'必须出现在'r'和't'之前。此外,"eetr"也是一个有效的答案。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>s = "cccaaa"
|
||||
<strong>输出: </strong>"cccaaa"
|
||||
<strong>解释: </strong>'c'和'a'都出现三次。此外,"aaaccc"也是有效的答案。
|
||||
注意"cacaca"是不正确的,因为相同的字母必须放在一起。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>s = "Aabb"
|
||||
<strong>输出: </strong>"bbAa"
|
||||
<strong>解释: </strong>此外,"bbaA"也是一个有效的答案,但"Aabb"是不正确的。
|
||||
注意'A'和'a'被认为是两种不同的字符。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 由大小写英文字母和数字组成</li>
|
||||
</ul>
|
34
算法题(国内版)/problem (Chinese)/检测大写字母 [detect-capital].html
Normal file
34
算法题(国内版)/problem (Chinese)/检测大写字母 [detect-capital].html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>我们定义,在以下情况时,单词的大写用法是正确的:</p>
|
||||
|
||||
<ul>
|
||||
<li>全部字母都是大写,比如 <code>"USA"</code> 。</li>
|
||||
<li>单词中所有字母都不是大写,比如 <code>"leetcode"</code> 。</li>
|
||||
<li>如果单词不只含有一个字母,只有首字母大写, 比如 <code>"Google"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个字符串 <code>word</code> 。如果大写用法正确,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "USA"
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>word = "FlaG"
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> 由小写和大写英文字母组成</li>
|
||||
</ul>
|
32
算法题(国内版)/problem (Chinese)/汉明距离 [hamming-distance].html
Normal file
32
算法题(国内版)/problem (Chinese)/汉明距离 [hamming-distance].html
Normal file
@@ -0,0 +1,32 @@
|
||||
<p>两个整数之间的 <a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E8%B7%9D%E7%A6%BB">汉明距离</a> 指的是这两个数字对应二进制位不同的位置的数目。</p>
|
||||
|
||||
<p>给你两个整数 <code>x</code> 和 <code>y</code>,计算并返回它们之间的汉明距离。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>x = 1, y = 4
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
1 (0 0 0 1)
|
||||
4 (0 1 0 0)
|
||||
↑ ↑
|
||||
上面的箭头指出了对应二进制位不同的位置。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>x = 3, y = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= x, y <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>两个整数的 <a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E8%B7%9D%E7%A6%BB/475174?fr=aladdin">汉明距离</a> 指的是这两个数字的二进制数对应位不同的数量。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>nums</code>,请你计算并返回 <code>nums</code> 中任意两个数之间 <strong>汉明距离的总和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,14,2]
|
||||
<strong>输出:</strong>6
|
||||
<strong>解释:</strong>在二进制表示中,4 表示为 0100 ,14 表示为 1110 ,2表示为 0010 。(这样表示是为了体现后四位之间关系)
|
||||
所以答案为:
|
||||
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,14,4]
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li>给定输入的对应答案符合 <strong>32-bit</strong> 整数范围</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>中位数是有序序列最中间的那个数。如果序列的长度是偶数,则没有最中间的数;此时中位数是最中间的两个数的平均数。</p>
|
||||
|
||||
<p>例如:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>[2,3,4]</code>,中位数是 <code>3</code></li>
|
||||
<li><code>[2,3]</code>,中位数是 <code>(2 + 3) / 2 = 2.5</code></li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个数组 <em>nums</em>,有一个长度为 <em>k</em> 的窗口从最左端滑动到最右端。窗口中有 <em>k</em> 个数,每次窗口向右移动 <em>1</em> 位。你的任务是找出每次窗口移动后得到的新窗口中元素的中位数,并输出由它们组成的数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<p>给出 <em>nums</em> = <code>[1,3,-1,-3,5,3,6,7]</code>,以及 <em>k</em> = 3。</p>
|
||||
|
||||
<pre>
|
||||
窗口位置 中位数
|
||||
--------------- -----
|
||||
[1 3 -1] -3 5 3 6 7 1
|
||||
1 [3 -1 -3] 5 3 6 7 -1
|
||||
1 3 [-1 -3 5] 3 6 7 -1
|
||||
1 3 -1 [-3 5 3] 6 7 3
|
||||
1 3 -1 -3 [5 3 6] 7 5
|
||||
1 3 -1 -3 5 [3 6 7] 6
|
||||
</pre>
|
||||
|
||||
<p> 因此,返回该滑动窗口的中位数数组 <code>[1,-1,-1,3,5,6]</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>你可以假设 <code>k</code> 始终有效,即:<code>k</code> 始终小于等于输入的非空数组的元素个数。</li>
|
||||
<li>与真实值误差在 <code>10 ^ -5</code> 以内的答案将被视作正确答案。</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>你将得到一个整数数组 <code>matchsticks</code> ,其中 <code>matchsticks[i]</code> 是第 <code>i</code> 个火柴棒的长度。你要用 <strong>所有的火柴棍</strong> 拼成一个正方形。你 <strong>不能折断</strong> 任何一根火柴棒,但你可以把它们连在一起,而且每根火柴棒必须 <strong>使用一次</strong> 。</p>
|
||||
|
||||
<p>如果你能使这个正方形,则返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2021/04/09/matchsticks1-grid.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> matchsticks = [1,1,2,2,2]
|
||||
<strong>输出:</strong> true
|
||||
<strong>解释:</strong> 能拼成一个边长为2的正方形,每边两根火柴。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> matchsticks = [3,3,3,3,4]
|
||||
<strong>输出:</strong> false
|
||||
<strong>解释:</strong> 不能用所有火柴拼成一个正方形。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= matchsticks.length <= 15</code></li>
|
||||
<li><code>1 <= matchsticks[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>存在一个不含 <code>0</code> 的<strong> 环形 </strong>数组 <code>nums</code> ,每个 <code>nums[i]</code> 都表示位于下标 <code>i</code> 的角色应该向前或向后移动的下标个数:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>nums[i]</code> 是正数,<strong>向前</strong>(下标递增方向)移动 <code>|nums[i]|</code> 步</li>
|
||||
<li>如果 <code>nums[i]</code> 是负数,<strong>向后</strong>(下标递减方向)移动 <code>|nums[i]|</code> 步</li>
|
||||
</ul>
|
||||
|
||||
<p>因为数组是 <strong>环形</strong> 的,所以可以假设从最后一个元素向前移动一步会到达第一个元素,而第一个元素向后移动一步会到达最后一个元素。</p>
|
||||
|
||||
<p>数组中的 <strong>循环</strong> 由长度为 <code>k</code> 的下标序列 <code>seq</code> 标识:</p>
|
||||
|
||||
<ul>
|
||||
<li>遵循上述移动规则将导致一组重复下标序列 <code>seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...</code></li>
|
||||
<li>所有 <code>nums[seq[j]]</code> 应当不是 <strong>全正</strong> 就是 <strong>全负</strong></li>
|
||||
<li><code>k > 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p>如果 <code>nums</code> 中存在循环,返回 <code>true</code> ;否则,返回<em> </em><code>false</code><em> </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,-1,1,2,2]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>存在循环,按下标 0 -> 2 -> 3 -> 0 。循环长度为 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-1,2]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>按下标 1 -> 1 -> 1 ... 的运动无法构成循环,因为循环的长度为 1 。根据定义,循环的长度必须大于 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [-2,1,-1,-2,-2]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>按下标 1 -> 2 -> 1 -> ... 的运动无法构成循环,因为 nums[1] 是正数,而 nums[2] 是负数。
|
||||
所有 nums[seq[j]] 应当不是全正就是全负。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 5000</code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
<li><code>nums[i] != 0</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你能设计一个时间复杂度为 <code>O(n)</code> 且额外空间复杂度为 <code>O(1)</code> 的算法吗?</p>
|
@@ -0,0 +1,42 @@
|
||||
<p>把字符串 <code>s</code> 看作是 <code>“abcdefghijklmnopqrstuvwxyz”</code> 的无限环绕字符串,所以 <code>s</code> 看起来是这样的:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>"...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd...."</code> . </li>
|
||||
</ul>
|
||||
|
||||
<p>现在给定另一个字符串 <code>p</code> 。返回<em> <code>s</code> 中 <strong>唯一</strong> 的 <code>p</code> 的 <strong>非空子串</strong> 的数量 </em>。 </p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> p = "a"
|
||||
<strong>输出:</strong> 1
|
||||
<strong>解释:</strong> 字符串 s 中只有一个"a"子字符。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> p = "cac"
|
||||
<strong>输出:</strong> 2
|
||||
<strong>解释:</strong> 字符串 s 中的字符串“cac”只有两个子串“a”、“c”。.
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> p = "zab"
|
||||
<strong>输出:</strong> 6
|
||||
<strong>解释:</strong> 在字符串 s 中有六个子串“z”、“a”、“b”、“za”、“ab”、“zab”。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= p.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>p</code> 由小写英文字母构成</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>有一些球形气球贴在一堵用 XY 平面表示的墙面上。墙面上的气球记录在整数数组 <code>points</code> ,其中<code>points[i] = [x<sub>start</sub>, x<sub>end</sub>]</code> 表示水平直径在 <code>x<sub>start</sub></code> 和 <code>x<sub>end</sub></code>之间的气球。你不知道气球的确切 y 坐标。</p>
|
||||
|
||||
<p>一支弓箭可以沿着 x 轴从不同点 <strong>完全垂直</strong> 地射出。在坐标 <code>x</code> 处射出一支箭,若有一个气球的直径的开始和结束坐标为 <code>x</code><sub><code>start</code>,</sub><code>x</code><sub><code>end</code>,</sub> 且满足 <code>x<sub>start</sub> ≤ x ≤ x</code><sub><code>end</code>,</sub>则该气球会被 <strong>引爆</strong> <sub>。</sub>可以射出的弓箭的数量 <strong>没有限制</strong> 。 弓箭一旦被射出之后,可以无限地前进。</p>
|
||||
|
||||
<p>给你一个数组 <code>points</code> ,<em>返回引爆所有气球所必须射出的 <strong>最小</strong> 弓箭数 </em>。</p>
|
||||
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[10,16],[2,8],[1,6],[7,12]]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>气球可以用2支箭来爆破:
|
||||
-在x = 6处射出箭,击破气球[2,8]和[1,6]。
|
||||
-在x = 11处发射箭,击破气球[10,16]和[7,12]。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[1,2],[3,4],[5,6],[7,8]]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>每个气球需要射出一支箭,总共需要4支箭。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>points = [[1,2],[2,3],[3,4],[4,5]]
|
||||
<strong>输出:</strong>2
|
||||
解释:气球可以用2支箭来爆破:
|
||||
- 在x = 2处发射箭,击破气球[1,2]和[2,3]。
|
||||
- 在x = 4处射出箭,击破气球[3,4]和[4,5]。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><meta charset="UTF-8" /></p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= points.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>-2<sup>31</sup> <= x<sub>start</sub> < x<sub>end</sub> <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>board</code> 表示甲板,其中,每个单元格可以是一艘战舰 <code>'X'</code> 或者是一个空位 <code>'.'</code> ,返回在甲板 <code>board</code> 上放置的 <strong>战舰</strong> 的数量。</p>
|
||||
|
||||
<p><strong>战舰</strong> 只能水平或者垂直放置在 <code>board</code> 上。换句话说,战舰只能按 <code>1 x k</code>(<code>1</code> 行,<code>k</code> 列)或 <code>k x 1</code>(<code>k</code> 行,<code>1</code> 列)的形状建造,其中 <code>k</code> 可以是任意大小。两艘战舰之间至少有一个水平或垂直的空位分隔 (即没有相邻的战舰)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/battelship-grid.jpg" style="width: 333px; height: 333px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>board = [["."]]
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == board.length</code></li>
|
||||
<li><code>n == board[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 200</code></li>
|
||||
<li><code>board[i][j]</code> 是 <code>'.'</code> 或 <code>'X'</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你可以实现一次扫描算法,并只使用<strong> </strong><code>O(1)</code><strong> </strong>额外空间,并且不修改 <code>board</code> 的值来解决这个问题吗?</p>
|
42
算法题(国内版)/problem (Chinese)/目标和 [target-sum].html
Normal file
42
算法题(国内版)/problem (Chinese)/目标和 [target-sum].html
Normal file
@@ -0,0 +1,42 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>target</code> 。</p>
|
||||
|
||||
<p>向数组中的每个整数前添加 <code>'+'</code> 或 <code>'-'</code> ,然后串联起所有整数,可以构造一个 <strong>表达式</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>nums = [2, 1]</code> ,可以在 <code>2</code> 之前添加 <code>'+'</code> ,在 <code>1</code> 之前添加 <code>'-'</code> ,然后串联起来得到表达式 <code>"+2-1"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回可以通过上述方法构造的、运算结果等于 <code>target</code> 的不同 <strong>表达式</strong> 的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1,1,1], target = 3
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>一共有 5 种方法让最终目标和为 3 。
|
||||
-1 + 1 + 1 + 1 + 1 = 3
|
||||
+1 - 1 + 1 + 1 + 1 = 3
|
||||
+1 + 1 - 1 + 1 + 1 = 3
|
||||
+1 + 1 + 1 - 1 + 1 = 3
|
||||
+1 + 1 + 1 + 1 - 1 = 3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1], target = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 20</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
<li><code>0 <= sum(nums[i]) <= 1000</code></li>
|
||||
<li><code>-1000 <= target <= 1000</code></li>
|
||||
</ul>
|
40
算法题(国内版)/problem (Chinese)/相对名次 [relative-ranks].html
Normal file
40
算法题(国内版)/problem (Chinese)/相对名次 [relative-ranks].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>score</code> ,其中 <code>score[i]</code> 是第 <code>i</code> 位运动员在比赛中的得分。所有得分都 <strong>互不相同</strong> 。</p>
|
||||
|
||||
<p>运动员将根据得分 <strong>决定名次</strong> ,其中名次第 <code>1</code> 的运动员得分最高,名次第 <code>2</code> 的运动员得分第 <code>2</code> 高,依此类推。运动员的名次决定了他们的获奖情况:</p>
|
||||
|
||||
<ul>
|
||||
<li>名次第 <code>1</code> 的运动员获金牌 <code>"Gold Medal"</code> 。</li>
|
||||
<li>名次第 <code>2</code> 的运动员获银牌 <code>"Silver Medal"</code> 。</li>
|
||||
<li>名次第 <code>3</code> 的运动员获铜牌 <code>"Bronze Medal"</code> 。</li>
|
||||
<li>从名次第 <code>4</code> 到第 <code>n</code> 的运动员,只能获得他们的名次编号(即,名次第 <code>x</code> 的运动员获得编号 <code>"x"</code>)。</li>
|
||||
</ul>
|
||||
|
||||
<p>使用长度为 <code>n</code> 的数组 <code>answer</code> 返回获奖,其中 <code>answer[i]</code> 是第 <code>i</code> 位运动员的获奖情况。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>score = [5,4,3,2,1]
|
||||
<strong>输出:</strong>["Gold Medal","Silver Medal","Bronze Medal","4","5"]
|
||||
<strong>解释:</strong>名次为 [1<sup>st</sup>, 2<sup>nd</sup>, 3<sup>rd</sup>, 4<sup>th</sup>, 5<sup>th</sup>] 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>score = [10,3,8,9,4]
|
||||
<strong>输出:</strong>["Gold Medal","5","Bronze Medal","Silver Medal","4"]
|
||||
<strong>解释:</strong>名次为 [1<sup>st</sup>, 5<sup>th</sup>, 3<sup>rd</sup>, 2<sup>nd</sup>, 4<sup>th</sup>] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == score.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= score[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>score</code> 中的所有值 <strong>互不相同</strong></li>
|
||||
</ul>
|
40
算法题(国内版)/problem (Chinese)/省份数量 [number-of-provinces].html
Normal file
40
算法题(国内版)/problem (Chinese)/省份数量 [number-of-provinces].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p>有 <code>n</code> 个城市,其中一些彼此相连,另一些没有相连。如果城市 <code>a</code> 与城市 <code>b</code> 直接相连,且城市 <code>b</code> 与城市 <code>c</code> 直接相连,那么城市 <code>a</code> 与城市 <code>c</code> 间接相连。</p>
|
||||
|
||||
<p><strong>省份</strong> 是一组直接或间接相连的城市,组内不含其他没有相连的城市。</p>
|
||||
|
||||
<p>给你一个 <code>n x n</code> 的矩阵 <code>isConnected</code> ,其中 <code>isConnected[i][j] = 1</code> 表示第 <code>i</code> 个城市和第 <code>j</code> 个城市直接相连,而 <code>isConnected[i][j] = 0</code> 表示二者不直接相连。</p>
|
||||
|
||||
<p>返回矩阵中 <strong>省份</strong> 的数量。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg" style="width: 222px; height: 142px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>isConnected = [[1,1,0],[1,1,0],[0,0,1]]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg" style="width: 222px; height: 142px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>isConnected = [[1,0,0],[0,1,0],[0,0,1]]
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>n == isConnected.length</code></li>
|
||||
<li><code>n == isConnected[i].length</code></li>
|
||||
<li><code>isConnected[i][j]</code> 为 <code>1</code> 或 <code>0</code></li>
|
||||
<li><code>isConnected[i][i] == 1</code></li>
|
||||
<li><code>isConnected[i][j] == isConnected[j][i]</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
75
算法题(国内版)/problem (Chinese)/祖玛游戏 [zuma-game].html
Normal file
75
算法题(国内版)/problem (Chinese)/祖玛游戏 [zuma-game].html
Normal file
@@ -0,0 +1,75 @@
|
||||
<p>你正在参与祖玛游戏的一个变种。</p>
|
||||
|
||||
<p>在这个祖玛游戏变体中,桌面上有 <strong>一排</strong> 彩球,每个球的颜色可能是:红色 <code>'R'</code>、黄色 <code>'Y'</code>、蓝色 <code>'B'</code>、绿色 <code>'G'</code> 或白色 <code>'W'</code> 。你的手中也有一些彩球。</p>
|
||||
|
||||
<p>你的目标是 <strong>清空</strong> 桌面上所有的球。每一回合:</p>
|
||||
|
||||
<ul>
|
||||
<li>从你手上的彩球中选出 <strong>任意一颗</strong> ,然后将其插入桌面上那一排球中:两球之间或这一排球的任一端。</li>
|
||||
<li>接着,如果有出现 <strong>三个或者三个以上</strong> 且 <strong>颜色相同</strong> 的球相连的话,就把它们移除掉。
|
||||
<ul>
|
||||
<li>如果这种移除操作同样导致出现三个或者三个以上且颜色相同的球相连,则可以继续移除这些球,直到不再满足移除条件。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>如果桌面上所有球都被移除,则认为你赢得本场游戏。</li>
|
||||
<li>重复这个过程,直到你赢了游戏或者手中没有更多的球。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你一个字符串 <code>board</code> ,表示桌面上最开始的那排球。另给你一个字符串 <code>hand</code> ,表示手里的彩球。请你按上述操作步骤移除掉桌上所有球,计算并返回所需的 <strong>最少</strong> 球数。如果不能移除桌上所有的球,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>board = "WRRBBW", hand = "RB"
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>无法移除桌面上的所有球。可以得到的最好局面是:
|
||||
- 插入一个 'R' ,使桌面变为 WRR<em><strong>R</strong></em>BBW 。W<em><strong>RRR</strong></em>BBW -> WBBW
|
||||
- 插入一个 'B' ,使桌面变为 WBB<em><strong>B</strong></em>W 。W<em><strong>BBB</strong></em>W -> WW
|
||||
桌面上还剩着球,没有其他球可以插入。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>board = "WWRRBBWW", hand = "WRBRW"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>要想清空桌面上的球,可以按下述步骤:
|
||||
- 插入一个 'R' ,使桌面变为 WWRR<strong><em>R</em></strong>BBWW 。WW<em><strong>RRR</strong></em>BBWW -> WWBBWW
|
||||
- 插入一个 'B' ,使桌面变为 WWBB<em><strong>B</strong></em>WW 。WW<em><strong>BBB</strong></em>WW -> <em><strong>WWWW</strong></em> -> empty
|
||||
只需从手中出 2 个球就可以清空桌面。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>board = "G", hand = "GGGGG"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>要想清空桌面上的球,可以按下述步骤:
|
||||
- 插入一个 'G' ,使桌面变为 G<em><strong>G</strong></em> 。
|
||||
- 插入一个 'G' ,使桌面变为 GG<em><strong>G</strong></em> 。<em><strong>GGG</strong></em> -> empty
|
||||
只需从手中出 2 个球就可以清空桌面。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>board = "RBYYBBRRB", hand = "YRBGB"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>要想清空桌面上的球,可以按下述步骤:
|
||||
- 插入一个 'Y' ,使桌面变为 RBYY<em><strong>Y</strong></em>BBRRB 。RB<em><strong>YYY</strong></em>BBRRB -> R<em><strong>BBB</strong></em>RRB -> <em><strong>RRR</strong></em>B -> B
|
||||
- 插入一个 'B' ,使桌面变为 B<em><strong>B</strong></em> 。
|
||||
- 插入一个 'B' ,使桌面变为 BB<em><strong>B</strong></em> 。<em><strong>BBB</strong></em> -> empty
|
||||
只需从手中出 3 个球就可以清空桌面。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= board.length <= 16</code></li>
|
||||
<li><code>1 <= hand.length <= 5</code></li>
|
||||
<li><code>board</code> 和 <code>hand</code> 由字符 <code>'R'</code>、<code>'Y'</code>、<code>'B'</code>、<code>'G'</code> 和 <code>'W'</code> 组成</li>
|
||||
<li>桌面上一开始的球中,不会有三个及三个以上颜色相同且连着的球</li>
|
||||
</ul>
|
34
算法题(国内版)/problem (Chinese)/神奇字符串 [magical-string].html
Normal file
34
算法题(国内版)/problem (Chinese)/神奇字符串 [magical-string].html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>神奇字符串 <code>s</code> 仅由 <code>'1'</code> 和 <code>'2'</code> 组成,并需要遵守下面的规则:</p>
|
||||
|
||||
<ul>
|
||||
<li>神奇字符串 s 的神奇之处在于,串联字符串中 <code>'1'</code> 和 <code>'2'</code> 的连续出现次数可以生成该字符串。</li>
|
||||
</ul>
|
||||
|
||||
<p><code>s</code> 的前几个元素是 <code>s = "1221121221221121122……"</code> 。如果将 <code>s</code> 中连续的若干 <code>1</code> 和 <code>2</code> 进行分组,可以得到 <code>"1 22 11 2 1 22 1 22 11 2 11 22 ......"</code> 。每组中 <code>1</code> 或者 <code>2</code> 的出现次数分别是 <code>"1 2 2 1 1 2 1 2 2 1 2 2 ......"</code> 。上面的出现次数正是 <code>s</code> 自身。</p>
|
||||
|
||||
<p>给你一个整数 <code>n</code> ,返回在神奇字符串 <code>s</code> 的前 <code>n</code> 个数字中 <code>1</code> 的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 6
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>神奇字符串 s 的前 6 个元素是 “<code>122112</code>”,它包含三个 1,因此返回 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
43
算法题(国内版)/problem (Chinese)/移除盒子 [remove-boxes].html
Normal file
43
算法题(国内版)/problem (Chinese)/移除盒子 [remove-boxes].html
Normal file
@@ -0,0 +1,43 @@
|
||||
<p>给出一些不同颜色的盒子<meta charset="UTF-8" /> <code>boxes</code> ,盒子的颜色由不同的正数表示。</p>
|
||||
|
||||
<p>你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止。每一轮你可以移除具有相同颜色的连续 <code>k</code> 个盒子(<code>k >= 1</code>),这样一轮之后你将得到 <code>k * k</code> 个积分。</p>
|
||||
|
||||
<p>返回 <em>你能获得的最大积分和</em> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>boxes = [1,3,2,2,2,3,4,3,1]
|
||||
<strong>输出:</strong>23
|
||||
<strong>解释:</strong>
|
||||
[1, 3, 2, 2, 2, 3, 4, 3, 1]
|
||||
----> [1, 3, 3, 4, 3, 1] (3*3=9 分)
|
||||
----> [1, 3, 3, 3, 1] (1*1=1 分)
|
||||
----> [1, 1] (3*3=9 分)
|
||||
----> [] (2*2=4 分)
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>boxes = [1,1,1]
|
||||
<strong>输出:</strong>9
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>boxes = [1]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= boxes.length <= 100</code></li>
|
||||
<li><code>1 <= boxes[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>Bob 站在单元格 <code>(0, 0)</code> ,想要前往目的地 <code>destination</code> :<code>(row, column)</code> 。他只能向 <strong>右</strong> 或向 <strong>下</strong> 走。你可以为 Bob 提供导航 <strong>指令</strong> 来帮助他到达目的地 <code>destination</code> 。</p>
|
||||
|
||||
<p><strong>指令</strong> 用字符串表示,其中每个字符:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'H'</code> ,意味着水平向右移动</li>
|
||||
<li><code>'V'</code> ,意味着竖直向下移动</li>
|
||||
</ul>
|
||||
|
||||
<p>能够为 Bob 导航到目的地 <code>destination</code> 的指令可以有多种,例如,如果目的地 <code>destination</code> 是 <code>(2, 3)</code>,<code>"HHHVV"</code> 和 <code>"HVHVH"</code> 都是有效<strong> 指令</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p>然而,Bob 很挑剔。因为他的幸运数字是 <code>k</code>,他想要遵循 <strong>按字典序排列后的第 <code>k</code> 条最小指令 </strong>的导航前往目的地 <code>destination</code> 。<code>k</code> 的编号 <strong>从 1 开始</strong> 。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>destination</code> 和一个整数 <code>k</code> ,请你返回可以为<em> </em>Bob<em> </em>提供前往目的地 <code>destination</code> 导航的<strong> 按字典序排列后的第 <code>k</code> 条最小指令 </strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/11/01/ex1.png" style="width: 300px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>destination = [2,3], k = 1
|
||||
<strong>输出:</strong>"HHHVV"
|
||||
<strong>解释:</strong>能前往 (2, 3) 的所有导航指令 <strong>按字典序排列后</strong> 如下所示:
|
||||
["HHHVV", "HHVHV", "HHVVH", "HVHHV", "HVHVH", "HVVHH", "VHHHV", "VHHVH", "VHVHH", "VVHHH"].
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/11/01/ex2.png" style="width: 300px; height: 229px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>destination = [2,3], k = 2
|
||||
<strong>输出:</strong>"HHVHV"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/11/01/ex3.png" style="width: 300px; height: 229px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>destination = [2,3], k = 3
|
||||
<strong>输出:</strong>"HHVVH"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>destination.length == 2</code></li>
|
||||
<li><code>1 <= row, column <= 15</code></li>
|
||||
<li><code>1 <= k <= nCr(row + column, row)</code>,其中 <code>nCr(a, b)</code> 表示组合数,即从 <code>a</code> 个物品中选 <code>b</code> 个物品的不同方案数。</li>
|
||||
</ul>
|
39
算法题(国内版)/problem (Chinese)/第三大的数 [third-maximum-number].html
Normal file
39
算法题(国内版)/problem (Chinese)/第三大的数 [third-maximum-number].html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>给你一个非空数组,返回此数组中 <strong>第三大的数</strong> 。如果不存在,则返回数组中最大的数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>[3, 2, 1]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>第三大的数是 1 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>[1, 2]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>第三大的数不存在, 所以返回最大的数 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>[2, 2, 3, 1]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>注意,要求返回第三大的数,是指在所有不同数字中排第三大的数。
|
||||
此例中存在两个值为 2 的数,它们都排第二。在所有不同数字中排第三大的数为 1 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你能设计一个时间复杂度 <code>O(n)</code> 的解决方案吗?</p>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,返回 <code>nums</code> 中所有 <strong>等差子序列</strong> 的数目。</p>
|
||||
|
||||
<p>如果一个序列中 <strong>至少有三个元素</strong> ,并且任意两个相邻元素之差相同,则称该序列为等差序列。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>[1, 3, 5, 7, 9]</code>、<code>[7, 7, 7, 7]</code> 和 <code>[3, -1, -5, -9]</code> 都是等差序列。</li>
|
||||
<li>再例如,<code>[1, 1, 2, 5, 7]</code> 不是等差序列。</li>
|
||||
</ul>
|
||||
|
||||
<p>数组中的子序列是从数组中删除一些元素(也可能不删除)得到的一个序列。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>[2,5,10]</code> 是 <code>[1,2,1,<em><strong>2</strong></em>,4,1,<strong><em>5</em></strong>,<em><strong>10</strong></em>]</code> 的一个子序列。</li>
|
||||
</ul>
|
||||
|
||||
<p>题目数据保证答案是一个 <strong>32-bit</strong> 整数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [2,4,6,8,10]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>所有的等差子序列为:
|
||||
[2,4,6]
|
||||
[4,6,8]
|
||||
[6,8,10]
|
||||
[2,4,6,8]
|
||||
[4,6,8,10]
|
||||
[2,4,6,8,10]
|
||||
[2,6,10]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [7,7,7,7,7]
|
||||
<strong>输出:</strong>16
|
||||
<strong>解释:</strong>数组中的任意子序列都是等差子序列。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>定义 <code>str = [s, n]</code> 表示 <code>str</code> 由 <code>n</code> 个字符串 <code>s</code> 连接构成。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,<code>str == ["abc", 3] =="abcabcabc"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果可以从 <code>s2</code><sub> </sub>中删除某些字符使其变为 <code>s1</code>,则称字符串 <code>s1</code><sub> </sub>可以从字符串 <code>s2</code> 获得。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,根据定义,<code>s1 = "abc"</code> 可以从 <code>s2 = "ab<em><strong>dbe</strong></em>c"</code> 获得,仅需要删除加粗且用斜体标识的字符。</li>
|
||||
</ul>
|
||||
|
||||
<p>现在给你两个字符串 <code>s1</code> 和 <code>s2</code> 和两个整数 <code>n1</code> 和 <code>n2</code> 。由此构造得到两个字符串,其中 <code>str1 = [s1, n1]</code>、<code>str2 = [s2, n2]</code> 。</p>
|
||||
|
||||
<p>请你找出一个最大整数 <code>m</code> ,以满足 <code>str = [str2, m]</code> 可以从 <code>str1</code> 获得。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s1 = "acb", n1 = 4, s2 = "ab", n2 = 2
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length <= 100</code></li>
|
||||
<li><code>s1</code> 和 <code>s2</code> 由小写英文字母组成</li>
|
||||
<li><code>1 <= n1, n2 <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
24
算法题(国内版)/problem (Chinese)/翻转对 [reverse-pairs].html
Normal file
24
算法题(国内版)/problem (Chinese)/翻转对 [reverse-pairs].html
Normal file
@@ -0,0 +1,24 @@
|
||||
<p>给定一个数组 <code>nums</code> ,如果 <code>i < j</code> 且 <code>nums[i] > 2*nums[j]</code> 我们就将 <code>(i, j)</code> 称作一个<strong><em>重要翻转对</em></strong>。</p>
|
||||
|
||||
<p>你需要返回给定数组中的重要翻转对的数量。</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>: [1,3,2,3,1]
|
||||
<strong>输出</strong>: 2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入</strong>: [2,4,3,5,1]
|
||||
<strong>输出</strong>: 3
|
||||
</pre>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>给定数组的长度不会超过<code>50000</code>。</li>
|
||||
<li>输入数组中的所有数字都在32位整数的表示范围内。</li>
|
||||
</ol>
|
47
算法题(国内版)/problem (Chinese)/自由之路 [freedom-trail].html
Normal file
47
算法题(国内版)/problem (Chinese)/自由之路 [freedom-trail].html
Normal file
@@ -0,0 +1,47 @@
|
||||
<p>电子游戏“辐射4”中,任务 <strong>“通向自由”</strong> 要求玩家到达名为 “<strong>Freedom Trail Ring”</strong> 的金属表盘,并使用表盘拼写特定关键词才能开门。</p>
|
||||
|
||||
<p>给定一个字符串 <code>ring</code> ,表示刻在外环上的编码;给定另一个字符串 <code>key</code> ,表示需要拼写的关键词。您需要算出能够拼写关键词中所有字符的<strong>最少</strong>步数。</p>
|
||||
|
||||
<p>最初,<strong>ring </strong>的第一个字符与 <code>12:00</code> 方向对齐。您需要顺时针或逆时针旋转 <code>ring</code> 以使 <strong>key </strong>的一个字符在 <code>12:00</code> 方向对齐,然后按下中心按钮,以此逐个拼写完 <strong><code>key</code> </strong>中的所有字符。</p>
|
||||
|
||||
<p>旋转 <code>ring</code><strong> </strong>拼出 key 字符 <code>key[i]</code><strong> </strong>的阶段中:</p>
|
||||
|
||||
<ol>
|
||||
<li>您可以将 <strong>ring </strong>顺时针或逆时针旋转 <strong>一个位置 </strong>,计为1步。旋转的最终目的是将字符串 <strong><code>ring</code> </strong>的一个字符与 <code>12:00</code> 方向对齐,并且这个字符必须等于字符 <strong><code>key[i]</code> 。</strong></li>
|
||||
<li>如果字符 <strong><code>key[i]</code> </strong>已经对齐到12:00方向,您需要按下中心按钮进行拼写,这也将算作 <strong>1 步</strong>。按完之后,您可以开始拼写 <strong>key </strong>的下一个字符(下一阶段), 直至完成所有拼写。</li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2018/10/22/ring.jpg" style="height: 450px; width: 450px;" /></p>
|
||||
|
||||
<center> </center>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> ring = "godding", key = "gd"
|
||||
<strong>输出:</strong> 4
|
||||
<strong>解释:</strong>
|
||||
对于 key 的第一个字符 'g',已经在正确的位置, 我们只需要1步来拼写这个字符。
|
||||
对于 key 的第二个字符 'd',我们需要逆时针旋转 ring "godding" 2步使它变成 "ddinggo"。
|
||||
当然, 我们还需要1步进行拼写。
|
||||
因此最终的输出是 4。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> ring = "godding", key = "godding"
|
||||
<strong>输出:</strong> 13
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ring.length, key.length <= 100</code></li>
|
||||
<li><code>ring</code> 和 <code>key</code> 只包含小写英文字母</li>
|
||||
<li><strong>保证</strong> 字符串 <code>key</code> 一定可以由字符串 <code>ring</code> 旋转拼出</li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>假设有 <code>n</code><strong> </strong>台超级洗衣机放在同一排上。开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的。</p>
|
||||
|
||||
<p>在每一步操作中,你可以选择任意 <code>m</code> (<code>1 <= m <= n</code>) 台洗衣机,与此同时将每台洗衣机的一件衣服送到相邻的一台洗衣机。</p>
|
||||
|
||||
<p>给定一个整数数组 <code>machines</code> 代表从左至右每台洗衣机中的衣物数量,请给出能让所有洗衣机中剩下的衣物的数量相等的 <strong>最少的操作步数 </strong>。如果不能使每台洗衣机中衣物的数量相等,则返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>machines = [1,0,5]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
第一步: 1 0 <-- 5 => 1 1 4
|
||||
第二步: 1 <-- 1 <-- 4 => 2 1 3
|
||||
第三步: 2 1 <-- 3 => 2 2 2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>machines = [0,3,0]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
第一步: 0 <-- 3 0 => 1 2 0
|
||||
第二步: 1 2 --> 0 => 1 1 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>machines = [0,2,0]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>
|
||||
不可能让所有三个洗衣机同时剩下相同数量的衣物。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == machines.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= machines[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
32
算法题(国内版)/problem (Chinese)/路径总和 III [path-sum-iii].html
Normal file
32
算法题(国内版)/problem (Chinese)/路径总和 III [path-sum-iii].html
Normal file
@@ -0,0 +1,32 @@
|
||||
<p>给定一个二叉树的根节点 <code>root</code> ,和一个整数 <code>targetSum</code> ,求该二叉树里节点值之和等于 <code>targetSum</code> 的 <strong>路径</strong> 的数目。</p>
|
||||
|
||||
<p><strong>路径</strong> 不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg" style="width: 452px; " /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>和等于 8 的路径有 3 条,如图所示。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树的节点个数的范围是 <code>[0,1000]</code></li>
|
||||
<li><meta charset="UTF-8" /><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code> </li>
|
||||
<li><code>-1000 <= targetSum <= 1000</code> </li>
|
||||
</ul>
|
32
算法题(国内版)/problem (Chinese)/连接词 [concatenated-words].html
Normal file
32
算法题(国内版)/problem (Chinese)/连接词 [concatenated-words].html
Normal file
@@ -0,0 +1,32 @@
|
||||
<p>给你一个 <strong>不含重复 </strong>单词的字符串数组 <code>words</code> ,请你找出并返回 <code>words</code> 中的所有 <strong>连接词</strong> 。</p>
|
||||
|
||||
<p><strong>连接词</strong> 定义为:一个完全由给定数组中的至少两个较短单词组成的字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
|
||||
<strong>输出:</strong>["catsdogcats","dogcatsdog","ratcatdogcat"]
|
||||
<strong>解释:</strong>"catsdogcats" 由 "cats", "dog" 和 "cats" 组成;
|
||||
"dogcatsdog" 由 "dog", "cats" 和 "dog" 组成;
|
||||
"ratcatdogcat" 由 "rat", "cat", "dog" 和 "cat" 组成。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["cat","dog","catdog"]
|
||||
<strong>输出:</strong>["catdog"]</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= words[i].length <= 1000</code></li>
|
||||
<li><code>words[i]</code> 仅由小写字母组成</li>
|
||||
<li><code>0 <= sum(words[i].length) <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
26
算法题(国内版)/problem (Chinese)/连续数组 [contiguous-array].html
Normal file
26
算法题(国内版)/problem (Chinese)/连续数组 [contiguous-array].html
Normal file
@@ -0,0 +1,26 @@
|
||||
<p>给定一个二进制数组 <code>nums</code> , 找到含有相同数量的 <code>0</code> 和 <code>1</code> 的最长连续子数组,并返回该子数组的长度。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [0,1]
|
||||
<strong>输出:</strong> 2
|
||||
<strong>说明:</strong> [0, 1] 是具有相同数量 0 和 1 的最长连续子数组。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [0,1,0]
|
||||
<strong>输出:</strong> 2
|
||||
<strong>说明:</strong> [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>nums[i]</code> 不是 <code>0</code> 就是 <code>1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,编写一个函数来判断该数组是否含有同时满足下述条件的连续子数组:</p>
|
||||
|
||||
<ul>
|
||||
<li>子数组大小 <strong>至少为 2</strong> ,且</li>
|
||||
<li>子数组元素总和为 <code>k</code> 的倍数。</li>
|
||||
</ul>
|
||||
|
||||
<p>如果存在,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p>如果存在一个整数 <code>n</code> ,令整数 <code>x</code> 符合 <code>x = n * k</code> ,则称 <code>x</code> 是 <code>k</code> 的一个倍数。<code>0</code> 始终视为 <code>k</code> 的一个倍数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [23<u>,2,4</u>,6,7], k = 6
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>[2,4] 是一个大小为 2 的子数组,并且和为 6 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [<u>23,2,6,4,7</u>], k = 6
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>[23, 2, 6, 4, 7] 是大小为 5 的子数组,并且和为 42 。
|
||||
42 是 6 的倍数,因为 42 = 7 * 6 且 7 是一个整数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [23,2,6,4,7], k = 13
|
||||
<strong>输出:</strong>false
|
||||
</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 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li>
|
||||
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,找出并返回所有该数组中不同的递增子序列,递增子序列中 <strong>至少有两个元素</strong> 。你可以按 <strong>任意顺序</strong> 返回答案。</p>
|
||||
|
||||
<p>数组中可能含有重复元素,如出现两个整数相等,也可以视作递增序列的一种特殊情况。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,6,7,7]
|
||||
<strong>输出:</strong>[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [4,4,3,2,1]
|
||||
<strong>输出:</strong>[[4,4]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 15</code></li>
|
||||
<li><code>-100 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,30 @@
|
||||
<p>给你一个字符串 <code>s</code> 和一个字符串数组 <code>dictionary</code> ,找出并返回 <code>dictionary</code> 中最长的字符串,该字符串可以通过删除 <code>s</code> 中的某些字符得到。</p>
|
||||
|
||||
<p>如果答案不止一个,返回长度最长且字母序最小的字符串。如果答案不存在,则返回空字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abpcplea", dictionary = ["ale","apple","monkey","plea"]
|
||||
<strong>输出:</strong>"apple"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abpcplea", dictionary = ["a","b","c"]
|
||||
<strong>输出:</strong>"a"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>1 <= dictionary.length <= 1000</code></li>
|
||||
<li><code>1 <= dictionary[i].length <= 1000</code></li>
|
||||
<li><code>s</code> 和 <code>dictionary[i]</code> 仅由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,37 @@
|
||||
<p>给定一个非空的字符串<meta charset="UTF-8" /> <code>s</code> ,检查是否可以通过由它的一个子串重复多次构成。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> s = "abab"
|
||||
<strong>输出:</strong> true
|
||||
<strong>解释:</strong> 可由子串 "ab" 重复两次构成。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> s = "aba"
|
||||
<strong>输出:</strong> false
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> s = "abcabcabcabc"
|
||||
<strong>输出:</strong> true
|
||||
<strong>解释:</strong> 可由子串 "abc" 重复四次构成。 (或子串 "abcabc" 重复两次构成。)
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>提示:</b></p>
|
||||
|
||||
<p><meta charset="UTF-8" /></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> 由小写英文字母组成</li>
|
||||
</ul>
|
44
算法题(国内版)/problem (Chinese)/键盘行 [keyboard-row].html
Normal file
44
算法题(国内版)/problem (Chinese)/键盘行 [keyboard-row].html
Normal file
@@ -0,0 +1,44 @@
|
||||
<p>给你一个字符串数组 <code>words</code> ,只返回可以使用在 <strong>美式键盘</strong> 同一行的字母打印出来的单词。键盘如下图所示。</p>
|
||||
|
||||
<p><strong>美式键盘</strong> 中:</p>
|
||||
|
||||
<ul>
|
||||
<li>第一行由字符 <code>"qwertyuiop"</code> 组成。</li>
|
||||
<li>第二行由字符 <code>"asdfghjkl"</code> 组成。</li>
|
||||
<li>第三行由字符 <code>"zxcvbnm"</code> 组成。</li>
|
||||
</ul>
|
||||
|
||||
<p><img alt="American keyboard" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/keyboard.png" style="width: 100%; max-width: 600px" /></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["Hello","Alaska","Dad","Peace"]
|
||||
<strong>输出:</strong>["Alaska","Dad"]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["omk"]
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>words = ["adsdf","sfd"]
|
||||
<strong>输出:</strong>["adsdf","sfd"]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 20</code></li>
|
||||
<li><code>1 <= words[i].length <= 100</code></li>
|
||||
<li><code>words[i]</code> 由英文字母(小写和大写字母)组成</li>
|
||||
</ul>
|
50
算法题(国内版)/problem (Chinese)/零钱兑换 II [coin-change-2].html
Normal file
50
算法题(国内版)/problem (Chinese)/零钱兑换 II [coin-change-2].html
Normal file
@@ -0,0 +1,50 @@
|
||||
<p>给你一个整数数组 <code>coins</code> 表示不同面额的硬币,另给一个整数 <code>amount</code> 表示总金额。</p>
|
||||
|
||||
<p>请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 <code>0</code> 。</p>
|
||||
|
||||
<p>假设每一种面额的硬币有无限个。 </p>
|
||||
|
||||
<p>题目数据保证结果符合 32 位带符号整数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>amount = 5, coins = [1, 2, 5]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>有四种方式可以凑成总金额:
|
||||
5=5
|
||||
5=2+2+1
|
||||
5=2+1+1+1
|
||||
5=1+1+1+1+1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>amount = 3, coins = [2]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>只用面额 2 的硬币不能凑成总金额 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>amount = 10, coins = [10]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= coins.length <= 300</code></li>
|
||||
<li><code>1 <= coins[i] <= 5000</code></li>
|
||||
<li><code>coins</code> 中的所有值 <strong>互不相同</strong></li>
|
||||
<li><code>0 <= amount <= 5000</code></li>
|
||||
</ul>
|
34
算法题(国内版)/problem (Chinese)/预测赢家 [predict-the-winner].html
Normal file
34
算法题(国内版)/problem (Chinese)/预测赢家 [predict-the-winner].html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 。玩家 1 和玩家 2 基于这个数组设计了一个游戏。</p>
|
||||
|
||||
<p>玩家 1 和玩家 2 轮流进行自己的回合,玩家 1 先手。开始时,两个玩家的初始分值都是 <code>0</code> 。每一回合,玩家从数组的任意一端取一个数字(即,<code>nums[0]</code> 或 <code>nums[nums.length - 1]</code>),取到的数字将会从数组中移除(数组长度减 <code>1</code> )。玩家选中的数字将会加到他的得分上。当数组中没有剩余数字可取时,游戏结束。</p>
|
||||
|
||||
<p>如果玩家 1 能成为赢家,返回 <code>true</code> 。如果两个玩家得分相等,同样认为玩家 1 是游戏的赢家,也返回 <code>true</code> 。你可以假设每个玩家的玩法都会使他的分数最大化。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,5,2]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>一开始,玩家 1 可以从 1 和 2 中进行选择。
|
||||
如果他选择 2(或者 1 ),那么玩家 2 可以从 1(或者 2 )和 5 中进行选择。如果玩家 2 选择了 5 ,那么玩家 1 则只剩下 1(或者 2 )可选。
|
||||
所以,玩家 1 的最终分数为 1 + 2 = 3,而玩家 2 为 5 。
|
||||
因此,玩家 1 永远不会成为赢家,返回 false 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,5,233,7]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>玩家 1 一开始选择 1 。然后玩家 2 必须从 5 和 7 中进行选择。无论玩家 2 选择了哪个,玩家 1 都可以选择 233 。
|
||||
最终,玩家 1(234 分)比玩家 2(12 分)获得更多的分数,所以返回 true,表示玩家 1 可以成为赢家。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 20</code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>7</sup></code></li>
|
||||
</ul>
|
47
算法题(国内版)/problem (Chinese)/验证IP地址 [validate-ip-address].html
Normal file
47
算法题(国内版)/problem (Chinese)/验证IP地址 [validate-ip-address].html
Normal file
@@ -0,0 +1,47 @@
|
||||
<p>给定一个字符串 <code>queryIP</code>。如果是有效的 IPv4 地址,返回 <code>"IPv4"</code> ;如果是有效的 IPv6 地址,返回 <code>"IPv6"</code> ;如果不是上述类型的 IP 地址,返回 <code>"Neither"</code> 。</p>
|
||||
|
||||
<p><strong>有效的IPv4地址</strong> 是 <code>“x1.x2.x3.x4”</code> 形式的IP地址。 其中 <code>0 <= x<sub>i</sub> <= 255</code> 且 <code>x<sub>i</sub></code> <strong>不能包含</strong> 前导零。例如: <code>“192.168.1.1”</code> 、 <code>“192.168.1.0”</code> 为有效IPv4地址, <code>“192.168.01.1”</code> 为无效IPv4地址; <code>“192.168.1.00”</code> 、 <code>“192.168@1.1”</code> 为无效IPv4地址。</p>
|
||||
|
||||
<p><strong>一个有效的IPv6地址 </strong>是一个格式为<code>“x1:x2:x3:x4:x5:x6:x7:x8”</code> 的IP地址,其中:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= x<sub>i</sub>.length <= 4</code></li>
|
||||
<li><code>x<sub>i</sub></code> 是一个 <strong>十六进制字符串</strong> ,可以包含数字、小写英文字母( <code>'a'</code> 到 <code>'f'</code> )和大写英文字母( <code>'A'</code> 到 <code>'F'</code> )。</li>
|
||||
<li>在 <code>x<sub>i</sub></code> 中允许前导零。</li>
|
||||
</ul>
|
||||
|
||||
<p>例如 <code>"2001:0db8:85a3:0000:0000:8a2e:0370:7334"</code> 和 <code>"2001:db8:85a3:0:0:8A2E:0370:7334"</code> 是有效的 IPv6 地址,而 <code>"2001:0db8:85a3::8A2E:037j:7334"</code> 和 <code>"02001:0db8:85a3:0000:0000:8a2e:0370:7334"</code> 是无效的 IPv6 地址。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>queryIP = "172.16.254.1"
|
||||
<strong>输出:</strong>"IPv4"
|
||||
<strong>解释:</strong>有效的 IPv4 地址,返回 "IPv4"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
|
||||
<strong>输出:</strong>"IPv6"
|
||||
<strong>解释:</strong>有效的 IPv6 地址,返回 "IPv6"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>queryIP = "256.256.256.256"
|
||||
<strong>输出:</strong>"Neither"
|
||||
<strong>解释:</strong>既不是 IPv4 地址,又不是 IPv6 地址
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>queryIP</code> 仅由英文字母,数字,字符 <code>'.'</code> 和 <code>':'</code> 组成。</li>
|
||||
</ul>
|
Reference in New Issue
Block a user