mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<p><code>Insurance</code> 表:</p>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<pre>
|
||||
+-------------+-------+
|
||||
| Column Name | Type |
|
||||
+-------------+-------+
|
||||
| pid | int |
|
||||
| tiv_2015 | float |
|
||||
| tiv_2016 | float |
|
||||
| lat | float |
|
||||
| lon | float |
|
||||
+-------------+-------+
|
||||
pid 是这张表的主键(具有唯一值的列)。
|
||||
表中的每一行都包含一条保险信息,其中:
|
||||
pid 是投保人的投保编号。
|
||||
tiv_2015 是该投保人在 2015 年的总投保金额,tiv_2016 是该投保人在 2016 年的总投保金额。
|
||||
lat 是投保人所在城市的纬度。题目数据确保 lat 不为空。
|
||||
lon 是投保人所在城市的经度。题目数据确保 lon 不为空。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写解决方案报告 2016 年 (<code>tiv_2016</code>) 所有满足下述条件的投保人的投保金额之和:</p>
|
||||
|
||||
<ul>
|
||||
<li>他在 2015 年的投保额 (<code>tiv_2015</code>) 至少跟一个其他投保人在 2015 年的投保额相同。</li>
|
||||
<li>他所在的城市必须与其他投保人都不同(也就是说 (<code>lat, lon</code>) 不能跟其他任何一个投保人完全相同)。</li>
|
||||
</ul>
|
||||
|
||||
<p><code>tiv_2016</code> 四舍五入的 <strong>两位小数</strong> 。</p>
|
||||
|
||||
<p>查询结果格式如下例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Insurance 表:
|
||||
+-----+----------+----------+-----+-----+
|
||||
| pid | tiv_2015 | tiv_2016 | lat | lon |
|
||||
+-----+----------+----------+-----+-----+
|
||||
| 1 | 10 | 5 | 10 | 10 |
|
||||
| 2 | 20 | 20 | 20 | 20 |
|
||||
| 3 | 10 | 30 | 20 | 20 |
|
||||
| 4 | 10 | 40 | 40 | 40 |
|
||||
+-----+----------+----------+-----+-----+
|
||||
<strong>输出:</strong>
|
||||
+----------+
|
||||
| tiv_2016 |
|
||||
+----------+
|
||||
| 45.00 |
|
||||
+----------+
|
||||
<strong>解释:
|
||||
</strong>表中的第一条记录和最后一条记录都满足两个条件。
|
||||
tiv_2015 值为 10 与第三条和第四条记录相同,且其位置是唯一的。
|
||||
|
||||
第二条记录不符合任何一个条件。其 tiv_2015 与其他投保人不同,并且位置与第三条记录相同,这也导致了第三条记录不符合题目要求。
|
||||
因此,结果是第一条记录和最后一条记录的 tiv_2016 之和,即 45 。</pre>
|
||||
</div>
|
||||
</div>
|
@@ -0,0 +1,32 @@
|
||||
<p>逆序对的定义如下:对于数组 <code>nums</code> 的第 <code>i</code> 个和第 <code>j</code> 个元素,如果满足 <code>0 <= i < j < nums.length</code> 且 <code>nums[i] > nums[j]</code>,则其为一个逆序对;否则不是。</p>
|
||||
|
||||
<p>给你两个整数 <code>n</code> 和 <code>k</code>,找出所有包含从 <code>1</code> 到 <code>n</code> 的数字,且恰好拥有 <code>k</code> 个 <strong>逆序对</strong> 的不同的数组的个数。由于答案可能很大,只需要返回对 <code>10<sup>9</sup> + 7</code> 取余的结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, k = 0
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>
|
||||
只有数组 [1,2,3] 包含了从1到3的整数并且正好拥有 0 个逆序对。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3, k = 1
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
数组 [1,3,2] 和 [2,1,3] 都有 1 个逆序对。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
<li><code>0 <= k <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>表: <code>Employees</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+----------+
|
||||
| Column Name | Type |
|
||||
+-------------+----------+
|
||||
| employee_id | int |
|
||||
| name | varchar |
|
||||
| manager_id | int |
|
||||
| salary | int |
|
||||
+-------------+----------+
|
||||
在 SQL 中,employee_id 是这个表的主键。
|
||||
这个表包含了员工,他们的薪水和上级经理的id。
|
||||
有一些员工没有上级经理(其 manager_id 是空值)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>查找这些员工的id,他们的薪水严格少于<code>$30000</code> 并且他们的上级经理已离职。当一个经理离开公司时,他们的信息需要从员工表中删除掉,但是表中的员工的<code>manager_id</code> 这一列还是设置的离职经理的id 。</p>
|
||||
|
||||
<p>返回的结果按照<code>employee_id </code>从小到大排序。</p>
|
||||
|
||||
<p>查询结果如下所示:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Employees table:
|
||||
+-------------+-----------+------------+--------+
|
||||
| employee_id | name | manager_id | salary |
|
||||
+-------------+-----------+------------+--------+
|
||||
| 3 | Mila | 9 | 60301 |
|
||||
| 12 | Antonella | null | 31000 |
|
||||
| 13 | Emery | null | 67084 |
|
||||
| 1 | Kalel | 11 | 21241 |
|
||||
| 9 | Mikaela | null | 50937 |
|
||||
| 11 | Joziah | 6 | 28485 |
|
||||
+-------------+-----------+------------+--------+
|
||||
<strong>输出:</strong>
|
||||
+-------------+
|
||||
| employee_id |
|
||||
+-------------+
|
||||
| 11 |
|
||||
+-------------+
|
||||
|
||||
<strong>解释:</strong>
|
||||
薪水少于 30000 美元的员工有 1 号(Kalel) 和 11号 (Joziah)。
|
||||
Kalel 的上级经理是 11 号员工,他还在公司上班(他是 Joziah )。
|
||||
Joziah 的上级经理是 6 号员工,他已经离职,因为员工表里面已经没有 6 号员工的信息了,它被删除了。
|
||||
</pre>
|
@@ -0,0 +1,33 @@
|
||||
<p>给你 <code>n</code> 个二维平面上的点 <code>points</code> ,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> ,请你返回两点之间内部不包含任何点的 <strong>最宽垂直区域</strong> 的宽度。</p>
|
||||
|
||||
<p><strong>垂直区域</strong> 的定义是固定宽度,而 y 轴上无限延伸的一块区域(也就是高度为无穷大)。 <strong>最宽垂直区域</strong> 为宽度最大的一个垂直区域。</p>
|
||||
|
||||
<p>请注意,垂直区域 <strong>边上</strong> 的点 <strong>不在</strong> 区域内。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/10/31/points3.png" style="width: 276px; height: 371px;" />
|
||||
<pre>
|
||||
<b>输入:</b>points = [[8,7],[9,9],[7,4],[9,7]]
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>红色区域和蓝色区域都是最优区域。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>points = [[3,1],[9,0],[1,0],[1,4],[5,3],[8,8]]
|
||||
<b>输出:</b>3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == points.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>points[i].length == 2</code></li>
|
||||
<li><code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,请你返回子数组内所有元素的乘积严格小于<em> </em><code>k</code> 的连续子数组的数目。
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [10,5,2,6], k = 100
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>8 个乘积小于 100 的子数组分别为:[10]、[5]、[2],、[6]、[10,5]、[5,2]、[2,6]、[5,2,6]。
|
||||
需要注意的是 [10,5,2] 并不是乘积小于 100 的子数组。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3], k = 0
|
||||
<strong>输出:</strong>0</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
<li><code>0 <= k <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,66 @@
|
||||
<p><code>Customer</code> 表:</p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| customer_id | int |
|
||||
| product_key | int |
|
||||
+-------------+---------+
|
||||
该表可能包含重复的行。
|
||||
customer_id 不为 NULL。
|
||||
product_key 是 Product 表的外键(reference 列)。
|
||||
</pre>
|
||||
|
||||
<p><code>Product</code> 表:</p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| product_key | int |
|
||||
+-------------+---------+
|
||||
product_key 是这张表的主键(具有唯一值的列)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写解决方案,报告 <code>Customer</code> 表中购买了 <code>Product</code> 表中所有产品的客户的 id。</p>
|
||||
|
||||
<p>返回结果表 <strong>无顺序要求</strong> 。</p>
|
||||
|
||||
<p>返回结果格式如下所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Customer 表:
|
||||
+-------------+-------------+
|
||||
| customer_id | product_key |
|
||||
+-------------+-------------+
|
||||
| 1 | 5 |
|
||||
| 2 | 6 |
|
||||
| 3 | 5 |
|
||||
| 3 | 6 |
|
||||
| 1 | 6 |
|
||||
+-------------+-------------+
|
||||
Product 表:
|
||||
+-------------+
|
||||
| product_key |
|
||||
+-------------+
|
||||
| 5 |
|
||||
| 6 |
|
||||
+-------------+
|
||||
<strong>输出:</strong>
|
||||
+-------------+
|
||||
| customer_id |
|
||||
+-------------+
|
||||
| 1 |
|
||||
| 3 |
|
||||
+-------------+
|
||||
<strong>解释:</strong>
|
||||
购买了所有产品(5 和 6)的客户的 id 是 1 和 3 。
|
||||
</pre>
|
@@ -0,0 +1,36 @@
|
||||
<p>数组 <code>prices</code> 记录了某芯片近期的交易价格,其中 <code>prices[i]</code> 表示的 <code>i</code> 天该芯片的价格。你只能选择 <strong>某一天</strong> 买入芯片,并选择在 <strong>未来的某一个不同的日子</strong> 卖出该芯片。请设计一个算法计算并返回你从这笔交易中能获取的最大利润。</p>
|
||||
|
||||
<p>如果你不能获取任何利润,返回 0。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>prices = [3, 6, 2, 9, 8, 5]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>在第 3 天(芯片价格 = 2)买入,在第 4 天(芯片价格 = 9)卖出,最大利润 = 9 - 2 = 7。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>prices = [8, 12, 15, 7, 3, 10]
|
||||
<strong>输出:</strong>7
|
||||
<strong>解释:</strong>在第 5 天(芯片价格 = 3)买入,在第 6 天(芯片价格 = 10)卖出,最大利润 = 10 - 3 = 7。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>提示:</b></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= prices.length <= 10^5</code></li>
|
||||
<li><code>0 <= prices[i] <= 10^4</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>本题与主站 121 题相同:<a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/">https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/</a></p>
|
||||
|
||||
<p> </p>
|
49
leetcode-cn/problem (Chinese)/二叉树的序列化与反序列化 [h54YBf].html
Normal file
49
leetcode-cn/problem (Chinese)/二叉树的序列化与反序列化 [h54YBf].html
Normal file
@@ -0,0 +1,49 @@
|
||||
<p>序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。</p>
|
||||
|
||||
<p>请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg" style="width: 442px; height: 324px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,3,null,null,4,5]
|
||||
<strong>输出:</strong>[1,2,3,null,null,4,5]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = []
|
||||
<strong>输出:</strong>[]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1]
|
||||
<strong>输出:</strong>[1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2]
|
||||
<strong>输出:</strong>[1,2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 <a href="/faq/#binary-tree">LeetCode 序列化二叉树的格式</a>。你并非必须采取这种方式,也可以采用其他的方法解决这个问题。</li>
|
||||
<li>树中结点数在范围 <code>[0, 10<sup>4</sup>]</code> 内</li>
|
||||
<li><code>-1000 <= Node.val <= 1000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><meta charset="UTF-8" />注意:本题与主站 297 题相同:<a href="https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/">https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/</a> </p>
|
@@ -0,0 +1,17 @@
|
||||
<p>在股票交易中,如果前一天的股价高于后一天的股价,则可以认为存在一个「交易逆序对」。请设计一个程序,输入一段时间内的股票交易记录 <code>record</code>,返回其中存在的「交易逆序对」总数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>record = [9, 7, 5, 4, 6]
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>交易中的逆序对为 (9, 7), (9, 5), (9, 4), (9, 6), (7, 5), (7, 4), (7, 6), (5, 4)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>限制:</strong></p>
|
||||
|
||||
<p><code>0 <= record.length <= 50000</code></p>
|
@@ -0,0 +1,70 @@
|
||||
<p>销售表 <code>Sales</code>:</p>
|
||||
|
||||
<pre>
|
||||
+-------------+-------+
|
||||
| Column Name | Type |
|
||||
+-------------+-------+
|
||||
| sale_id | int |
|
||||
| product_id | int |
|
||||
| year | int |
|
||||
| quantity | int |
|
||||
| price | int |
|
||||
+-------------+-------+
|
||||
(sale_id, year) 是销售表 Sales 的主键(具有唯一值的列的组合)。
|
||||
product_id 是关联到产品表 Product 的外键(reference 列)。
|
||||
该表的每一行显示 product_id 在某一年的销售情况。
|
||||
注意: price 表示每单位价格。
|
||||
</pre>
|
||||
|
||||
<p>产品表 <code>Product</code>:</p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| product_id | int |
|
||||
| product_name | varchar |
|
||||
+--------------+---------+
|
||||
product_id 是表的主键(具有唯一值的列)。
|
||||
该表的每一行表示每种产品的产品名称。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写解决方案,以获取 <code>Sales</code> 表中所有 <code>sale_id</code> 对应的 <code>product_name</code> 以及该产品的所有<strong> </strong><code>year</code> 和<strong> </strong><code>price</code> 。</p>
|
||||
|
||||
<p>返回结果表 <strong>无顺序要求</strong> 。</p>
|
||||
|
||||
<p>结果格式示例如下。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<code><strong>输入:</strong>
|
||||
Sales</code> 表:
|
||||
+---------+------------+------+----------+-------+
|
||||
| sale_id | product_id | year | quantity | price |
|
||||
+---------+------------+------+----------+-------+
|
||||
| 1 | 100 | 2008 | 10 | 5000 |
|
||||
| 2 | 100 | 2009 | 12 | 5000 |
|
||||
| 7 | 200 | 2011 | 15 | 9000 |
|
||||
+---------+------------+------+----------+-------+
|
||||
Product 表:
|
||||
+------------+--------------+
|
||||
| product_id | product_name |
|
||||
+------------+--------------+
|
||||
| 100 | Nokia |
|
||||
| 200 | Apple |
|
||||
| 300 | Samsung |
|
||||
+------------+--------------+
|
||||
<strong>输出:</strong>
|
||||
+--------------+-------+-------+
|
||||
| product_name | year | price |
|
||||
+--------------+-------+-------+
|
||||
| Nokia | 2008 | 5000 |
|
||||
| Nokia | 2009 | 5000 |
|
||||
| Apple | 2011 | 9000 |
|
||||
+--------------+-------+-------+
|
||||
</pre>
|
@@ -0,0 +1,69 @@
|
||||
<p>销售表 <code>Sales</code>:</p>
|
||||
|
||||
<pre>
|
||||
+-------------+-------+
|
||||
| Column Name | Type |
|
||||
+-------------+-------+
|
||||
| sale_id | int |
|
||||
| product_id | int |
|
||||
| year | int |
|
||||
| quantity | int |
|
||||
| price | int |
|
||||
+-------------+-------+
|
||||
(sale_id, year) 是这张表的主键(具有唯一值的列的组合)。
|
||||
product_id 是产品表的外键(reference 列)。
|
||||
这张表的每一行都表示:编号 product_id 的产品在某一年的销售额。
|
||||
请注意,价格是按每单位计的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>产品表 <code>Product</code>:</p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| product_id | int |
|
||||
| product_name | varchar |
|
||||
+--------------+---------+
|
||||
product_id 是这张表的主键(具有唯一值的列)。
|
||||
这张表的每一行都标识:每个产品的 id 和 产品名称。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写解决方案,选出每个售出过的产品 <strong>第一年</strong> 销售的 <strong>产品 id</strong>、<strong>年份</strong>、<strong>数量 </strong>和 <strong>价格</strong>。</p>
|
||||
|
||||
<p>结果表中的条目可以按 <strong>任意顺序</strong> 排列。</p>
|
||||
|
||||
<p>结果格式如下例所示:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Sales 表:
|
||||
+---------+------------+------+----------+-------+
|
||||
| sale_id | product_id | year | quantity | price |
|
||||
+---------+------------+------+----------+-------+
|
||||
| 1 | 100 | 2008 | 10 | 5000 |
|
||||
| 2 | 100 | 2009 | 12 | 5000 |
|
||||
| 7 | 200 | 2011 | 15 | 9000 |
|
||||
+---------+------------+------+----------+-------+
|
||||
Product 表:
|
||||
+------------+--------------+
|
||||
| product_id | product_name |
|
||||
+------------+--------------+
|
||||
| 100 | Nokia |
|
||||
| 200 | Apple |
|
||||
| 300 | Samsung |
|
||||
+------------+--------------+
|
||||
<strong>输出:</strong>
|
||||
+------------+------------+----------+-------+
|
||||
| product_id | first_year | quantity | price |
|
||||
+------------+------------+----------+-------+
|
||||
| 100 | 2008 | 10 | 5000 |
|
||||
| 200 | 2011 | 15 | 9000 |
|
||||
+------------+------------+----------+-------+</pre>
|
@@ -0,0 +1,31 @@
|
||||
<p>编写一个解决方案,基于名为 <code>student_data</code> 的二维列表 <b>创建 </b>一个 DataFrame 。这个二维列表包含一些学生的 ID 和年龄信息。</p>
|
||||
|
||||
<p>DataFrame 应该有两列, <code>student_id</code> 和 <code>age</code>,并且与原始二维列表的顺序相同。</p>
|
||||
|
||||
<p>返回结果格式如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:
|
||||
</strong>student_data:<strong>
|
||||
</strong><code>[
|
||||
[1, 15],
|
||||
[2, 11],
|
||||
[3, 11],
|
||||
[4, 20]
|
||||
]</code>
|
||||
<b>输出:</b>
|
||||
+------------+-----+
|
||||
| student_id | age |
|
||||
+------------+-----+
|
||||
| 1 | 15 |
|
||||
| 2 | 11 |
|
||||
| 3 | 11 |
|
||||
| 4 | 20 |
|
||||
+------------+-----+
|
||||
<b>解释:</b>
|
||||
基于 student_data 创建了一个 DataFrame,包含 student_id 和 age 两列。
|
||||
</pre>
|
@@ -0,0 +1,75 @@
|
||||
<p><code>Employees</code> 表:</p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| id | int |
|
||||
| name | varchar |
|
||||
+---------------+---------+
|
||||
在 SQL 中,id 是这张表的主键。
|
||||
这张表的每一行分别代表了某公司其中一位员工的名字和 ID 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><code>EmployeeUNI</code> 表:</p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| id | int |
|
||||
| unique_id | int |
|
||||
+---------------+---------+
|
||||
在 SQL 中,(id, unique_id) 是这张表的主键。
|
||||
这张表的每一行包含了该公司某位员工的 ID 和他的唯一标识码(unique ID)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>展示每位用户的<strong> 唯一标识码(unique ID )</strong>;如果某位员工没有唯一标识码,使用 null 填充即可。</p>
|
||||
|
||||
<p>你可以以<strong> 任意</strong> 顺序返回结果表。</p>
|
||||
|
||||
<p>返回结果的格式如下例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<code><strong>输入:</strong>
|
||||
Employees</code> 表:
|
||||
+----+----------+
|
||||
| id | name |
|
||||
+----+----------+
|
||||
| 1 | Alice |
|
||||
| 7 | Bob |
|
||||
| 11 | Meir |
|
||||
| 90 | Winston |
|
||||
| 3 | Jonathan |
|
||||
+----+----------+
|
||||
<code>EmployeeUNI</code> 表:
|
||||
+----+-----------+
|
||||
| id | unique_id |
|
||||
+----+-----------+
|
||||
| 3 | 1 |
|
||||
| 11 | 2 |
|
||||
| 90 | 3 |
|
||||
+----+-----------+
|
||||
<strong>输出:</strong>
|
||||
+-----------+----------+
|
||||
| unique_id | name |
|
||||
+-----------+----------+
|
||||
| null | Alice |
|
||||
| null | Bob |
|
||||
| 2 | Meir |
|
||||
| 3 | Winston |
|
||||
| 1 | Jonathan |
|
||||
+-----------+----------+
|
||||
<strong>解释:</strong>
|
||||
Alice and Bob 没有唯一标识码, 因此我们使用 null 替代。
|
||||
Meir 的唯一标识码是 2 。
|
||||
Winston 的唯一标识码是 3 。
|
||||
Jonathan 唯一标识码是 1 。</pre>
|
42
leetcode-cn/problem (Chinese)/修改列 [modify-columns].html
Normal file
42
leetcode-cn/problem (Chinese)/修改列 [modify-columns].html
Normal file
@@ -0,0 +1,42 @@
|
||||
<pre>
|
||||
DataFrame <code>employees</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| name | object |
|
||||
| salary | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>一家公司决定增加员工的薪水。</p>
|
||||
|
||||
<p>编写一个解决方案,将每个员工的薪水乘以2来 <strong>修改</strong> <code>salary</code> 列。</p>
|
||||
|
||||
<p>返回结果格式如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>示例 1:</b></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:
|
||||
</strong>DataFrame employees
|
||||
+---------+--------+
|
||||
| name | salary |
|
||||
+---------+--------+
|
||||
| Jack | 19666 |
|
||||
| Piper | 74754 |
|
||||
| Mia | 62509 |
|
||||
| Ulysses | 54866 |
|
||||
+---------+--------+
|
||||
<strong>输出:
|
||||
</strong>+---------+--------+
|
||||
| name | salary |
|
||||
+---------+--------+
|
||||
| Jack | 39332 |
|
||||
| Piper | 149508 |
|
||||
| Mia | 125018 |
|
||||
| Ulysses | 109732 |
|
||||
+---------+--------+
|
||||
<strong>解释:
|
||||
</strong>每个人的薪水都被加倍。</pre>
|
@@ -0,0 +1,85 @@
|
||||
<p>表: <code>Products</code></p>
|
||||
|
||||
<pre>
|
||||
+------------------+---------+
|
||||
| Column Name | Type |
|
||||
+------------------+---------+
|
||||
| product_id | int |
|
||||
| product_name | varchar |
|
||||
| product_category | varchar |
|
||||
+------------------+---------+
|
||||
product_id 是该表主键(具有唯一值的列)。
|
||||
该表包含该公司产品的数据。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>表: <code>Orders</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| product_id | int |
|
||||
| order_date | date |
|
||||
| unit | int |
|
||||
+---------------+---------+
|
||||
该表可能包含重复行。
|
||||
product_id 是表单 Products 的外键(reference 列)。
|
||||
unit 是在日期 order_date 内下单产品的数目。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>写一个解决方案,要求获取在 2020 年 2 月份下单的数量不少于 100 的产品的名字和数目。</p>
|
||||
|
||||
<p>返回结果表单的 <strong>顺序无要求 </strong>。</p>
|
||||
|
||||
<p>查询结果的格式如下。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Products 表:
|
||||
+-------------+-----------------------+------------------+
|
||||
| product_id | product_name | product_category |
|
||||
+-------------+-----------------------+------------------+
|
||||
| 1 | Leetcode Solutions | Book |
|
||||
| 2 | Jewels of Stringology | Book |
|
||||
| 3 | HP | Laptop |
|
||||
| 4 | Lenovo | Laptop |
|
||||
| 5 | Leetcode Kit | T-shirt |
|
||||
+-------------+-----------------------+------------------+
|
||||
Orders 表:
|
||||
+--------------+--------------+----------+
|
||||
| product_id | order_date | unit |
|
||||
+--------------+--------------+----------+
|
||||
| 1 | 2020-02-05 | 60 |
|
||||
| 1 | 2020-02-10 | 70 |
|
||||
| 2 | 2020-01-18 | 30 |
|
||||
| 2 | 2020-02-11 | 80 |
|
||||
| 3 | 2020-02-17 | 2 |
|
||||
| 3 | 2020-02-24 | 3 |
|
||||
| 4 | 2020-03-01 | 20 |
|
||||
| 4 | 2020-03-04 | 30 |
|
||||
| 4 | 2020-03-04 | 60 |
|
||||
| 5 | 2020-02-25 | 50 |
|
||||
| 5 | 2020-02-27 | 50 |
|
||||
| 5 | 2020-03-01 | 50 |
|
||||
+--------------+--------------+----------+
|
||||
<strong>输出:</strong>
|
||||
+--------------------+---------+
|
||||
| product_name | unit |
|
||||
+--------------------+---------+
|
||||
| Leetcode Solutions | 130 |
|
||||
| Leetcode Kit | 100 |
|
||||
+--------------------+---------+
|
||||
<strong>解释:</strong>
|
||||
2020 年 2 月份下单 product_id = 1 的产品的数目总和为 (60 + 70) = 130 。
|
||||
2020 年 2 月份下单 product_id = 2 的产品的数目总和为 80 。
|
||||
2020 年 2 月份下单 product_id = 3 的产品的数目总和为 (2 + 3) = 5 。
|
||||
2020 年 2 月份 product_id = 4 的产品并没有下单。
|
||||
2020 年 2 月份下单 product_id = 5 的产品的数目总和为 (50 + 50) = 100 。</pre>
|
@@ -0,0 +1,46 @@
|
||||
<pre>
|
||||
DataFrame <code>employees</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type. |
|
||||
+-------------+--------+
|
||||
| name | object |
|
||||
| salary | int. |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>一家公司计划为员工提供奖金。</p>
|
||||
|
||||
<p>编写一个解决方案,创建一个名为 <code>bonus</code> 的新列,其中包含 <code>salary</code> 值的 <strong>两倍</strong>。</p>
|
||||
|
||||
<p>返回结果格式如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>示例 1:</b></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
DataFrame employees
|
||||
+---------+--------+
|
||||
| name | salary |
|
||||
+---------+--------+
|
||||
| Piper | 4548 |
|
||||
| Grace | 28150 |
|
||||
| Georgia | 1103 |
|
||||
| Willow | 6593 |
|
||||
| Finn | 74576 |
|
||||
| Thomas | 24433 |
|
||||
+---------+--------+
|
||||
<b>输出:</b>
|
||||
+---------+--------+--------+
|
||||
| name | salary | bonus |
|
||||
+---------+--------+--------+
|
||||
| Piper | 4548 | 9096 |
|
||||
| Grace | 28150 | 56300 |
|
||||
| Georgia | 1103 | 2206 |
|
||||
| Willow | 593 | 13186 |
|
||||
| Finn | 74576 | 149152 |
|
||||
| Thomas | 24433 | 48866 |
|
||||
+---------+--------+--------+
|
||||
<b>解释:</b>
|
||||
通过将 salary 列中的值加倍创建了一个新的 bonus 列。</pre>
|
@@ -0,0 +1,41 @@
|
||||
<pre>
|
||||
DataFrame students
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| student_id | int |
|
||||
| name | object |
|
||||
| age | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>在 <code>name</code> 列里有一些具有缺失值的行。</p>
|
||||
|
||||
<p>编写一个解决方案,删除具有缺失值的行。</p>
|
||||
|
||||
<p>返回结果格式如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>示例 1:</b></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:
|
||||
</strong>+------------+---------+-----+
|
||||
| student_id | name | age |
|
||||
+------------+---------+-----+
|
||||
| 32 | Piper | 5 |
|
||||
| 217 | None | 19 |
|
||||
| 779 | Georgia | 20 |
|
||||
| 849 | Willow | 14 |
|
||||
+------------+---------+-----+
|
||||
<strong>输出:
|
||||
</strong>+------------+---------+-----+
|
||||
| student_id | name | age |
|
||||
+------------+---------+-----+
|
||||
| 32 | Piper | 5 |
|
||||
| 779 | Georgia | 20 |
|
||||
| 849 | Willow | 14 |
|
||||
+------------+---------+-----+
|
||||
<b>解释:
|
||||
</b>学号为 217 的学生所在行在 name 列中有空值,因此这一行将被删除。</pre>
|
@@ -0,0 +1,46 @@
|
||||
<pre>
|
||||
DataFrame customers
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| customer_id | int |
|
||||
| name | object |
|
||||
| email | object |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>在 DataFrame 中基于 <code>email</code> 列存在一些重复行。</p>
|
||||
|
||||
<p>编写一个解决方案,删除这些重复行,仅保留第一次出现的行。</p>
|
||||
|
||||
<p>返回结果格式如下例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
+-------------+---------+---------------------+
|
||||
| customer_id | name | email |
|
||||
+-------------+---------+---------------------+
|
||||
| 1 | Ella | emily@example.com |
|
||||
| 2 | David | michael@example.com |
|
||||
| 3 | Zachary | sarah@example.com |
|
||||
| 4 | Alice | john@example.com |
|
||||
| 5 | Finn | john@example.com |
|
||||
| 6 | Violet | alice@example.com |
|
||||
+-------------+---------+---------------------+
|
||||
<b>输出:</b>
|
||||
+-------------+---------+---------------------+
|
||||
| customer_id | name | email |
|
||||
+-------------+---------+---------------------+
|
||||
| 1 | Ella | emily@example.com |
|
||||
| 2 | David | michael@example.com |
|
||||
| 3 | Zachary | sarah@example.com |
|
||||
| 4 | Alice | john@example.com |
|
||||
| 6 | Violet | alice@example.com |
|
||||
+-------------+---------+---------------------+
|
||||
<b>解释:</b>
|
||||
Alice (customer_id = 4) 和 Finn (customer_id = 5) 都使用 john@example.com,因此只保留该邮箱地址的第一次出现。
|
||||
</pre>
|
@@ -0,0 +1,42 @@
|
||||
<p>表: <code>Triangle</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| x | int |
|
||||
| y | int |
|
||||
| z | int |
|
||||
+-------------+------+
|
||||
在 SQL 中,(x, y, z)是该表的主键列。
|
||||
该表的每一行包含三个线段的长度。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>对每三个线段报告它们是否可以形成一个三角形。</p>
|
||||
|
||||
<p>以 <strong>任意顺序 </strong>返回结果表。</p>
|
||||
|
||||
<p>查询结果格式如下所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Triangle 表:
|
||||
+----+----+----+
|
||||
| x | y | z |
|
||||
+----+----+----+
|
||||
| 13 | 15 | 30 |
|
||||
| 10 | 20 | 15 |
|
||||
+----+----+----+
|
||||
<strong>输出:</strong>
|
||||
+----+----+----+----------+
|
||||
| x | y | z | triangle |
|
||||
+----+----+----+----------+
|
||||
| 13 | 15 | 30 | No |
|
||||
| 10 | 20 | 15 | Yes |
|
||||
+----+----+----+----------+</pre>
|
@@ -0,0 +1,31 @@
|
||||
<p>请设计一个函数判断一棵二叉树是否 <strong>轴对称</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1694689008-JaaRdV-%E8%BD%B4%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%911.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [6,7,7,8,9,9,8]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>从图中可看出树是轴对称的。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1694689054-vENzHe-%E8%BD%B4%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%912.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,2,2,null,3,null,3]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>从图中可看出最后一层的节点不对称。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<p><code>0 <= 节点个数 <= 1000</code></p>
|
||||
|
||||
<p>注意:本题与主站 101 题相同:<a href="https://leetcode-cn.com/problems/symmetric-tree/">https://leetcode-cn.com/problems/symmetric-tree/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,31 @@
|
||||
<p>输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [3,9,20,null,null,15,7]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>如下图
|
||||
</pre>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1695102431-vbmWJn-image.png" style="height: 281px; width: 500px;" /><br />
|
||||
<br />
|
||||
<strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
输入:root = [1,2,2,3,3,null,null,4,4]
|
||||
输出:false
|
||||
解释:如下图
|
||||
</pre>
|
||||
<img alt="" src="https://pic.leetcode.cn/1695102434-WlaxCo-image.png" style="height: 281px; width: 500px;" />
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= 树的结点个数 <= 10000</code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意:本题与主站 110 题相同:<a href="https://leetcode-cn.com/problems/balanced-binary-tree/">https://leetcode-cn.com/problems/balanced-binary-tree/</a></p>
|
@@ -0,0 +1,29 @@
|
||||
<p>计算机安全专家正在开发一款高度安全的加密通信软件,需要在进行数据传输时对数据进行加密和解密操作。假定 <code>dataA</code> 和 <code>dataB</code> 分别为随机抽样的两次通信的数据量:</p>
|
||||
|
||||
<ul>
|
||||
<li>正数为发送量</li>
|
||||
<li>负数为接受量</li>
|
||||
<li>0 为数据遗失</li>
|
||||
</ul>
|
||||
|
||||
<p>请不使用四则运算符的情况下实现一个函数计算两次通信的数据量之和(三种情况均需被统计),以确保在数据传输过程中的高安全性和保密性。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>dataA = 5, dataB = -1
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>dataA</code> 和 <code>dataB</code> 均可能是负数或 0</li>
|
||||
<li>结果不会溢出 32 位整数</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,34 @@
|
||||
<p>某公司门禁密码使用动态口令技术。初始密码为字符串 <code>password</code>,密码更新均遵循以下步骤:</p>
|
||||
|
||||
<ul>
|
||||
<li>设定一个正整数目标值 <code>target</code></li>
|
||||
<li>将 <code>password</code> 前 <code>target</code> 个字符按原顺序移动至字符串末尾</li>
|
||||
</ul>
|
||||
|
||||
<p>请返回更新后的密码字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> password = "s3cur1tyC0d3", target = 4
|
||||
<strong>输出:</strong> "r1tyC0d3s3cu"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> password = "lrloseumgh", target = 6
|
||||
<strong>输出: </strong>"umghlrlose"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= target < password.length <= 10000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,56 @@
|
||||
<p>配送表: <code>Delivery</code></p>
|
||||
|
||||
<pre>
|
||||
+-----------------------------+---------+
|
||||
| Column Name | Type |
|
||||
+-----------------------------+---------+
|
||||
| delivery_id | int |
|
||||
| customer_id | int |
|
||||
| order_date | date |
|
||||
| customer_pref_delivery_date | date |
|
||||
+-----------------------------+---------+
|
||||
delivery_id 是该表中具有唯一值的列。
|
||||
该表保存着顾客的食物配送信息,顾客在某个日期下了订单,并指定了一个期望的配送日期(和下单日期相同或者在那之后)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>如果顾客期望的配送日期和下单日期相同,则该订单称为 「<strong>即时订单</strong>」,否则称为「<strong>计划订单</strong>」。</p>
|
||||
|
||||
<p>「<strong>首次订单</strong>」是顾客最早创建的订单。我们保证一个顾客只会有一个「首次订单」。</p>
|
||||
|
||||
<p>编写解决方案以获取即时订单在所有用户的首次订单中的比例。<strong>保留两位小数。</strong></p>
|
||||
|
||||
<p>结果示例如下所示:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Delivery 表:
|
||||
+-------------+-------------+------------+-----------------------------+
|
||||
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
|
||||
+-------------+-------------+------------+-----------------------------+
|
||||
| 1 | 1 | 2019-08-01 | 2019-08-02 |
|
||||
| 2 | 2 | 2019-08-02 | 2019-08-02 |
|
||||
| 3 | 1 | 2019-08-11 | 2019-08-12 |
|
||||
| 4 | 3 | 2019-08-24 | 2019-08-24 |
|
||||
| 5 | 3 | 2019-08-21 | 2019-08-22 |
|
||||
| 6 | 2 | 2019-08-11 | 2019-08-13 |
|
||||
| 7 | 4 | 2019-08-09 | 2019-08-09 |
|
||||
+-------------+-------------+------------+-----------------------------+
|
||||
<strong>输出:</strong>
|
||||
+----------------------+
|
||||
| immediate_percentage |
|
||||
+----------------------+
|
||||
| 50.00 |
|
||||
+----------------------+
|
||||
<strong>解释:</strong>
|
||||
1 号顾客的 1 号订单是首次订单,并且是计划订单。
|
||||
2 号顾客的 2 号订单是首次订单,并且是即时订单。
|
||||
3 号顾客的 5 号订单是首次订单,并且是计划订单。
|
||||
4 号顾客的 7 号订单是首次订单,并且是即时订单。
|
||||
因此,一半顾客的首次订单是即时的。
|
||||
</pre>
|
@@ -0,0 +1,80 @@
|
||||
<p><code>MyNumbers</code> 表:</p>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| num | int |
|
||||
+-------------+------+
|
||||
该表可能包含重复项(换句话说,在SQL中,该表没有主键)。
|
||||
这张表的每一行都含有一个整数。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>单一数字</strong> 是在 <code>MyNumbers</code> 表中只出现一次的数字。</p>
|
||||
|
||||
<p>找出最大的 <strong>单一数字</strong> 。如果不存在 <strong>单一数字</strong> ,则返回 <code>null</code> 。</p>
|
||||
|
||||
<p>查询结果如下例所示。</p>
|
||||
<ptable> </ptable>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
MyNumbers 表:
|
||||
+-----+
|
||||
| num |
|
||||
+-----+
|
||||
| 8 |
|
||||
| 8 |
|
||||
| 3 |
|
||||
| 3 |
|
||||
| 1 |
|
||||
| 4 |
|
||||
| 5 |
|
||||
| 6 |
|
||||
+-----+
|
||||
<strong>输出:</strong>
|
||||
+-----+
|
||||
| num |
|
||||
+-----+
|
||||
| 6 |
|
||||
+-----+
|
||||
<strong>解释:</strong>单一数字有 1、4、5 和 6 。
|
||||
6 是最大的单一数字,返回 6 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
MyNumbers table:
|
||||
+-----+
|
||||
| num |
|
||||
+-----+
|
||||
| 8 |
|
||||
| 8 |
|
||||
| 7 |
|
||||
| 7 |
|
||||
| 3 |
|
||||
| 3 |
|
||||
| 3 |
|
||||
+-----+
|
||||
<strong>输出:</strong>
|
||||
+------+
|
||||
| num |
|
||||
+------+
|
||||
| null |
|
||||
+------+
|
||||
<strong>解释:</strong>输入的表中不存在单一数字,所以返回 null 。
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,80 @@
|
||||
<p>用户表: <code>Users</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| user_id | int |
|
||||
| user_name | varchar |
|
||||
+-------------+---------+
|
||||
user_id 是该表的主键(具有唯一值的列)。
|
||||
该表中的每行包括用户 ID 和用户名。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注册表: <code>Register</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| contest_id | int |
|
||||
| user_id | int |
|
||||
+-------------+---------+
|
||||
(contest_id, user_id) 是该表的主键(具有唯一值的列的组合)。
|
||||
该表中的每行包含用户的 ID 和他们注册的赛事。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写解决方案统计出各赛事的用户注册百分率,保留两位小数。</p>
|
||||
|
||||
<p>返回的结果表按 <code>percentage</code> 的 <strong>降序 </strong>排序,若相同则按 <code>contest_id</code> 的 <strong>升序 </strong>排序。</p>
|
||||
|
||||
<p>返回结果如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<code><strong>输入:</strong>
|
||||
Users</code> 表:
|
||||
+---------+-----------+
|
||||
| user_id | user_name |
|
||||
+---------+-----------+
|
||||
| 6 | Alice |
|
||||
| 2 | Bob |
|
||||
| 7 | Alex |
|
||||
+---------+-----------+
|
||||
|
||||
<code>Register</code> 表:
|
||||
+------------+---------+
|
||||
| contest_id | user_id |
|
||||
+------------+---------+
|
||||
| 215 | 6 |
|
||||
| 209 | 2 |
|
||||
| 208 | 2 |
|
||||
| 210 | 6 |
|
||||
| 208 | 6 |
|
||||
| 209 | 7 |
|
||||
| 209 | 6 |
|
||||
| 215 | 7 |
|
||||
| 208 | 7 |
|
||||
| 210 | 2 |
|
||||
| 207 | 2 |
|
||||
| 210 | 7 |
|
||||
+------------+---------+
|
||||
<strong>输出:</strong>
|
||||
+------------+------------+
|
||||
| contest_id | percentage |
|
||||
+------------+------------+
|
||||
| 208 | 100.0 |
|
||||
| 209 | 100.0 |
|
||||
| 210 | 100.0 |
|
||||
| 215 | 66.67 |
|
||||
| 207 | 33.33 |
|
||||
+------------+------------+
|
||||
<strong>解释:</strong>
|
||||
所有用户都注册了 208、209 和 210 赛事,因此这些赛事的注册率为 100% ,我们按 contest_id 的降序排序加入结果表中。
|
||||
Alice 和 Alex 注册了 215 赛事,注册率为 ((2/3) * 100) = 66.67%
|
||||
Bob 注册了 207 赛事,注册率为 ((1/3) * 100) = 33.33%</pre>
|
69
leetcode-cn/problem (Chinese)/员工奖金 [employee-bonus].html
Normal file
69
leetcode-cn/problem (Chinese)/员工奖金 [employee-bonus].html
Normal file
@@ -0,0 +1,69 @@
|
||||
<p>表:<code>Employee</code> </p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| empId | int |
|
||||
| name | varchar |
|
||||
| supervisor | int |
|
||||
| salary | int |
|
||||
+-------------+---------+
|
||||
empId 是该表中具有唯一值的列。
|
||||
该表的每一行都表示员工的姓名和 id,以及他们的工资和经理的 id。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>表:<code>Bonus</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| empId | int |
|
||||
| bonus | int |
|
||||
+-------------+------+
|
||||
empId 是该表具有唯一值的列。
|
||||
empId 是 Employee 表中 empId 的外键(reference 列)。
|
||||
该表的每一行都包含一个员工的 id 和他们各自的奖金。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写解决方案,报告每个奖金 <strong>少于</strong> <code>1000</code> 的员工的姓名和奖金数额。</p>
|
||||
|
||||
<p>以 <strong>任意顺序</strong> 返回结果表。</p>
|
||||
|
||||
<p>结果格式如下所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
Employee table:
|
||||
+-------+--------+------------+--------+
|
||||
| empId | name | supervisor | salary |
|
||||
+-------+--------+------------+--------+
|
||||
| 3 | Brad | null | 4000 |
|
||||
| 1 | John | 3 | 1000 |
|
||||
| 2 | Dan | 3 | 2000 |
|
||||
| 4 | Thomas | 3 | 4000 |
|
||||
+-------+--------+------------+--------+
|
||||
Bonus table:
|
||||
+-------+-------+
|
||||
| empId | bonus |
|
||||
+-------+-------+
|
||||
| 2 | 500 |
|
||||
| 4 | 2000 |
|
||||
+-------+-------+
|
||||
<b>输出:</b>
|
||||
+------+-------+
|
||||
| name | bonus |
|
||||
+------+-------+
|
||||
| Brad | null |
|
||||
| John | null |
|
||||
| Dan | 500 |
|
||||
+------+-------+</pre>
|
@@ -0,0 +1,60 @@
|
||||
<p>表:<code>Employee</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| employee_id | int |
|
||||
| department_id | int |
|
||||
| primary_flag | varchar |
|
||||
+---------------+---------+
|
||||
这张表的主键为 employee_id, department_id (具有唯一值的列的组合)
|
||||
employee_id 是员工的ID
|
||||
department_id 是部门的ID,表示员工与该部门有关系
|
||||
primary_flag 是一个枚举类型,值分别为('Y', 'N'). 如果值为'Y',表示该部门是员工的直属部门。 如果值是'N',则否
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>一个员工可以属于多个部门。当一个员工加入<strong>超过一个部门</strong>的时候,他需要决定哪个部门是他的直属部门。请注意,当员工只加入一个部门的时候,那这个部门将默认为他的直属部门,虽然表记录的值为<code>'N'</code>.</p>
|
||||
|
||||
<p>请编写解决方案,查出员工所属的直属部门。</p>
|
||||
|
||||
<p>返回结果 <strong>没有顺序要求</strong> 。</p>
|
||||
|
||||
<p>返回结果格式如下例子所示:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Employee table:
|
||||
+-------------+---------------+--------------+
|
||||
| employee_id | department_id | primary_flag |
|
||||
+-------------+---------------+--------------+
|
||||
| 1 | 1 | N |
|
||||
| 2 | 1 | Y |
|
||||
| 2 | 2 | N |
|
||||
| 3 | 3 | N |
|
||||
| 4 | 2 | N |
|
||||
| 4 | 3 | Y |
|
||||
| 4 | 4 | N |
|
||||
+-------------+---------------+--------------+
|
||||
<strong>输出:</strong>
|
||||
+-------------+---------------+
|
||||
| employee_id | department_id |
|
||||
+-------------+---------------+
|
||||
| 1 | 1 |
|
||||
| 2 | 1 |
|
||||
| 3 | 3 |
|
||||
| 4 | 3 |
|
||||
+-------------+---------------+
|
||||
<strong>解释:</strong>
|
||||
- 员工 1 的直属部门是 1
|
||||
- 员工 2 的直属部门是 1
|
||||
- 员工 3 的直属部门是 3
|
||||
- 员工 4 的直属部门是 3</pre>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,17 @@
|
||||
<p>书店店员有一张链表形式的书单,每个节点代表一本书,节点中的值表示书的编号。为更方便整理书架,店员需要将书单倒过来排列,就可以从最后一本书开始整理,逐一将书放回到书架上。请倒序返回这个书单链表。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>head = [3,6,4,1]
|
||||
|
||||
<strong>输出:</strong>[1,4,6,3]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<p><code>0 <= 链表长度 <= 10000</code></p>
|
@@ -0,0 +1,36 @@
|
||||
<p>读者来到图书馆排队借还书,图书管理员使用两个书车来完成整理借还书的任务。书车中的书从下往上叠加存放,图书管理员每次只能拿取书车顶部的书。排队的读者会有两种操作:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>push(bookID)</code>:把借阅的书籍还到图书馆。</li>
|
||||
<li><code>pop()</code>:从图书馆中借出书籍。</li>
|
||||
</ul>
|
||||
|
||||
<p>为了保持图书的顺序,图书管理员每次取出供读者借阅的书籍是 <strong>最早</strong> 归还到图书馆的书籍。你需要返回 <strong>每次读者借出书的值</strong> 。</p>
|
||||
|
||||
<p>如果没有归还的书可以取出,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["BookQueue", "push", "push", "pop"]
|
||||
[[], [1], [2], []]
|
||||
<strong>输出:</strong>[null,null,null,1]
|
||||
<strong>解释:
|
||||
</strong>MyQueue myQueue = new MyQueue();
|
||||
myQueue.push(1); // queue is: [1]
|
||||
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
|
||||
myQueue.pop(); // return 1, queue is [2]</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= bookID <= 10000</code></li>
|
||||
<li>最多会对 <code>push</code>、<code>pop</code> 进行 <code>10000</code> 次调用</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
39
leetcode-cn/problem (Chinese)/填充缺失值 [fill-missing-data].html
Normal file
39
leetcode-cn/problem (Chinese)/填充缺失值 [fill-missing-data].html
Normal file
@@ -0,0 +1,39 @@
|
||||
<pre>
|
||||
DataFrame <code>products</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| name | object |
|
||||
| quantity | int |
|
||||
| price | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>编写一个解决方案,在 <code>quantity</code> 列中将缺失的值填充为 <code><strong>0</strong></code>。</p>
|
||||
|
||||
<p>返回结果如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
<strong class="example">示例 1:</strong>
|
||||
|
||||
<pre>
|
||||
<strong>输入:
|
||||
</strong>+-----------------+----------+-------+
|
||||
| name | quantity | price |
|
||||
+-----------------+----------+-------+
|
||||
| Wristwatch | 32 | 135 |
|
||||
| WirelessEarbuds | None | 821 |
|
||||
| GolfClubs | None | 9319 |
|
||||
| Printer | 849 | 3051 |
|
||||
+-----------------+----------+-------+
|
||||
<strong>输出:
|
||||
</strong>+-----------------+----------+-------+
|
||||
| name | quantity | price |
|
||||
+-----------------+----------+-------+
|
||||
| Wristwatch | 32 | 135 |
|
||||
| WirelessEarbuds | 0 | 821 |
|
||||
| GolfClubs | 0 | 9319 |
|
||||
| Printer | 849 | 3051 |
|
||||
+-----------------+----------+-------+
|
||||
<b>解释:</b>
|
||||
Toaster 和 Headphones 的数量被填充为 0。</pre>
|
@@ -0,0 +1,22 @@
|
||||
<p>某店铺将用于组成套餐的商品记作字符串 <code>goods</code>,其中 <code>goods[i]</code> 表示对应商品。请返回该套餐内所含商品的 <strong>全部排列方式</strong> 。</p>
|
||||
|
||||
<p>返回结果 <strong>无顺序要求</strong>,但不能含有重复的元素。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>goods = "agew"
|
||||
<strong>输出:</strong>["aegw","aewg","agew","agwe","aweg","awge","eagw","eawg","egaw","egwa","ewag","ewga","gaew","gawe","geaw","gewa","gwae","gwea","waeg","wage","weag","wega","wgae","wgea"]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= goods.length <= 8</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,53 @@
|
||||
<p><code>RequestAccepted</code> 表:</p>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<pre>
|
||||
+----------------+---------+
|
||||
| Column Name | Type |
|
||||
+----------------+---------+
|
||||
| requester_id | int |
|
||||
| accepter_id | int |
|
||||
| accept_date | date |
|
||||
+----------------+---------+
|
||||
(requester_id, accepter_id) 是这张表的主键(具有唯一值的列的组合)。
|
||||
这张表包含发送好友请求的人的 ID ,接收好友请求的人的 ID ,以及好友请求通过的日期。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写解决方案,找出拥有最多的好友的人和他拥有的好友数目。</p>
|
||||
|
||||
<p>生成的测试用例保证拥有最多好友数目的只有 1 个人。</p>
|
||||
|
||||
<p>查询结果格式如下例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
RequestAccepted 表:
|
||||
+--------------+-------------+-------------+
|
||||
| requester_id | accepter_id | accept_date |
|
||||
+--------------+-------------+-------------+
|
||||
| 1 | 2 | 2016/06/03 |
|
||||
| 1 | 3 | 2016/06/08 |
|
||||
| 2 | 3 | 2016/06/08 |
|
||||
| 3 | 4 | 2016/06/09 |
|
||||
+--------------+-------------+-------------+
|
||||
<strong>输出:</strong>
|
||||
+----+-----+
|
||||
| id | num |
|
||||
+----+-----+
|
||||
| 3 | 3 |
|
||||
+----+-----+
|
||||
<strong>解释:</strong>
|
||||
编号为 3 的人是编号为 1 ,2 和 4 的人的好友,所以他总共有 3 个好友,比其他人都多。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>在真实世界里,可能会有多个人拥有好友数相同且最多,你能找到所有这些人吗?</p>
|
||||
</div>
|
||||
</div>
|
@@ -0,0 +1,33 @@
|
||||
<p>给定两棵二叉树 <code>tree1</code> 和 <code>tree2</code>,判断 <code>tree2</code> 是否以 <code>tree1</code> 的某个节点为根的子树具有 <strong>相同的结构和节点值</strong> 。<br />
|
||||
注意,<strong>空树 </strong>不会是以 <code>tree1</code> 的某个节点为根的子树具有 <strong>相同的结构和节点值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1694684670-vwyIgY-two_tree.png" /></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>tree1 = [1,7,5], tree2 = [6,1]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>tree2 与 tree1 的一个子树没有相同的结构和节点值。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1694685602-myWXCv-two_tree_2.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>tree1 = [3,6,7,1,8], tree2 = [6,1]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>tree2 与 tree1 的一个子树拥有相同的结构和节点值。即 6 - > 1。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<p><code>0 <= 节点个数 <= 10000</code></p>
|
@@ -0,0 +1,49 @@
|
||||
<p>字母迷宫游戏初始界面记作 <code>m x n</code> 二维字符串数组 <code>grid</code>,请判断玩家是否能在 <code>grid</code> 中找到目标单词 <code>target</code>。<br />
|
||||
注意:寻找单词时 <strong>必须</strong> 按照字母顺序,通过水平或垂直方向相邻的单元格内的字母构成,同时,同一个单元格内的字母 <strong>不允许被重复使用 </strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/word2.jpg" /></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], target = "ABCCED"
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], target = "SEE"
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], target = "ABCB"
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n = grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 6</code></li>
|
||||
<li><code>1 <= target.length <= 15</code></li>
|
||||
<li><code>grid</code> 和 <code>target</code> 仅由大小写英文字母组成</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>本题与主站 79 题相同:<a href="https://leetcode-cn.com/problems/word-search/">https://leetcode-cn.com/problems/word-search/</a></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,47 @@
|
||||
<p>你在与一位习惯从右往左阅读的朋友发消息,他发出的文字顺序都与正常相反但单词内容正确,为了和他顺利交流你决定写一个转换程序,把他所发的消息 <code>message</code> 转换为正常语序。</p>
|
||||
|
||||
<p>注意:输入字符串 <code>message</code> 中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> message = "<code>the sky is blue</code>"
|
||||
<strong>输出: </strong>"<code>blue is sky the</code>"
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> message = " hello world! "
|
||||
<strong>输出: </strong>"world! hello"
|
||||
<strong>解释: </strong>输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> message = "a good example"
|
||||
<strong>输出: </strong>"example good a"
|
||||
<strong>解释: </strong>如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= message.length <= 10^4</code></li>
|
||||
<li><code>message</code> 中包含英文大小写字母、空格和数字</li>
|
||||
<li><code>message</code> 中至少有一个单词</li>
|
||||
<li> </li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>本题与主站 151 题相同:<a href="https://leetcode-cn.com/problems/reverse-words-in-a-string/">https://leetcode-cn.com/problems/reverse-words-in-a-string/</a></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,112 @@
|
||||
<p>学生表: <code>Students</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| student_id | int |
|
||||
| student_name | varchar |
|
||||
+---------------+---------+
|
||||
在 SQL 中,主键为 student_id(学生ID)。
|
||||
该表内的每一行都记录有学校一名学生的信息。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>科目表: <code>Subjects</code></p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| subject_name | varchar |
|
||||
+--------------+---------+
|
||||
在 SQL 中,主键为 subject_name(科目名称)。
|
||||
每一行记录学校的一门科目名称。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>考试表: <code>Examinations</code></p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| student_id | int |
|
||||
| subject_name | varchar |
|
||||
+--------------+---------+
|
||||
这个表可能包含重复数据(换句话说,在 SQL 中,这个表没有主键)。
|
||||
学生表里的一个学生修读科目表里的每一门科目。
|
||||
这张考试表的每一行记录就表示学生表里的某个学生参加了一次科目表里某门科目的测试。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>查询出每个学生参加每一门科目测试的次数,结果按 <code>student_id</code> 和 <code>subject_name</code> 排序。</p>
|
||||
|
||||
<p>查询结构格式如下所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Students table:
|
||||
+------------+--------------+
|
||||
| student_id | student_name |
|
||||
+------------+--------------+
|
||||
| 1 | Alice |
|
||||
| 2 | Bob |
|
||||
| 13 | John |
|
||||
| 6 | Alex |
|
||||
+------------+--------------+
|
||||
Subjects table:
|
||||
+--------------+
|
||||
| subject_name |
|
||||
+--------------+
|
||||
| Math |
|
||||
| Physics |
|
||||
| Programming |
|
||||
+--------------+
|
||||
Examinations table:
|
||||
+------------+--------------+
|
||||
| student_id | subject_name |
|
||||
+------------+--------------+
|
||||
| 1 | Math |
|
||||
| 1 | Physics |
|
||||
| 1 | Programming |
|
||||
| 2 | Programming |
|
||||
| 1 | Physics |
|
||||
| 1 | Math |
|
||||
| 13 | Math |
|
||||
| 13 | Programming |
|
||||
| 13 | Physics |
|
||||
| 2 | Math |
|
||||
| 1 | Math |
|
||||
+------------+--------------+
|
||||
<strong>输出:</strong>
|
||||
+------------+--------------+--------------+----------------+
|
||||
| student_id | student_name | subject_name | attended_exams |
|
||||
+------------+--------------+--------------+----------------+
|
||||
| 1 | Alice | Math | 3 |
|
||||
| 1 | Alice | Physics | 2 |
|
||||
| 1 | Alice | Programming | 1 |
|
||||
| 2 | Bob | Math | 1 |
|
||||
| 2 | Bob | Physics | 0 |
|
||||
| 2 | Bob | Programming | 1 |
|
||||
| 6 | Alex | Math | 0 |
|
||||
| 6 | Alex | Physics | 0 |
|
||||
| 6 | Alex | Programming | 0 |
|
||||
| 13 | John | Math | 1 |
|
||||
| 13 | John | Physics | 1 |
|
||||
| 13 | John | Programming | 1 |
|
||||
+------------+--------------+--------------+----------------+
|
||||
<strong>解释:</strong>
|
||||
结果表需包含所有学生和所有科目(即便测试次数为0):
|
||||
Alice 参加了 3 次数学测试, 2 次物理测试,以及 1 次编程测试;
|
||||
Bob 参加了 1 次数学测试, 1 次编程测试,没有参加物理测试;
|
||||
Alex 啥测试都没参加;
|
||||
John 参加了数学、物理、编程测试各 1 次。
|
||||
</pre>
|
@@ -0,0 +1,42 @@
|
||||
<p>某公司组织架构以二叉搜索树形式记录,节点值为处于该职位的员工编号。请返回第 <code>cnt</code> 大的员工编号。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1695101634-kzHKZW-image.png" style="height: 281px; width: 500px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [7, 3, 9, 1, 5], cnt = 2
|
||||
7
|
||||
/ \
|
||||
3 9
|
||||
/ \
|
||||
1 5
|
||||
<strong>输出:</strong>7
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1695101636-ESZtLa-image.png" style="height: 281px; width: 500px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [10, 5, 15, 2, 7, null, 20, 1, null, 6, 8], cnt = 4
|
||||
10
|
||||
/ \
|
||||
5 15
|
||||
/ \ \
|
||||
2 7 20
|
||||
/ / \
|
||||
1 6 8
|
||||
<strong>输出:</strong> 8</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>1 ≤ cnt ≤ 二叉搜索树元素个数</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,21 @@
|
||||
<p>设备中存有 <code>n</code> 个文件,文件 <code>id</code> 记于数组 <code>documents</code>。若文件 <code>id</code> 相同,则定义为该文件存在副本。请返回任一存在副本的文件 <code>id</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>documents = [2, 5, 3, 0, 5, 0]
|
||||
<strong>输出:</strong>0 或 5
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 ≤ documents[i] ≤ n-1</code></li>
|
||||
<li><code>2 <= n <= 100000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,43 @@
|
||||
<p><code>m</code>*<code>n</code> 的二维数组 <code>plants</code> 记录了园林景观的植物排布情况,具有以下特性:</p>
|
||||
|
||||
<ul>
|
||||
<li>每行中,每棵植物的右侧相邻植物不矮于该植物;</li>
|
||||
<li>每列中,每棵植物的下侧相邻植物不矮于该植物。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>请判断 <code>plants</code> 中是否存在目标高度值 <code>target</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>plants = [[2,3,6,8],[4,5,8,9],[5,9,10,12]], target = 8
|
||||
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>plants = [[1,3,5],[2,5,7]], target = 4
|
||||
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 1000</code></li>
|
||||
<li><code>0 <= m <= 1000</code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意:本题与主站 240 题相同:<a href="https://leetcode-cn.com/problems/search-a-2d-matrix-ii/" rel="noopener noreferrer" target="_blank">https://leetcode-cn.com/problems/search-a-2d-matrix-ii/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,54 @@
|
||||
<p>将一个 <strong>二叉搜索树</strong> 就地转化为一个 <strong>已排序的双向循环链表</strong> 。</p>
|
||||
|
||||
<p>对于双向循环列表,你可以将左右孩子指针作为双向循环链表的前驱和后继指针,第一个节点的前驱是最后一个节点,最后一个节点的后继是第一个节点。</p>
|
||||
|
||||
<p>特别地,我们希望可以 <strong>就地</strong> 完成转换操作。当转化完成以后,树中节点的左指针需要指向前驱,树中节点的右指针需要指向后继。还需要返回链表中最小元素的指针。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [4,2,5,1,3]
|
||||
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturndll.png" />
|
||||
<strong>输出:</strong>[1,2,3,4,5]
|
||||
|
||||
<strong>解释:</strong>下图显示了转化后的二叉搜索树,实线表示后继关系,虚线表示前驱关系。
|
||||
<img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturnbst.png" />
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [2,1,3]
|
||||
<strong>输出:</strong>[1,2,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = []
|
||||
<strong>输出:</strong>[]
|
||||
<strong>解释:</strong>输入是空树,所以输出也是空链表。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1]
|
||||
<strong>输出:</strong>[1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-1000 <= Node.val <= 1000</code></li>
|
||||
<li><code>Node.left.val < Node.val < Node.right.val</code></li>
|
||||
<li><code>Node.val</code> 的所有值都是独一无二的</li>
|
||||
<li><code>0 <= Number of Nodes <= 2000</code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意:本题与主站 426 题相同:<a href="https://leetcode-cn.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/" rel="noopener noreferrer" target="_blank">https://leetcode-cn.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/</a></p>
|
@@ -0,0 +1,73 @@
|
||||
<p>表:<code>Prices</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| product_id | int |
|
||||
| start_date | date |
|
||||
| end_date | date |
|
||||
| price | int |
|
||||
+---------------+---------+
|
||||
(product_id,start_date,end_date) 是 <code>prices</code> 表的主键(具有唯一值的列的组合)。
|
||||
<code>prices</code> 表的每一行表示的是某个产品在一段时期内的价格。
|
||||
每个产品的对应时间段是不会重叠的,这也意味着同一个产品的价格时段不会出现交叉。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>表:<code>UnitsSold</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| product_id | int |
|
||||
| purchase_date | date |
|
||||
| units | int |
|
||||
+---------------+---------+
|
||||
<span style="white-space: pre-wrap;">该表可能包含重复数据</span>。
|
||||
<span style="white-space: pre-wrap;">该</span>表的每一行表示的是每种产品的出售日期,单位和产品 id。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写解决方案以查找每种产品的平均售价。<code>average_price</code> 应该 <strong>四舍五入到小数点后两位</strong>。</p>
|
||||
|
||||
<p>返回结果表 <strong>无顺序要求</strong> 。</p>
|
||||
|
||||
<p>结果格式如下例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Prices table:
|
||||
+------------+------------+------------+--------+
|
||||
| product_id | start_date | end_date | price |
|
||||
+------------+------------+------------+--------+
|
||||
| 1 | 2019-02-17 | 2019-02-28 | 5 |
|
||||
| 1 | 2019-03-01 | 2019-03-22 | 20 |
|
||||
| 2 | 2019-02-01 | 2019-02-20 | 15 |
|
||||
| 2 | 2019-02-21 | 2019-03-31 | 30 |
|
||||
+------------+------------+------------+--------+
|
||||
UnitsSold table:
|
||||
+------------+---------------+-------+
|
||||
| product_id | purchase_date | units |
|
||||
+------------+---------------+-------+
|
||||
| 1 | 2019-02-25 | 100 |
|
||||
| 1 | 2019-03-01 | 15 |
|
||||
| 2 | 2019-02-10 | 200 |
|
||||
| 2 | 2019-03-22 | 30 |
|
||||
+------------+---------------+-------+
|
||||
<strong>输出:</strong>
|
||||
+------------+---------------+
|
||||
| product_id | average_price |
|
||||
+------------+---------------+
|
||||
| 1 | 6.96 |
|
||||
| 2 | 16.96 |
|
||||
+------------+---------------+
|
||||
<strong>解释:</strong>
|
||||
平均售价 = 产品总价 / 销售的产品数量。
|
||||
产品 1 的平均售价 = ((100 * 5)+(15 * 20) )/ 115 = 6.96
|
||||
产品 2 的平均售价 = ((200 * 15)+(30 * 30) )/ 230 = 16.96</pre>
|
64
leetcode-cn/problem (Chinese)/序列重建 [ur2n8P].html
Normal file
64
leetcode-cn/problem (Chinese)/序列重建 [ur2n8P].html
Normal file
@@ -0,0 +1,64 @@
|
||||
<p>给定一个长度为 <code>n</code> 的整数数组 <code>nums</code> ,其中 <code>nums</code> 是范围为 <code>[1,n]</code> 的整数的排列。还提供了一个 2D 整数数组 <code>sequences</code> ,其中 <code>sequences[i]</code> 是 <code>nums</code> 的子序列。<br />
|
||||
检查 <code>nums</code> 是否是唯一的最短 <strong>超序列</strong> 。最短 <strong>超序列</strong> 是 <strong>长度最短</strong> 的序列,并且所有序列 <code>sequences[i]</code> 都是它的子序列。对于给定的数组 <code>sequences</code> ,可能存在多个有效的 <strong>超序列</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,对于 <code>sequences = [[1,2],[1,3]]</code> ,有两个最短的 <strong>超序列</strong> ,<code>[1,2,3]</code> 和 <code>[1,3,2]</code> 。</li>
|
||||
<li>而对于 <code>sequences = [[1,2],[1,3],[1,2,3]]</code> ,唯一可能的最短 <strong>超序列</strong> 是 <code>[1,2,3]</code> 。<code>[1,2,3,4]</code> 是可能的超序列,但不是最短的。</li>
|
||||
</ul>
|
||||
|
||||
<p><em>如果 <code>nums</code> 是序列的唯一最短 <strong>超序列</strong> ,则返回 <code>true</code> ,否则返回 <code>false</code> 。</em><br />
|
||||
<strong>子序列</strong> 是一个可以通过从另一个序列中删除一些元素或不删除任何元素,而不改变其余元素的顺序的序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3], sequences = [[1,2],[1,3]]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>有两种可能的超序列:[1,2,3]和[1,3,2]。
|
||||
序列 [1,2] 是[<u><strong>1,2</strong></u>,3]和[<u><strong>1</strong></u>,3,<u><strong>2</strong></u>]的子序列。
|
||||
序列 [1,3] 是[<u><strong>1</strong></u>,2,<u><strong>3</strong></u>]和[<u><strong>1,3</strong></u>,2]的子序列。
|
||||
因为 nums 不是唯一最短的超序列,所以返回false。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3], sequences = [[1,2]]
|
||||
<strong>输出:</strong>false
|
||||
<strong>解释:</strong>最短可能的超序列为 [1,2]。
|
||||
序列 [1,2] 是它的子序列:[<u><strong>1,2</strong></u>]。
|
||||
因为 nums 不是最短的超序列,所以返回false。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3], sequences = [[1,2],[1,3],[2,3]]
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>最短可能的超序列为[1,2,3]。
|
||||
序列 [1,2] 是它的一个子序列:[<strong>1,2</strong>,3]。
|
||||
序列 [1,3] 是它的一个子序列:[<u><strong>1</strong></u>,2,<u><strong>3</strong></u>]。
|
||||
序列 [2,3] 是它的一个子序列:[1,<u><strong>2,3</strong></u>]。
|
||||
因为 nums 是唯一最短的超序列,所以返回true。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>nums</code> 是 <code>[1, n]</code> 范围内所有整数的排列</li>
|
||||
<li><code>1 <= sequences.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= sequences[i].length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= sum(sequences[i].length) <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= sequences[i][j] <= n</code></li>
|
||||
<li><code>sequences</code> 的所有数组都是 <strong>唯一 </strong>的</li>
|
||||
<li><code>sequences[i]</code> 是 <code>nums</code> 的一个子序列</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:本题与主站 444 题相同:<a href="https://leetcode-cn.com/problems/sequence-reconstruction/">https://leetcode-cn.com/problems/sequence-reconstruction/</a></p>
|
@@ -0,0 +1,32 @@
|
||||
<p>仓库管理员以数组 <code>stock</code> 形式记录商品库存表。<code>stock[i]</code> 表示商品 <code>id</code>,可能存在重复。原库存表按商品 <code>id</code> 升序排列。现因突发情况需要进行商品紧急调拨,管理员将这批商品 <code>id</code> 提前依次整理至库存表最后。请你找到并返回库存表中编号的 <strong>最小的元素</strong> 以便及时记录本次调拨。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>stock =<strong> </strong>[4,5,8,3,4]
|
||||
<strong>输出:</strong>3
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>stock = [5,7,9,1,2]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>提示:</p>
|
||||
|
||||
<ul>
|
||||
<li>1 <= stock.length <= 5000</li>
|
||||
<li>-5000 <= stock[i] <= 5000</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:本题与主站 154 题相同:<a href="https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/">https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,22 @@
|
||||
<p>仓库管理员以数组 <code>stock</code> 形式记录商品库存表。<code>stock[i]</code> 表示商品 <code>id</code>,可能存在重复。请返回库存表中数量大于 <code>stock.length / 2</code> 的商品 <code>id</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>stock = [6, 1, 3, 1, 1, 1]
|
||||
<strong>输出: </strong>1</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>限制:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= stock.length <= 50000</code></li>
|
||||
<li>给定数组为非空数组,且存在结果数字</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:本题与主站 169 题相同:<a href="https://leetcode-cn.com/problems/majority-element/">https://leetcode-cn.com/problems/majority-element/</a></p>
|
@@ -0,0 +1,27 @@
|
||||
<p>仓库管理员以数组 <code>stock</code> 形式记录商品库存表,其中 <code>stock[i]</code> 表示对应商品库存余量。请返回库存余量最少的 <code>cnt</code> 个商品余量,返回 <strong>顺序不限</strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>stock = [2,5,7,4], cnt = 1
|
||||
<strong>输出:</strong>[2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>stock = [0,2,3,6], cnt = 2
|
||||
<strong>输出:</strong>[0,2] 或 [2,0]</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= cnt <= stock.length <= 10000<br />
|
||||
0 <= stock[i] <= 10000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,22 @@
|
||||
<p>一棵圣诞树记作根节点为 <code>root</code> 的二叉树,节点值为该位置装饰彩灯的颜色编号。请按照从 <strong>左</strong> 到 <strong>右</strong> 的顺序返回每一层彩灯编号。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1694758674-XYrUiV-%E5%89%91%E6%8C%87%20Offer%2032%20-%20I_%E7%A4%BA%E4%BE%8B1.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [8,17,21,18,null,null,6]
|
||||
<strong>输出:</strong>[8,17,21,18,6]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>节点总数 <= 1000</code></li>
|
||||
</ol>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,22 @@
|
||||
<p>一棵圣诞树记作根节点为 <code>root</code> 的二叉树,节点值为该位置装饰彩灯的颜色编号。请按照从左到右的顺序返回每一层彩灯编号,每一层的结果记录于一行。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1694758674-XYrUiV-%E5%89%91%E6%8C%87%20Offer%2032%20-%20I_%E7%A4%BA%E4%BE%8B1.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [8,17,21,18,null,null,6]
|
||||
<strong>输出:</strong>[[8],[17,21],[18,6]]
|
||||
</pre>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li><code>节点总数 <= 1000</code></li>
|
||||
</ol>
|
||||
|
||||
<p>注意:本题与主站 102 题相同:<a href="https://leetcode-cn.com/problems/binary-tree-level-order-traversal/">https://leetcode-cn.com/problems/binary-tree-level-order-traversal/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,27 @@
|
||||
<p>一棵圣诞树记作根节点为 <code>root</code> 的二叉树,节点值为该位置装饰彩灯的颜色编号。请按照如下规则记录彩灯装饰结果:</p>
|
||||
|
||||
<ul>
|
||||
<li>第一层按照从左到右的顺序记录</li>
|
||||
<li>除第一层外每一层的记录顺序均与上一层相反。即第一层为从左到右,第二层为从右到左。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1694758674-XYrUiV-%E5%89%91%E6%8C%87%20Offer%2032%20-%20I_%E7%A4%BA%E4%BE%8B1.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [8,17,21,18,null,null,6]
|
||||
<strong>输出:</strong>[[8],[21,17],[18,6]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>节点总数 <= 1000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,29 @@
|
||||
<p>给你一个整数 <code>n</code> ,请你找出所有可能含 <code>n</code> 个节点的 <strong>真二叉树</strong> ,并以列表形式返回。答案中每棵树的每个节点都必须符合 <code>Node.val == 0</code> 。</p>
|
||||
|
||||
<p>答案的每个元素都是一棵真二叉树的根节点。你可以按 <strong>任意顺序</strong> 返回最终的真二叉树列表<strong>。</strong></p>
|
||||
|
||||
<p><strong>真二叉树</strong> 是一类二叉树,树中每个节点恰好有 <code>0</code> 或 <code>2</code> 个子节点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/22/fivetrees.png" style="width: 700px; height: 400px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 7
|
||||
<strong>输出:</strong>[[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3
|
||||
<strong>输出:</strong>[[0,0,0]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 20</code></li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>给你两个字符串 <code>haystack</code> 和 <code>needle</code> ,请你在 <code>haystack</code> 字符串中找出 <code>needle</code> 字符串的第一个匹配项的下标(下标从 0 开始)。如果 <code>needle</code> 不是 <code>haystack</code> 的一部分,则返回 <code>-1</code><strong> </strong>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>haystack = "sadbutsad", needle = "sad"
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>"sad" 在下标 0 和 6 处匹配。
|
||||
第一个匹配项的下标是 0 ,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>haystack = "leetcode", needle = "leeto"
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>"leeto" 没有在 "leetcode" 中出现,所以返回 -1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= haystack.length, needle.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>haystack</code> 和 <code>needle</code> 仅由小写英文字符组成</li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>数对 <code>(a,b)</code> 由整数 <code>a</code> 和 <code>b</code> 组成,其数对距离定义为 <code>a</code> 和 <code>b</code> 的绝对差值。</p>
|
||||
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,数对由 <code>nums[i]</code> 和 <code>nums[j]</code> 组成且满足 <code>0 <= i < j < nums.length</code> 。返回 <strong>所有数对距离中</strong> 第 <code>k</code> 小的数对距离。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,3,1], k = 1
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>数对和对应的距离如下:
|
||||
(1,3) -> 2
|
||||
(1,1) -> 0
|
||||
(3,1) -> 2
|
||||
距离第 1 小的数对是 (1,1) ,距离为 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,1,1], k = 2
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,6,1], k = 3
|
||||
<strong>输出:</strong>5
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= n * (n - 1) / 2</code></li>
|
||||
</ul>
|
@@ -0,0 +1,29 @@
|
||||
<p>某班级学号记录系统发生错乱,原整数学号序列 <code>[0,1,2,3,4,...]</code> 分隔符丢失后变为 <code>01234...</code> 的字符序列。请实现一个函数返回该字符序列中的第 <code>k</code> 位数字。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>k = 5
|
||||
<strong>输出:</strong>5
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>k = 12
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>第 12 位数字在序列 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是 1 ,它是 11 的一部分。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= k < 2<sup>31</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意:本题与主站 400 题相同:<a href="https://leetcode-cn.com/problems/nth-digit/">https://leetcode-cn.com/problems/nth-digit/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,83 @@
|
||||
<p>请你来实现一个 <code>myAtoi(string s)</code> 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 <code>atoi</code> 函数)。</p>
|
||||
|
||||
<p>函数 <code>myAtoi(string s)</code> 的算法如下:</p>
|
||||
|
||||
<ol>
|
||||
<li>读入字符串并丢弃无用的前导空格</li>
|
||||
<li>检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。</li>
|
||||
<li>读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。</li>
|
||||
<li>将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 <code>0</code> 。必要时更改符号(从步骤 2 开始)。</li>
|
||||
<li>如果整数数超过 32 位有符号整数范围 <code>[−2<sup>31</sup>, 2<sup>31 </sup>− 1]</code> ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 <code>−2<sup>31</sup></code> 的整数应该被固定为 <code>−2<sup>31</sup></code> ,大于 <code>2<sup>31 </sup>− 1</code> 的整数应该被固定为 <code>2<sup>31 </sup>− 1</code> 。</li>
|
||||
<li>返回整数作为最终结果。</li>
|
||||
</ol>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>本题中的空白字符只包括空格字符 <code>' '</code> 。</li>
|
||||
<li>除前导空格或数字后的其余字符串外,<strong>请勿忽略</strong> 任何其他字符。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "42"
|
||||
<strong>输出:</strong>42
|
||||
<strong>解释:</strong>加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
|
||||
第 1 步:"42"(当前没有读入字符,因为没有前导空格)
|
||||
^
|
||||
第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
|
||||
^
|
||||
第 3 步:"<u>42</u>"(读入 "42")
|
||||
^
|
||||
解析得到整数 42 。
|
||||
由于 "42" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 42 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = " -42"
|
||||
<strong>输出:</strong>-42
|
||||
<strong>解释:</strong>
|
||||
第 1 步:"<u><strong> </strong></u>-42"(读入前导空格,但忽视掉)
|
||||
^
|
||||
第 2 步:" <u><strong>-</strong></u>42"(读入 '-' 字符,所以结果应该是负数)
|
||||
^
|
||||
第 3 步:" <u><strong>-42</strong></u>"(读入 "42")
|
||||
^
|
||||
解析得到整数 -42 。
|
||||
由于 "-42" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 -42 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "4193 with words"
|
||||
<strong>输出:</strong>4193
|
||||
<strong>解释:</strong>
|
||||
第 1 步:"4193 with words"(当前没有读入字符,因为没有前导空格)
|
||||
^
|
||||
第 2 步:"4193 with words"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
|
||||
^
|
||||
第 3 步:"<u>4193</u> with words"(读入 "4193";由于下一个字符不是一个数字,所以读入停止)
|
||||
^
|
||||
解析得到整数 4193 。
|
||||
由于 "4193" 在范围 [-2<sup>31</sup>, 2<sup>31</sup> - 1] 内,最终结果为 4193 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= s.length <= 200</code></li>
|
||||
<li><code>s</code> 由英文字母(大写和小写)、数字(<code>0-9</code>)、<code>' '</code>、<code>'+'</code>、<code>'-'</code> 和 <code>'.'</code> 组成</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:本题与主站 8 题相同:<a href="https://leetcode-cn.com/problems/string-to-integer-atoi/">https://leetcode-cn.com/problems/string-to-integer-atoi/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,9 @@
|
||||
<p>实现一个十进制数字报数程序,请按照数字从小到大的顺序返回一个整数数列,该数列从数字 <code>1</code> 开始,到最大的正整数 <code>cnt</code> 位数字结束。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>cnt = 2
|
||||
<strong>输出:</strong>[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99]</pre>
|
@@ -0,0 +1,43 @@
|
||||
<p>某套连招动作记作序列 <code>arr</code>,其中 <code>arr[i]</code> 为第 <code>i</code> 个招式的名字。请返回 <code>arr</code> 中最多可以出连续不重复的多少个招式。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>arr = "dbascDdad"
|
||||
<strong>输出: </strong>6
|
||||
<strong>解释:</strong> 因为连续且最长的招式序列是 "dbascD" 或 "bascDd",所以其长度为 6。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>arr = "KKK"
|
||||
<strong>输出: </strong>1
|
||||
<strong>解释: </strong>因为无重复字符的最长子串是 <code>"K"</code>,所以其长度为 1。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>arr = "pwwkew"
|
||||
<strong>输出: </strong>3
|
||||
<strong>解释: </strong>因为连续且最长的招式序列是 "wke",所以其长度为 3。
|
||||
请注意区分 <strong>子串</strong> 与 <strong>子序列</strong> 的概念:你的答案必须是 <strong>连续招式</strong> 的长度,也就是 <strong>子串</strong>。而 "pwke" 是一个非连续的 <strong>子序列</strong>,不是 <strong>子串</strong>。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>提示:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= arr.length <= 40000</code></li>
|
||||
<li><code>arr</code> 由英文字母、数字、符号和空格组成。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:本题与主站 3 题相同:<a href="https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/">https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,23 @@
|
||||
<p>某套连招动作记作仅由小写字母组成的序列 <code>arr</code>,其中 <code>arr[i]</code> 第 <code>i</code> 个招式的名字。请返回第一个只出现一次的招式名称,如不存在请返回空格。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = "abbccdeff"
|
||||
<strong>输出:</strong>'a'
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arr = "ccdd"
|
||||
<strong>输出:</strong>' '
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>限制:</strong></p>
|
||||
|
||||
<p><code>0 <= arr.length <= 50000</code></p>
|
@@ -0,0 +1,46 @@
|
||||
<p>产品数据表: <code>Products</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| product_id | int |
|
||||
| new_price | int |
|
||||
| change_date | date |
|
||||
+---------------+---------+
|
||||
(product_id, change_date) 是此表的主键(具有唯一值的列组合)。
|
||||
这张表的每一行分别记录了 某产品 在某个日期 更改后 的新价格。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写一个解决方案,找出在 <code>2019-08-16</code><strong> </strong>时全部产品的价格,假设所有产品在修改前的价格都是 <code>10</code><strong> 。</strong></p>
|
||||
|
||||
<p>以 <strong>任意顺序 </strong>返回结果表。</p>
|
||||
|
||||
<p>结果格式如下例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Products 表:
|
||||
+------------+-----------+-------------+
|
||||
| product_id | new_price | change_date |
|
||||
+------------+-----------+-------------+
|
||||
| 1 | 20 | 2019-08-14 |
|
||||
| 2 | 50 | 2019-08-14 |
|
||||
| 1 | 30 | 2019-08-15 |
|
||||
| 1 | 35 | 2019-08-16 |
|
||||
| 2 | 65 | 2019-08-17 |
|
||||
| 3 | 20 | 2019-08-18 |
|
||||
+------------+-----------+-------------+
|
||||
<strong>输出:</strong>
|
||||
+------------+-------+
|
||||
| product_id | price |
|
||||
+------------+-------+
|
||||
| 2 | 50 |
|
||||
| 1 | 35 |
|
||||
| 3 | 10 |
|
||||
+------------+-------+</pre>
|
@@ -0,0 +1,58 @@
|
||||
<p>表: <code>Accounts</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| 列名 | 类型 |
|
||||
+-------------+------+
|
||||
| account_id | int |
|
||||
| income | int |
|
||||
+-------------+------+
|
||||
在 SQL 中,account_id 是这个表的主键。
|
||||
每一行都包含一个银行帐户的月收入的信息。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>查询每个工资类别的银行账户数量。 工资类别如下:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>"Low Salary"</code>:所有工资 <strong>严格低于</strong> <code>20000</code> 美元。</li>
|
||||
<li><code>"Average Salary"</code>: <strong>包含</strong> 范围内的所有工资 <code>[$20000, $50000]</code> 。</li>
|
||||
<li>
|
||||
<p><code>"High Salary"</code>:所有工资 <strong>严格大于</strong> <code>50000</code> 美元。</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>结果表 <strong>必须</strong> 包含所有三个类别。 如果某个类别中没有帐户,则报告 <code>0</code> 。</p>
|
||||
|
||||
<p>按 <strong>任意顺序</strong> 返回结果表。</p>
|
||||
|
||||
<p>查询结果格式如下示例。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
Accounts 表:
|
||||
+------------+--------+
|
||||
| account_id | income |
|
||||
+------------+--------+
|
||||
| 3 | 108939 |
|
||||
| 2 | 12747 |
|
||||
| 8 | 87709 |
|
||||
| 6 | 91796 |
|
||||
+------------+--------+
|
||||
<strong>输出:</strong>
|
||||
+----------------+----------------+
|
||||
| category | accounts_count |
|
||||
+----------------+----------------+
|
||||
| Low Salary | 1 |
|
||||
| Average Salary | 0 |
|
||||
| High Salary | 3 |
|
||||
+----------------+----------------+
|
||||
<strong>解释:</strong>
|
||||
低薪: 有一个账户 2.
|
||||
中等薪水: 没有.
|
||||
高薪: 有三个账户,他们是 3, 6和 8.</pre>
|
@@ -0,0 +1,55 @@
|
||||
<p>给出长度相同的两个字符串<code>s1</code> 和 <code>s2</code> ,还有一个字符串 <code>baseStr</code> 。</p>
|
||||
|
||||
<p>其中 <code>s1[i]</code> 和 <code>s2[i]</code> 是一组等价字符。</p>
|
||||
|
||||
<ul>
|
||||
<li>举个例子,如果 <code>s1 = "abc"</code> 且 <code>s2 = "cde"</code>,那么就有 <code>'a' == 'c', 'b' == 'd', 'c' == 'e'</code>。</li>
|
||||
</ul>
|
||||
|
||||
<p>等价字符遵循任何等价关系的一般规则:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong> 自反性 </strong>:<code>'a' == 'a'</code></li>
|
||||
<li> <strong>对称性 </strong>:<code>'a' == 'b'</code> 则必定有 <code>'b' == 'a'</code></li>
|
||||
<li> <strong>传递性</strong> :<code>'a' == 'b'</code> 且 <code>'b' == 'c'</code> 就表明 <code>'a' == 'c'</code></li>
|
||||
</ul>
|
||||
|
||||
<p>例如, <code>s1 = "abc"</code> 和 <code>s2 = "cde"</code> 的等价信息和之前的例子一样,那么 <code>baseStr = "eed"</code> , <code>"acd"</code> 或 <code>"aab"</code>,这三个字符串都是等价的,而 <code>"aab"</code> 是 <code>baseStr</code> 的按字典序最小的等价字符串</p>
|
||||
|
||||
<p>利用<em> </em><code>s1</code> 和 <code>s2</code> 的等价信息,找出并返回<em> </em><code>baseStr</code><em> </em>的按字典序排列最小的等价字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s1 = "parker", s2 = "morris", baseStr = "parser"
|
||||
<strong>输出:</strong>"makkek"
|
||||
<strong>解释:</strong>根据 <code>A</code> 和 <code>B 中的等价信息,</code>我们可以将这些字符分为 <code>[m,p]</code>, <code>[a,o]</code>, <code>[k,r,s]</code>, <code>[e,i] 共 4 组</code>。每组中的字符都是等价的,并按字典序排列。所以答案是 <code>"makkek"</code>。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s1 = "hello", s2 = "world", baseStr = "hold"
|
||||
<strong>输出:</strong>"hdld"
|
||||
<strong>解释:</strong>根据 <code>A</code> 和 <code>B 中的等价信息,</code>我们可以将这些字符分为 <code>[h,w]</code>, <code>[d,e,o]</code>, <code>[l,r] 共 3 组</code>。所以只有 S 中的第二个字符 <code>'o'</code> 变成 <code>'d',最后答案为 </code><code>"hdld"</code>。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
|
||||
<strong>输出:</strong>"aauaaaaada"
|
||||
<strong>解释:</strong>我们可以把 A 和 B 中的等价字符分为 <code>[a,o,e,r,s,c]</code>, <code>[l,p]</code>, <code>[g,t]</code> 和 <code>[d,m] 共 4 组</code>,因此 <code>S</code> 中除了 <code>'u'</code> 和 <code>'d'</code> 之外的所有字母都转化成了 <code>'a'</code>,最后答案为 <code>"aauaaaaada"</code>。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s1.length, s2.length, baseStr <= 1000</code></li>
|
||||
<li><code>s1.length == s2.length</code></li>
|
||||
<li>字符串<code>s1</code>, <code>s2</code>, and <code>baseStr</code> 仅由从 <code>'a'</code> 到 <code>'z'</code> 的小写英文字母组成。</li>
|
||||
</ul>
|
@@ -0,0 +1,21 @@
|
||||
<p>为了深入了解这些生物群体的生态特征,你们进行了大量的实地观察和数据采集。数组 <code>arrayA</code> 记录了各个生物群体数量数据,其中 <code>arrayA[i]</code> 表示第 <code>i</code> 个生物群体的数量。请返回一个数组 <code>arrayB</code>,该数组为基于数组 <code>arrayA</code> 中的数据计算得出的结果,其中 <code>arrayB[i]</code> 表示将第 <code>i</code> 个生物群体的数量从总体中排除后的其他数量的乘积。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>arrayA = [2, 4, 6, 8, 10]
|
||||
<strong>输出:</strong>[1920, 960, 640, 480, 384]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>所有元素乘积之和不会溢出 32 位整数</li>
|
||||
<li><code>arrayA.length <= 100000</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
40
leetcode-cn/problem (Chinese)/换水问题 [water-bottles].html
Normal file
40
leetcode-cn/problem (Chinese)/换水问题 [water-bottles].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>超市正在促销,你可以用 <code>numExchange</code> 个空水瓶从超市兑换一瓶水。最开始,你一共购入了 <code>numBottles</code> 瓶水。</p>
|
||||
|
||||
<p>如果喝掉了水瓶中的水,那么水瓶就会变成空的。</p>
|
||||
|
||||
<p>给你两个整数 <code>numBottles</code> 和 <code>numExchange</code> ,返回你 <strong>最多</strong> 可以喝到多少瓶水。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/07/19/sample_1_1875.png" style="height: 240px; width: 480px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>numBottles = 9, numExchange = 3
|
||||
<strong>输出:</strong>13
|
||||
<strong>解释:</strong>你可以用 <code>3</code> 个空瓶兑换 1 瓶水。
|
||||
所以最多能喝到 9 + 3 + 1 = 13 瓶水。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/07/19/sample_2_1875.png" style="height: 240px; width: 790px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>numBottles = 15, numExchange = 4
|
||||
<strong>输出:</strong>19
|
||||
<strong>解释:</strong>你可以用 <code>4</code> 个空瓶兑换 1 瓶水。
|
||||
所以最多能喝到 15 + 3 + 1 = 19 瓶水。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= numBottles <= 100</code></li>
|
||||
<li><code>2 <= numExchange <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>某二叉树的先序遍历结果记录于整数数组 <code>preorder</code>,它的中序遍历结果记录于整数数组 <code>inorder</code>。请根据 <code>preorder</code> 和 <code>inorder</code> 的提示构造出这棵二叉树并返回其根节点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:<code>preorder</code> 和 <code>inorder</code> 中均不含重复数字。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
|
||||
|
||||
<strong>输出: </strong>[3,9,20,null,null,15,7]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>preorder = [-1], inorder = [-1]
|
||||
|
||||
<strong>输出:</strong> [-1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= preorder.length <= 3000</code></li>
|
||||
<li><code>inorder.length == preorder.length</code></li>
|
||||
<li><code>-3000 <= preorder[i], inorder[i] <= 3000</code></li>
|
||||
<li><code>inorder</code> 均出现在 <code>preorder</code></li>
|
||||
<li><code>preorder</code> 保证 为二叉树的前序遍历序列</li>
|
||||
<li><code>inorder</code> 保证 为二叉树的中序遍历序列</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:本题与主站 105 题重复:<a href="https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/" rel="noopener noreferrer" target="_blank">https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,24 @@
|
||||
<p>整数数组 <code>sockets</code> 记录了一个袜子礼盒的颜色分布情况,其中 <code>sockets[i]</code> 表示该袜子的颜色编号。礼盒中除了一款撞色搭配的袜子,每种颜色的袜子均有两只。请设计一个程序,在时间复杂度 O(n),空间复杂度O(1) 内找到这双撞色搭配袜子的两个颜色编号。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>sockets = [4, 5, 2, 4, 6, 6]
|
||||
<strong>输出:</strong>[2,5] 或 [5,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>sockets = [1, 2, 4, 1, 4, 3, 12, 3]
|
||||
<strong>输出:</strong>[2,12] 或 [12,2]</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= sockets.length <= 10000</code></li>
|
||||
</ul>
|
40
leetcode-cn/problem (Chinese)/改变数据类型 [change-data-type].html
Normal file
40
leetcode-cn/problem (Chinese)/改变数据类型 [change-data-type].html
Normal file
@@ -0,0 +1,40 @@
|
||||
<pre>
|
||||
DataFrame <code>students</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| student_id | int |
|
||||
| name | object |
|
||||
| age | int |
|
||||
| grade | float |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>编写一个解决方案来纠正以下错误:</p>
|
||||
|
||||
<p> <code>grade</code> 列被存储为浮点数,将它转换为整数。</p>
|
||||
|
||||
<p>返回结果格式如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:
|
||||
</strong>DataFrame students:
|
||||
+------------+------+-----+-------+
|
||||
| student_id | name | age | grade |
|
||||
+------------+------+-----+-------+
|
||||
| 1 | Ava | 6 | 73.0 |
|
||||
| 2 | Kate | 15 | 87.0 |
|
||||
+------------+------+-----+-------+
|
||||
<strong>输出:
|
||||
</strong>+------------+------+-----+-------+
|
||||
| student_id | name | age | grade |
|
||||
+------------+------+-----+-------+
|
||||
| 1 | Ava | 6 | 73 |
|
||||
| 2 | Kate | 15 | 87 |
|
||||
+------------+------+-----+-------+
|
||||
<b>解释:</b>
|
||||
grade 列的数据类型已转换为整数。</pre>
|
@@ -0,0 +1,28 @@
|
||||
<p>给定一个整数 <code>num</code>,计算所有小于等于 <code>num</code> 的非负整数中数字 <code>1</code> 出现的个数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 0
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 13
|
||||
<strong>输出:</strong>6</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= num < 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意:本题与主站 233 题相同:<a href="https://leetcode-cn.com/problems/number-of-digit-one/">https://leetcode-cn.com/problems/number-of-digit-one/</a></p>
|
||||
|
||||
<p> </p>
|
38
leetcode-cn/problem (Chinese)/数据选取 [select-data].html
Normal file
38
leetcode-cn/problem (Chinese)/数据选取 [select-data].html
Normal file
@@ -0,0 +1,38 @@
|
||||
<pre>
|
||||
DataFrame students
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| student_id | int |
|
||||
| name | object |
|
||||
| age | int |
|
||||
+-------------+--------+
|
||||
|
||||
</pre>
|
||||
|
||||
<p>编写一个解决方案,选择 <code>student_id = 101</code> 的学生的 name 和 age 并输出。</p>
|
||||
|
||||
<p>返回结果格式如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
+------------+---------+-----+
|
||||
| student_id | name | age |
|
||||
+------------+---------+-----+
|
||||
| 101 | Ulysses | 13 |
|
||||
| 53 | William | 10 |
|
||||
| 128 | Henry | 6 |
|
||||
| 3 | Henry | 11 |
|
||||
+------------+---------+-----+
|
||||
<b>输出:</b>
|
||||
+---------+-----+
|
||||
| name | age |
|
||||
+---------+-----+
|
||||
| Ulysses | 13 |
|
||||
+---------+-----+
|
||||
<strong>解释:
|
||||
</strong>学生 Ulysses 的 student_id = 101,所以我们输出了他的 name 和 age。</pre>
|
@@ -0,0 +1,46 @@
|
||||
<pre>
|
||||
DataFrame <code>weather</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| city | object |
|
||||
| month | object |
|
||||
| temperature | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>编写一个解决方案,以便将数据 <strong>旋转</strong>,使得每一行代表特定月份的温度,而每个城市都是一个单独的列。</p>
|
||||
|
||||
<p>输出结果格式如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
<b>示例 1:</b>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
+--------------+----------+-------------+
|
||||
| city | month | temperature |
|
||||
+--------------+----------+-------------+
|
||||
| Jacksonville | January | 13 |
|
||||
| Jacksonville | February | 23 |
|
||||
| Jacksonville | March | 38 |
|
||||
| Jacksonville | April | 5 |
|
||||
| Jacksonville | May | 34 |
|
||||
| ElPaso | January | 20 |
|
||||
| ElPaso | February | 6 |
|
||||
| ElPaso | March | 26 |
|
||||
| ElPaso | April | 2 |
|
||||
| ElPaso | May | 43 |
|
||||
+--------------+----------+-------------+
|
||||
<code><b>输出:</b>
|
||||
+----------+--------+--------------+
|
||||
| month | ElPaso | Jacksonville |
|
||||
+----------+--------+--------------+
|
||||
| April | 2 | 5 |
|
||||
| February | 6 | 23 |
|
||||
| January | 20 | 13 |
|
||||
| March | 26 | 38 |
|
||||
| May | 43 | 34 |
|
||||
+----------+--------+--------------+</code>
|
||||
<strong>解释:
|
||||
</strong>表格被旋转,每一列代表一个城市,每一行代表特定的月份。</pre>
|
@@ -0,0 +1,36 @@
|
||||
<p>待传输文件被切分成多个部分,按照原排列顺序,每部分文件编号均为一个 <strong>正整数</strong>(至少含有两个文件)。传输要求为:连续文件编号总和为接收方指定数字 <code>target</code> 的所有文件。请返回所有符合该要求的文件传输组合列表。</p>
|
||||
|
||||
<p><strong>注意</strong>,返回时需遵循以下规则:</p>
|
||||
|
||||
<ul>
|
||||
<li>每种组合按照文件编号 <strong>升序</strong> 排列;</li>
|
||||
<li>不同组合按照第一个文件编号 <strong>升序</strong> 排列。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>target = 12
|
||||
<strong>输出:</strong>[[3, 4, 5]]
|
||||
<strong>解释:</strong>在上述示例中,存在一个连续正整数序列的和为 12,为 [3, 4, 5]。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>target = 18
|
||||
<strong>输出:</strong>[[3,4,5,6],[5,6,7]]
|
||||
<strong>解释:</strong>在上述示例中,存在两个连续正整数序列的和分别为 18,分别为 [3, 4, 5, 6] 和 [5, 6, 7]。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>提示:</b></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= target <= 10^5</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,30 @@
|
||||
<p>展览馆展出来自 13 个朝代的文物,每排展柜展出 5 个文物。某排文物的摆放情况记录于数组 <code>places</code>,其中 <code>places[i]</code> 表示处于第 <code>i</code> 位文物的所属朝代编号。其中,编号为 0 的朝代表示未知朝代。请判断并返回这排文物的所属朝代编号是否连续(如遇未知朝代可算作连续情况)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>places = [0, 6, 9, 0, 7]
|
||||
<strong>输出: </strong>True
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>places = [7, 8, 9, 10, 11]
|
||||
<strong>输出:</strong> True
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>places.length = 5</code></li>
|
||||
<li><code>0 <= places[i] <= 13</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,46 @@
|
||||
<p><strong>斐波那契数</strong> (通常用 <code>F(n)</code> 表示)形成的序列称为 <strong>斐波那契数列</strong> 。该数列由 <strong>0</strong> 和 <strong>1</strong> 开始,后面的每一项数字都是前面两项数字的和。也就是:</p>
|
||||
|
||||
<pre>
|
||||
F(0) = 0,F(1) = 1
|
||||
F(n) = F(n - 1) + F(n - 2),其中 n > 1
|
||||
</pre>
|
||||
|
||||
<p>给定 <code>n</code> ,请计算 <code>F(n)</code> 。</p>
|
||||
|
||||
<p>答案需要取模 1e9+7(1000000007) ,如计算初始结果为:1000000008,请返回 1。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>F(2) = F(1) + F(0) = 1 + 0 = 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>F(3) = F(2) + F(1) = 1 + 1 = 2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>F(4) = F(3) + F(2) = 2 + 1 = 3
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 100</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
54
leetcode-cn/problem (Chinese)/方法链 [method-chaining].html
Normal file
54
leetcode-cn/problem (Chinese)/方法链 [method-chaining].html
Normal file
@@ -0,0 +1,54 @@
|
||||
<pre>
|
||||
DataFrame <code>animals</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| name | object |
|
||||
| species | object |
|
||||
| age | int |
|
||||
| weight | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>编写一个解决方案来列出体重 <strong>严格超过 </strong> <code>100</code> 千克的动物的名称。</p>
|
||||
|
||||
<p>按体重 <strong>降序</strong> 返回动物。</p>
|
||||
|
||||
<p>返回结果格式如下示例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>示例 1:</b></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
DataFrame animals:
|
||||
+----------+---------+-----+--------+
|
||||
| name | species | age | weight |
|
||||
+----------+---------+-----+--------+
|
||||
| Tatiana | Snake | 98 | 464 |
|
||||
| Khaled | Giraffe | 50 | 41 |
|
||||
| Alex | Leopard | 6 | 328 |
|
||||
| Jonathan | Monkey | 45 | 463 |
|
||||
| Stefan | Bear | 100 | 50 |
|
||||
| Tommy | Panda | 26 | 349 |
|
||||
+----------+---------+-----+--------+
|
||||
<b>输出:</b>
|
||||
+----------+
|
||||
| name |
|
||||
+----------+
|
||||
| Tatiana |
|
||||
| Jonathan |
|
||||
| Tommy |
|
||||
| Alex |
|
||||
+----------+
|
||||
<b>解释:</b>
|
||||
所有体重超过 100 的动物都应包含在结果表中。
|
||||
Tatiana 的体重为 464,Jonathan 的体重为 463,Tommy 的体重为 349,Alex 的体重为 328。
|
||||
结果应按体重降序排序。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>在 Pandas 中,<strong>方法链</strong> 允许我们在 DataFrame 上执行操作,而无需将每个操作拆分成单独的行或创建多个临时变量。</p>
|
||||
|
||||
<p>你能用 <strong>一行</strong> 代码的方法链完成这个任务吗?</p>
|
44
leetcode-cn/problem (Chinese)/无效的推文 [invalid-tweets].html
Normal file
44
leetcode-cn/problem (Chinese)/无效的推文 [invalid-tweets].html
Normal file
@@ -0,0 +1,44 @@
|
||||
<p>表:<code>Tweets</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+---------+
|
||||
| Column Name | Type |
|
||||
+----------------+---------+
|
||||
| tweet_id | int |
|
||||
| content | varchar |
|
||||
+----------------+---------+
|
||||
在 SQL 中,tweet_id 是这个表的主键。
|
||||
这个表包含某社交媒体 App 中所有的推文。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>查询所有无效推文的编号(ID)。当推文内容中的字符数<strong>严格大于</strong> <code>15</code> 时,该推文是无效的。</p>
|
||||
|
||||
<p>以<strong>任意顺序</strong>返回结果表。</p>
|
||||
|
||||
<p>查询结果格式如下所示:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Tweets 表:
|
||||
+----------+----------------------------------+
|
||||
| tweet_id | content |
|
||||
+----------+----------------------------------+
|
||||
| 1 | Vote for Biden |
|
||||
| 2 | Let us make America great again! |
|
||||
+----------+----------------------------------+
|
||||
|
||||
<strong>输出:</strong>
|
||||
+----------+
|
||||
| tweet_id |
|
||||
+----------+
|
||||
| 2 |
|
||||
+----------+
|
||||
<strong>解释:</strong>
|
||||
推文 1 的长度 length = 14。该推文是有效的。
|
||||
推文 2 的长度 length = 32。该推文是无效的。
|
||||
</pre>
|
@@ -0,0 +1,41 @@
|
||||
<pre>
|
||||
DataFrame: <code>employees</code>
|
||||
+-------------+--------+
|
||||
| Column Name | Type |
|
||||
+-------------+--------+
|
||||
| employee_id | int |
|
||||
| name | object |
|
||||
| department | object |
|
||||
| salary | int |
|
||||
+-------------+--------+
|
||||
</pre>
|
||||
|
||||
<p>编写一个解决方案,显示这个 DataFrame 的<strong> 前 <code>3</code> </strong>行。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>示例 1:</b></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:
|
||||
</strong>DataFrame employees
|
||||
+-------------+-----------+-----------------------+--------+
|
||||
| employee_id | name | department | salary |
|
||||
+-------------+-----------+-----------------------+--------+
|
||||
| 3 | Bob | Operations | 48675 |
|
||||
| 90 | Alice | Sales | 11096 |
|
||||
| 9 | Tatiana | Engineering | 33805 |
|
||||
| 60 | Annabelle | InformationTechnology | 37678 |
|
||||
| 49 | Jonathan | HumanResources | 23793 |
|
||||
| 43 | Khaled | Administration | 40454 |
|
||||
+-------------+-----------+-----------------------+--------+
|
||||
<b>输出:</b>
|
||||
+-------------+---------+-------------+--------+
|
||||
| employee_id | name | department | salary |
|
||||
+-------------+---------+-------------+--------+
|
||||
| 3 | Bob | Operations | 48675 |
|
||||
| 90 | Alice | Sales | 11096 |
|
||||
| 9 | Tatiana | Engineering | 33805 |
|
||||
+-------------+---------+-------------+--------+
|
||||
<b>解释:</b>
|
||||
只有前 3 行被显示。</pre>
|
@@ -0,0 +1,61 @@
|
||||
<p>表: <code>Queue</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| person_id | int |
|
||||
| person_name | varchar |
|
||||
| weight | int |
|
||||
| turn | int |
|
||||
+-------------+---------+
|
||||
person_id 是这个表具有唯一值的列。
|
||||
该表展示了所有候车乘客的信息。
|
||||
表中 person_id 和 turn 列将包含从 1 到 n 的所有数字,其中 n 是表中的行数。
|
||||
turn 决定了候车乘客上巴士的顺序,其中 turn=1 表示第一个上巴士,turn=n 表示最后一个上巴士。
|
||||
weight 表示候车乘客的体重,以千克为单位。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>有一队乘客在等着上巴士。然而,巴士有<code>1000</code> <strong>千克</strong> 的重量限制,所以其中一部分乘客可能无法上巴士。</p>
|
||||
|
||||
<p>编写解决方案找出 <strong>最后一个</strong> 上巴士且不超过重量限制的乘客,并报告 <code>person_name</code> 。题目测试用例确保顺位第一的人可以上巴士且不会超重。</p>
|
||||
|
||||
<p>返回结果格式如下所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Queue 表
|
||||
+-----------+-------------+--------+------+
|
||||
| person_id | person_name | weight | turn |
|
||||
+-----------+-------------+--------+------+
|
||||
| 5 | Alice | 250 | 1 |
|
||||
| 4 | Bob | 175 | 5 |
|
||||
| 3 | Alex | 350 | 2 |
|
||||
| 6 | John Cena | 400 | 3 |
|
||||
| 1 | Winston | 500 | 6 |
|
||||
| 2 | Marie | 200 | 4 |
|
||||
+-----------+-------------+--------+------+
|
||||
<strong>输出:</strong>
|
||||
+-------------+
|
||||
| person_name |
|
||||
+-------------+
|
||||
| John Cena |
|
||||
+-------------+
|
||||
<strong>解释:</strong>
|
||||
为了简化,Queue 表按 turn 列由小到大排序。
|
||||
+------+----+-----------+--------+--------------+
|
||||
| Turn | ID | Name | Weight | Total Weight |
|
||||
+------+----+-----------+--------+--------------+
|
||||
| 1 | 5 | Alice | 250 | 250 |
|
||||
| 2 | 3 | Alex | 350 | 600 |
|
||||
| 3 | 6 | John Cena | 400 | 1000 | (最后一个上巴士)
|
||||
| 4 | 2 | Marie | 200 | 1200 | (无法上巴士)
|
||||
| 5 | 4 | Bob | 175 | ___ |
|
||||
| 6 | 1 | Winston | 500 | ___ |
|
||||
+------+----+-----------+--------+--------------+</pre>
|
@@ -0,0 +1,32 @@
|
||||
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code> ,返回使所有数组元素相等需要的最小操作数。</p>
|
||||
|
||||
<p>在一次操作中,你可以使数组中的一个元素加 <code>1</code> 或者减 <code>1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,3]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
只需要两次操作(每次操作指南使一个元素加 1 或减 1):
|
||||
[<strong><em>1</em></strong>,2,3] => [2,2,<strong><em>3</em></strong>] => [2,2,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,10,2,9]
|
||||
<strong>输出:</strong>16
|
||||
</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>
|
||||
</ul>
|
@@ -0,0 +1,53 @@
|
||||
<p>请你设计一个 <strong>最小栈</strong> 。它提供 <code>push</code> ,<code>pop</code> ,<code>top</code> 操作,并能在常数时间内检索到最小元素的栈。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>实现 <code>MinStack</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>MinStack()</code> 初始化堆栈对象。</li>
|
||||
<li><code>void push(int val)</code> 将元素val推入堆栈。</li>
|
||||
<li><code>void pop()</code> 删除堆栈顶部的元素。</li>
|
||||
<li><code>int top()</code> 获取堆栈顶部的元素。</li>
|
||||
<li><code>int getMin()</code> 获取堆栈中的最小元素。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["MinStack","push","push","push","getMin","pop","top","getMin"]
|
||||
[[],[-2],[2],[-3],[],[],[],[]]
|
||||
|
||||
<strong>输出:</strong>
|
||||
[null,null,null,null,-3,null,2,-2]
|
||||
|
||||
<strong>解释:</strong>
|
||||
MinStack minStack = new MinStack();
|
||||
minStack.push(-2);
|
||||
minStack.push(2);
|
||||
minStack.push(-3);
|
||||
minStack.getMin(); --> 返回 -3.
|
||||
minStack.pop();
|
||||
minStack.top(); --> 返回 2.
|
||||
minStack.getMin(); --> 返回 -2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong> <br />
|
||||
提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code></li>
|
||||
<li><code>pop</code>、<code>top</code> 和 <code>getMin</code> 操作总是在 <strong>非空栈</strong> 上调用</li>
|
||||
<li><code>push</code>、<code>pop</code>、<code>top</code> 和 <code>getMin</code> 最多被调用 <code>3 * 10<sup>4</sup></code> 次</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:本题与主站 155 题相同:<a href="https://leetcode-cn.com/problems/min-stack/">https://leetcode-cn.com/problems/min-stack/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,30 @@
|
||||
<p>科技馆内有一台虚拟观景望远镜,它可以用来观测特定纬度地区的地形情况。该纬度的海拔数据记于数组 <code>heights</code> ,其中 <code>heights[i]</code> 表示对应位置的海拔高度。请找出并返回望远镜视野范围 <code>limit</code> 内,可以观测到的最高海拔值。</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>heights = [14,2,27,-5,28,13,39], limit = 3
|
||||
<strong>输出:</strong>[27,27,28,28,39]
|
||||
<strong>解释:</strong>
|
||||
滑动窗口的位置 最大值
|
||||
--------------- -----
|
||||
[14 2 27] -5 28 13 39 27
|
||||
14 [2 27 -5] 28 13 39 27
|
||||
14 2 [27 -5 28] 13 39 28
|
||||
14 2 27 [-5 28 13] 39 28
|
||||
14 2 27 -5 [28 13 39] 39</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<p>你可以假设输入总是有效的,在输入数组不为空的情况下:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= limit <= heights.length</code></li>
|
||||
<li><code>-10000 <= heights[i] <= 10000</code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意:本题与主站 239 题相同:<a href="https://leetcode-cn.com/problems/sliding-window-maximum/">https://leetcode-cn.com/problems/sliding-window-maximum/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,25 @@
|
||||
<p>购物车内的商品价格按照升序记录于数组 <code>price</code>。请在购物车中找到两个商品的价格总和刚好是 <code>target</code>。若存在多种情况,返回任一结果即可。</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>price = [3, 9, 12, 15], target = 18
|
||||
<strong>输出:</strong>[3,15] 或者 [15,3]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>price = [8, 21, 27, 34, 52, 66], target = 61
|
||||
<strong>输出:</strong>[27,34] 或者 [34,27]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= price.length <= 10^5</code></li>
|
||||
<li><code>1 <= price[i] <= 10^6</code></li>
|
||||
<li><code>1 <= target <= 2*10^6</code></li>
|
||||
</ul>
|
@@ -0,0 +1,61 @@
|
||||
<p>表: <code>Users</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| user_id | int |
|
||||
| name | varchar |
|
||||
| mail | varchar |
|
||||
+---------------+---------+
|
||||
user_id 是该表的主键(具有唯一值的列)。
|
||||
该表包含了网站已注册用户的信息。有一些电子邮件是无效的。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写一个解决方案,以查找具有有效电子邮件的用户。</p>
|
||||
|
||||
<p>一个有效的电子邮件具有前缀名称和域,其中:</p>
|
||||
|
||||
<ol>
|
||||
<li> <strong>前缀</strong> 名称是一个字符串,可以包含字母(大写或小写),数字,下划线 <code>'_'</code> ,点 <code>'.'</code> 和/或破折号 <code>'-'</code> 。前缀名称 <strong>必须</strong> 以字母开头。</li>
|
||||
<li><strong>域</strong> 为 <code>'@leetcode.com'</code> 。</li>
|
||||
</ol>
|
||||
|
||||
<p>以任何顺序返回结果表。</p>
|
||||
|
||||
<p>结果的格式如以下示例所示:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
Users 表:
|
||||
+---------+-----------+-------------------------+
|
||||
| user_id | name | mail |
|
||||
+---------+-----------+-------------------------+
|
||||
| 1 | Winston | winston@leetcode.com |
|
||||
| 2 | Jonathan | jonathanisgreat |
|
||||
| 3 | Annabelle | bella-@leetcode.com |
|
||||
| 4 | Sally | sally.come@leetcode.com |
|
||||
| 5 | Marwan | quarz#2020@leetcode.com |
|
||||
| 6 | David | david69@gmail.com |
|
||||
| 7 | Shapiro | .shapo@leetcode.com |
|
||||
+---------+-----------+-------------------------+
|
||||
<b>输出:</b>
|
||||
+---------+-----------+-------------------------+
|
||||
| user_id | name | mail |
|
||||
+---------+-----------+-------------------------+
|
||||
| 1 | Winston | winston@leetcode.com |
|
||||
| 3 | Annabelle | bella-@leetcode.com |
|
||||
| 4 | Sally | sally.come@leetcode.com |
|
||||
+---------+-----------+-------------------------+
|
||||
<b>解释:</b>
|
||||
用户 2 的电子邮件没有域。
|
||||
用户 5 的电子邮件带有不允许的 '#' 符号。
|
||||
用户 6 的电子邮件没有 leetcode 域。
|
||||
用户 7 的电子邮件以点开头。
|
||||
</pre>
|
@@ -0,0 +1,35 @@
|
||||
<p>力扣数据中心有 <code>n</code> 台服务器,分别按从 <code>0</code> 到 <code>n-1</code> 的方式进行了编号。它们之间以 <strong>服务器到服务器</strong> 的形式相互连接组成了一个内部集群,连接是无向的。用 <code>connections</code> 表示集群网络,<code>connections[i] = [a, b]</code> 表示服务器 <code>a</code> 和 <code>b</code> 之间形成连接。任何服务器都可以直接或者间接地通过网络到达任何其他服务器。</p>
|
||||
|
||||
<p><strong>关键连接</strong><em> </em>是在该集群中的重要连接,假如我们将它移除,便会导致某些服务器无法访问其他服务器。</p>
|
||||
|
||||
<p>请你以任意顺序返回该集群内的所有 <strong>关键连接</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/original_images/critical-connections-in-a-network.png" style="height: 205px; width: 200px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
|
||||
<strong>输出:</strong>[[1,3]]
|
||||
<strong>解释:</strong>[[3,1]] 也是正确的。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 2, connections = [[0,1]]
|
||||
<b>输出:</b>[[0,1]]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>n - 1 <= connections.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li>不存在重复的连接</li>
|
||||
</ul>
|
@@ -0,0 +1,70 @@
|
||||
<p><code>Queries</code> 表: </p>
|
||||
|
||||
<pre>
|
||||
+-------------+---------+
|
||||
| Column Name | Type |
|
||||
+-------------+---------+
|
||||
| query_name | varchar |
|
||||
| result | varchar |
|
||||
| position | int |
|
||||
| rating | int |
|
||||
+-------------+---------+
|
||||
此表可能有重复的行。
|
||||
此表包含了一些从数据库中收集的查询信息。
|
||||
“位置”(<code>position</code>)列的值为 <strong>1</strong> 到 <strong>500</strong> 。
|
||||
“评分”(<code>rating</code>)列的值为 <strong>1</strong> 到 <strong>5</strong> 。评分小于 3 的查询被定义为质量很差的查询。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>将查询结果的质量 <code>quality</code> 定义为:</p>
|
||||
|
||||
<blockquote>
|
||||
<p>各查询结果的评分与其位置之间比率的平均值。</p>
|
||||
</blockquote>
|
||||
|
||||
<p>将劣质查询百分比 <code>poor_query_percentage</code> 为:</p>
|
||||
|
||||
<blockquote>
|
||||
<p>评分小于 3 的查询结果占全部查询结果的百分比。</p>
|
||||
</blockquote>
|
||||
|
||||
<p>编写解决方案,找出每次的 <code>query_name</code> 、 <code>quality</code> 和 <code>poor_query_percentage</code>。</p>
|
||||
|
||||
<p><code>quality</code> 和 <code>poor_query_percentage</code> 都应 <strong>四舍五入到小数点后两位</strong> 。</p>
|
||||
|
||||
<p>以 <strong>任意顺序</strong> 返回结果表。</p>
|
||||
|
||||
<p>结果格式如下所示:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Queries table:
|
||||
+------------+-------------------+----------+--------+
|
||||
| query_name | result | position | rating |
|
||||
+------------+-------------------+----------+--------+
|
||||
| Dog | Golden Retriever | 1 | 5 |
|
||||
| Dog | German Shepherd | 2 | 5 |
|
||||
| Dog | Mule | 200 | 1 |
|
||||
| Cat | Shirazi | 5 | 2 |
|
||||
| Cat | Siamese | 3 | 3 |
|
||||
| Cat | Sphynx | 7 | 4 |
|
||||
+------------+-------------------+----------+--------+
|
||||
<strong>输出:</strong>
|
||||
+------------+---------+-----------------------+
|
||||
| query_name | quality | poor_query_percentage |
|
||||
+------------+---------+-----------------------+
|
||||
| Dog | 2.50 | 33.33 |
|
||||
| Cat | 0.66 | 33.33 |
|
||||
+------------+---------+-----------------------+
|
||||
<strong>解释:</strong>
|
||||
Dog 查询结果的质量为 ((5 / 1) + (5 / 2) + (1 / 200)) / 3 = 2.50
|
||||
Dog 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33
|
||||
|
||||
Cat 查询结果的质量为 ((2 / 5) + (3 / 3) + (4 / 7)) / 3 = 0.66
|
||||
Cat 查询结果的劣质查询百分比为 (1 / 3) * 100 = 33.33
|
||||
</pre>
|
@@ -0,0 +1,54 @@
|
||||
<p>请设计一个程序来支持用户在文本编辑器中的模糊搜索功能。用户输入内容中可能使用到如下两种通配符:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'.'</code> 匹配任意单个字符。</li>
|
||||
<li><code>'*'</code> 匹配零个或多个前面的那一个元素。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>请返回用户输入内容 <code>input</code> 所有字符是否可以匹配原文字符串 <code>article</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>article = "aa", input = "a"
|
||||
<strong>输出:</strong> false
|
||||
<strong>解释:</strong> "a" 无法匹配 "aa" 整个字符串。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>article = "aa", input = "a*"
|
||||
<strong>输出:</strong> true
|
||||
<strong>解释:</strong> 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>article = "ab", input = ".*"
|
||||
<strong>输出:</strong> true
|
||||
<strong>解释:</strong> ".*" 表示可匹配零个或多个('*')任意字符('.')。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= article.length <= 20</code></li>
|
||||
<li><code>1 <= input.length <= 20</code></li>
|
||||
<li><code>article</code> 只包含从 <code>a-z</code> 的小写字母。</li>
|
||||
<li><code>input</code> 只包含从 <code>a-z</code> 的小写字母,以及字符 <code>.</code> 和 <code>*</code> 。</li>
|
||||
<li>保证每次出现字符 <code>*</code> 时,前面都匹配到有效的字符</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:本题与主站 10 题相同:<a href="https://leetcode-cn.com/problems/regular-expression-matching/">https://leetcode-cn.com/problems/regular-expression-matching/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,56 @@
|
||||
<p>表: <code>Teacher</code></p>
|
||||
|
||||
<pre>
|
||||
+-------------+------+
|
||||
| Column Name | Type |
|
||||
+-------------+------+
|
||||
| teacher_id | int |
|
||||
| subject_id | int |
|
||||
| dept_id | int |
|
||||
+-------------+------+
|
||||
在 SQL 中,(subject_id, dept_id) 是该表的主键。
|
||||
该表中的每一行都表示带有 teacher_id 的教师在系 dept_id 中教授科目 subject_id。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>查询每位老师在大学里教授的科目种类的数量。</p>
|
||||
|
||||
<p data-group="1-1">以 <strong>任意顺序</strong> 返回结果表。</p>
|
||||
|
||||
<p>查询结果格式示例如下。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Teacher 表:
|
||||
+------------+------------+---------+
|
||||
| teacher_id | subject_id | dept_id |
|
||||
+------------+------------+---------+
|
||||
| 1 | 2 | 3 |
|
||||
| 1 | 2 | 4 |
|
||||
| 1 | 3 | 3 |
|
||||
| 2 | 1 | 1 |
|
||||
| 2 | 2 | 1 |
|
||||
| 2 | 3 | 1 |
|
||||
| 2 | 4 | 1 |
|
||||
+------------+------------+---------+
|
||||
<strong>输出:</strong>
|
||||
+------------+-----+
|
||||
| teacher_id | cnt |
|
||||
+------------+-----+
|
||||
| 1 | 2 |
|
||||
| 2 | 4 |
|
||||
+------------+-----+
|
||||
<strong>解释:</strong>
|
||||
教师 1:
|
||||
- 他在 3、4 系教科目 2。
|
||||
- 他在 3 系教科目 3。
|
||||
教师 2:
|
||||
- 他在 1 系教科目 1。
|
||||
- 他在 1 系教科目 2。
|
||||
- 他在 1 系教科目 3。
|
||||
- 他在 1 系教科目 4。</pre>
|
@@ -0,0 +1,44 @@
|
||||
<p>Table: <code>Employees</code></p>
|
||||
|
||||
<pre>+-------------+----------+
|
||||
| Column Name | Type |
|
||||
+-------------+----------+
|
||||
| employee_id | int |
|
||||
| name | varchar |
|
||||
| reports_to | int |
|
||||
| age | int |
|
||||
+-------------+----------+
|
||||
employee_id 是这个表的主键.
|
||||
该表包含员工以及需要听取他们汇报的上级经理的ID的信息。 有些员工不需要向任何人汇报(reports_to 为空)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>对于此问题,我们将至少有一个其他员工需要向他汇报的员工,视为一个经理。</p>
|
||||
|
||||
<p>编写SQL查询需要听取汇报的所有经理的ID、名称、直接向该经理汇报的员工人数,以及这些员工的平均年龄,其中该平均年龄需要四舍五入到最接近的整数。</p>
|
||||
|
||||
<p>返回的结果集需要按照 <code>employee_id </code>进行排序。</p>
|
||||
|
||||
<p>查询结果的格式如下:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<pre>Employees table:
|
||||
+-------------+---------+------------+-----+
|
||||
| employee_id | name | reports_to | age |
|
||||
+-------------+---------+------------+-----+
|
||||
| 9 | Hercy | null | 43 |
|
||||
| 6 | Alice | 9 | 41 |
|
||||
| 4 | Bob | 9 | 36 |
|
||||
| 2 | Winston | null | 37 |
|
||||
+-------------+---------+------------+-----+
|
||||
|
||||
Result table:
|
||||
+-------------+-------+---------------+-------------+
|
||||
| employee_id | name | reports_count | average_age |
|
||||
+-------------+-------+---------------+-------------+
|
||||
| 9 | Hercy | 2 | 39 |
|
||||
+-------------+-------+---------------+-------------+
|
||||
Hercy 有两个需要向他汇报的员工, 他们是 Alice and Bob. 他们的平均年龄是 (41+36)/2 = 38.5, 四舍五入的结果是 39.
|
||||
</pre>
|
@@ -0,0 +1,68 @@
|
||||
<p>表: <code>Activity</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+---------+
|
||||
| Column Name | Type |
|
||||
+----------------+---------+
|
||||
| machine_id | int |
|
||||
| process_id | int |
|
||||
| activity_type | enum |
|
||||
| timestamp | float |
|
||||
+----------------+---------+
|
||||
该表展示了一家工厂网站的用户活动。
|
||||
(machine_id, process_id, activity_type) 是当前表的主键(具有唯一值的列的组合)。
|
||||
machine_id 是一台机器的ID号。
|
||||
process_id 是运行在各机器上的进程ID号。
|
||||
activity_type 是枚举类型 ('start', 'end')。
|
||||
timestamp 是浮点类型,代表当前时间(以秒为单位)。
|
||||
'start' 代表该进程在这台机器上的开始运行时间戳 , 'end' 代表该进程在这台机器上的终止运行时间戳。
|
||||
同一台机器,同一个进程都有一对开始时间戳和结束时间戳,而且开始时间戳永远在结束时间戳前面。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>现在有一个工厂网站由几台机器运行,每台机器上运行着 <strong>相同数量的进程</strong> 。编写解决方案,计算每台机器各自完成一个进程任务的平均耗时。</p>
|
||||
|
||||
<p>完成一个进程任务的时间指进程的<code>'end' 时间戳</code> 减去 <code>'start' 时间戳</code>。平均耗时通过计算每台机器上所有进程任务的总耗费时间除以机器上的总进程数量获得。</p>
|
||||
|
||||
<p>结果表必须包含<code>machine_id(机器ID)</code> 和对应的 <strong>average time(平均耗时)</strong> 别名 <code>processing_time</code>,且<strong>四舍五入保留3位小数。</strong></p>
|
||||
|
||||
<p>以 <strong>任意顺序</strong> 返回表。</p>
|
||||
|
||||
<p>具体参考例子如下。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Activity table:
|
||||
+------------+------------+---------------+-----------+
|
||||
| machine_id | process_id | activity_type | timestamp |
|
||||
+------------+------------+---------------+-----------+
|
||||
| 0 | 0 | start | 0.712 |
|
||||
| 0 | 0 | end | 1.520 |
|
||||
| 0 | 1 | start | 3.140 |
|
||||
| 0 | 1 | end | 4.120 |
|
||||
| 1 | 0 | start | 0.550 |
|
||||
| 1 | 0 | end | 1.550 |
|
||||
| 1 | 1 | start | 0.430 |
|
||||
| 1 | 1 | end | 1.420 |
|
||||
| 2 | 0 | start | 4.100 |
|
||||
| 2 | 0 | end | 4.512 |
|
||||
| 2 | 1 | start | 2.500 |
|
||||
| 2 | 1 | end | 5.000 |
|
||||
+------------+------------+---------------+-----------+
|
||||
<strong>输出:</strong>
|
||||
+------------+-----------------+
|
||||
| machine_id | processing_time |
|
||||
+------------+-----------------+
|
||||
| 0 | 0.894 |
|
||||
| 1 | 0.995 |
|
||||
| 2 | 1.456 |
|
||||
+------------+-----------------+
|
||||
<strong>解释:</strong>
|
||||
一共有3台机器,每台机器运行着两个进程.
|
||||
机器 0 的平均耗时: ((1.520 - 0.712) + (4.120 - 3.140)) / 2 = 0.894
|
||||
机器 1 的平均耗时: ((1.550 - 0.550) + (1.420 - 0.430)) / 2 = 0.995
|
||||
机器 2 的平均耗时: ((4.512 - 4.100) + (5.000 - 2.500)) / 2 = 1.456</pre>
|
@@ -0,0 +1,48 @@
|
||||
<p>表:<code>Transactions</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| id | int |
|
||||
| country | varchar |
|
||||
| state | enum |
|
||||
| amount | int |
|
||||
| trans_date | date |
|
||||
+---------------+---------+
|
||||
id 是这个表的主键。
|
||||
该表包含有关传入事务的信息。
|
||||
state 列类型为 ["approved", "declined"] 之一。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写一个 sql 查询来查找每个月和每个国家/地区的事务数及其总金额、已批准的事务数及其总金额。</p>
|
||||
|
||||
<p>以 <strong>任意顺序</strong> 返回结果表。</p>
|
||||
|
||||
<p>查询结果格式如下所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<code><strong>输入:</strong>
|
||||
Transactions</code> table:
|
||||
+------+---------+----------+--------+------------+
|
||||
| id | country | state | amount | trans_date |
|
||||
+------+---------+----------+--------+------------+
|
||||
| 121 | US | approved | 1000 | 2018-12-18 |
|
||||
| 122 | US | declined | 2000 | 2018-12-19 |
|
||||
| 123 | US | approved | 2000 | 2019-01-01 |
|
||||
| 124 | DE | approved | 2000 | 2019-01-07 |
|
||||
+------+---------+----------+--------+------------+
|
||||
<strong>输出:</strong>
|
||||
+----------+---------+-------------+----------------+--------------------+-----------------------+
|
||||
| month | country | trans_count | approved_count | trans_total_amount | approved_total_amount |
|
||||
+----------+---------+-------------+----------------+--------------------+-----------------------+
|
||||
| 2018-12 | US | 2 | 1 | 3000 | 1000 |
|
||||
| 2019-01 | US | 1 | 1 | 2000 | 2000 |
|
||||
| 2019-01 | DE | 1 | 1 | 2000 | 2000 |
|
||||
+----------+---------+-------------+----------------+--------------------+-----------------------+</pre>
|
@@ -0,0 +1,47 @@
|
||||
<p>Table: <code>Activity</code></p>
|
||||
|
||||
<pre>
|
||||
+--------------+---------+
|
||||
| Column Name | Type |
|
||||
+--------------+---------+
|
||||
| player_id | int |
|
||||
| device_id | int |
|
||||
| event_date | date |
|
||||
| games_played | int |
|
||||
+--------------+---------+
|
||||
(player_id,event_date)是此表的主键(具有唯一值的列的组合)。
|
||||
这张表显示了某些游戏的玩家的活动情况。
|
||||
每一行是一个玩家的记录,他在某一天使用某个设备注销之前登录并玩了很多游戏(可能是 0)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>编写解决方案,报告在首次登录的第二天再次登录的玩家的 <strong>比率</strong>,<strong>四舍五入到小数点后两位</strong>。换句话说,你需要计算从首次登录日期开始至少连续两天登录的玩家的数量,然后除以玩家总数。</p>
|
||||
|
||||
<p>结果格式如下所示:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Activity table:
|
||||
+-----------+-----------+------------+--------------+
|
||||
| player_id | device_id | event_date | games_played |
|
||||
+-----------+-----------+------------+--------------+
|
||||
| 1 | 2 | 2016-03-01 | 5 |
|
||||
| 1 | 2 | 2016-03-02 | 6 |
|
||||
| 2 | 3 | 2017-06-25 | 1 |
|
||||
| 3 | 1 | 2016-03-02 | 0 |
|
||||
| 3 | 4 | 2018-07-03 | 5 |
|
||||
+-----------+-----------+------------+--------------+
|
||||
<strong>输出:</strong>
|
||||
+-----------+
|
||||
| fraction |
|
||||
+-----------+
|
||||
| 0.33 |
|
||||
+-----------+
|
||||
<strong>解释:</strong>
|
||||
只有 ID 为 1 的玩家在第一天登录后才重新登录,所以答案是 1/3 = 0.33
|
||||
</pre>
|
@@ -0,0 +1,22 @@
|
||||
<p>某班级 n 位同学的学号为 0 ~ n-1。点名结果记录于升序数组 <code>records</code>。假定仅有一位同学缺席,请返回他的学号。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> records = [0,1,2,3,5]
|
||||
<strong>输出:</strong> 4
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> records = [0, 1, 2, 3, 4, 5, 6, 8]
|
||||
<strong>输出:</strong> 7</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>提示:</b></p>
|
||||
|
||||
<p><code>1 <= records.length <= 10000</code></p>
|
47
leetcode-cn/problem (Chinese)/爱吃香蕉的狒狒 [nZZqjQ].html
Normal file
47
leetcode-cn/problem (Chinese)/爱吃香蕉的狒狒 [nZZqjQ].html
Normal file
@@ -0,0 +1,47 @@
|
||||
<p>狒狒喜欢吃香蕉。这里有 <code>N</code> 堆香蕉,第 <code>i</code> 堆中有 <code>piles[i]</code> 根香蕉。警卫已经离开了,将在 <code>H</code> 小时后回来。</p>
|
||||
|
||||
<p>狒狒可以决定她吃香蕉的速度 <code>K</code> (单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 <code>K</code> 根。如果这堆香蕉少于 <code>K</code> 根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉,下一个小时才会开始吃另一堆的香蕉。 </p>
|
||||
|
||||
<p>狒狒喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。</p>
|
||||
|
||||
<p>返回她可以在 <code>H</code> 小时内吃掉所有香蕉的最小速度 <code>K</code>(<code>K</code> 为整数)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>piles = [3,6,7,11], H = 8
|
||||
<strong>输出: </strong>4
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>piles = [30,11,23,4,20], H = 5
|
||||
<strong>输出: </strong>30
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>piles = [30,11,23,4,20], H = 6
|
||||
<strong>输出: </strong>23
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= piles.length <= 10^4</code></li>
|
||||
<li><code>piles.length <= H <= 10^9</code></li>
|
||||
<li><code>1 <= piles[i] <= 10^9</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><meta charset="UTF-8" />注意:本题与主站 875 题相同: <a href="https://leetcode-cn.com/problems/koko-eating-bananas/">https://leetcode-cn.com/problems/koko-eating-bananas/</a></p>
|
@@ -0,0 +1,29 @@
|
||||
<p>现有一个记作二维矩阵 <code>frame</code> 的珠宝架,其中 <code>frame[i][j]</code> 为该位置珠宝的价值。拿取珠宝的规则为:</p>
|
||||
|
||||
<ul>
|
||||
<li>只能从架子的左上角开始拿珠宝</li>
|
||||
<li>每次可以移动到右侧或下侧的相邻位置</li>
|
||||
<li>到达珠宝架子的右下角时,停止拿取</li>
|
||||
</ul>
|
||||
|
||||
<p>注意:珠宝的价值都是大于 0 的。除非这个架子上没有任何珠宝,比如 <code>frame = [[0]]</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> frame = [[1,3,1],[1,5,1],[4,2,1]]
|
||||
<strong>输出:</strong> <code>12
|
||||
</code><strong>解释:</strong> 路径 1→3→5→2→1 可以拿到最高价值的珠宝</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 < frame.length <= 200</code></li>
|
||||
<li><code>0 < frame[0].length <= 200</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
103
leetcode-cn/problem (Chinese)/电影评分 [movie-rating].html
Normal file
103
leetcode-cn/problem (Chinese)/电影评分 [movie-rating].html
Normal file
@@ -0,0 +1,103 @@
|
||||
<p>表:<code>Movies</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| movie_id | int |
|
||||
| title | varchar |
|
||||
+---------------+---------+
|
||||
movie_id 是这个表的主键(具有唯一值的列)。
|
||||
title 是电影的名字。
|
||||
</pre>
|
||||
|
||||
<p>表:<code>Users</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| user_id | int |
|
||||
| name | varchar |
|
||||
+---------------+---------+
|
||||
user_id 是表的主键(具有唯一值的列)。
|
||||
</pre>
|
||||
|
||||
<p>表:<code>MovieRating</code></p>
|
||||
|
||||
<pre>
|
||||
+---------------+---------+
|
||||
| Column Name | Type |
|
||||
+---------------+---------+
|
||||
| movie_id | int |
|
||||
| user_id | int |
|
||||
| rating | int |
|
||||
| created_at | date |
|
||||
+---------------+---------+
|
||||
(movie_id, user_id) 是这个表的主键(具有唯一值的列的组合)。
|
||||
这个表包含用户在其评论中对电影的评分 rating 。
|
||||
created_at 是用户的点评日期。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>请你编写一个解决方案:</p>
|
||||
|
||||
<ul>
|
||||
<li>查找评论电影数量最多的用户名。如果出现平局,返回字典序较小的用户名。</li>
|
||||
<li>查找在 <code>February 2020</code><strong> 平均评分最高</strong> 的电影名称。如果出现平局,返回字典序较小的电影名称。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>字典序</strong> ,即按字母在字典中出现顺序对字符串排序,字典序较小则意味着排序靠前。</p>
|
||||
|
||||
<p>返回结果格式如下例所示。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
Movies 表:
|
||||
+-------------+--------------+
|
||||
| movie_id | title |
|
||||
+-------------+--------------+
|
||||
| 1 | Avengers |
|
||||
| 2 | Frozen 2 |
|
||||
| 3 | Joker |
|
||||
+-------------+--------------+
|
||||
Users 表:
|
||||
+-------------+--------------+
|
||||
| user_id | name |
|
||||
+-------------+--------------+
|
||||
| 1 | Daniel |
|
||||
| 2 | Monica |
|
||||
| 3 | Maria |
|
||||
| 4 | James |
|
||||
+-------------+--------------+
|
||||
MovieRating 表:
|
||||
+-------------+--------------+--------------+-------------+
|
||||
| movie_id | user_id | rating | created_at |
|
||||
+-------------+--------------+--------------+-------------+
|
||||
| 1 | 1 | 3 | 2020-01-12 |
|
||||
| 1 | 2 | 4 | 2020-02-11 |
|
||||
| 1 | 3 | 2 | 2020-02-12 |
|
||||
| 1 | 4 | 1 | 2020-01-01 |
|
||||
| 2 | 1 | 5 | 2020-02-17 |
|
||||
| 2 | 2 | 2 | 2020-02-01 |
|
||||
| 2 | 3 | 2 | 2020-03-01 |
|
||||
| 3 | 1 | 3 | 2020-02-22 |
|
||||
| 3 | 2 | 4 | 2020-02-25 |
|
||||
+-------------+--------------+--------------+-------------+
|
||||
<strong>输出:</strong>
|
||||
Result 表:
|
||||
+--------------+
|
||||
| results |
|
||||
+--------------+
|
||||
| Daniel |
|
||||
| Frozen 2 |
|
||||
+--------------+
|
||||
<strong>解释:</strong>
|
||||
Daniel 和 Monica 都点评了 3 部电影("Avengers", "Frozen 2" 和 "Joker") 但是 Daniel 字典序比较小。
|
||||
Frozen 2 和 Joker 在 2 月的评分都是 3.5,但是 Frozen 2 的字典序比较小。
|
||||
</pre>
|
@@ -0,0 +1,17 @@
|
||||
<p>现需要将一根长为正整数 <code>bamboo_len</code> 的竹子砍为若干段,每段长度均为正整数。请返回每段竹子长度的最大乘积是多少。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>bamboo_len<strong> </strong>=<strong> </strong>12
|
||||
<strong>输出: </strong>81
|
||||
</pre>
|
||||
<strong>提示:</strong>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= bamboo_len <= 58</code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意:本题与主站 343 题相同:<a href="https://leetcode-cn.com/problems/integer-break/">https://leetcode-cn.com/problems/integer-break/</a></p>
|
@@ -0,0 +1,24 @@
|
||||
<p>现需要将一根长为正整数 <code>bamboo_len</code> 的竹子砍为若干段,每段长度均为 <strong>正整数</strong>。请返回每段竹子长度的 <strong>最大乘积</strong> 是多少。</p>
|
||||
|
||||
<p>答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>bamboo_len = 12
|
||||
<strong>输出:</strong>81
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= bamboo_len <= 1000</code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意:本题与主站 343 题相同:<a href="https://leetcode-cn.com/problems/integer-break/">https://leetcode-cn.com/problems/integer-break/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,28 @@
|
||||
<p>社团共有 <code>num</code> 为成员参与破冰游戏,编号为 <code>0 ~ num-1</code>。成员们按照编号顺序围绕圆桌而坐。社长抽取一个数字 <code>target</code>,从 0 号成员起开始计数,排在第 <code>target</code> 位的成员离开圆桌,且成员离开后从下一个成员开始计数。请返回游戏结束时最后一位成员的编号。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 7, target = 4
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 12, target = 5
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 10^5</code></li>
|
||||
<li><code>1 <= target <= 10^6</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,39 @@
|
||||
<p>闯关游戏需要破解一组密码,闯关组给出的有关密码的线索是:</p>
|
||||
|
||||
<ul>
|
||||
<li>一个拥有密码所有元素的非负整数数组 <code>password</code></li>
|
||||
<li>密码是 <code>password</code> 中所有元素拼接后得到的最小的一个数</li>
|
||||
</ul>
|
||||
|
||||
<p>请编写一个程序返回这个密码。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>password = [15, 8, 7]
|
||||
<strong>输出: </strong>"1578"</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入: </strong>password = [0, 3, 30, 34, 5, 9]
|
||||
<strong>输出: </strong>"03033459"</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 < password.length <= 100</code></li>
|
||||
</ul>
|
||||
|
||||
<p><strong>说明: </strong></p>
|
||||
|
||||
<ul>
|
||||
<li>输出结果可能非常大,所以你需要返回一个字符串而不是整数</li>
|
||||
<li>拼接起来的数字可能会有前导 0,最后结果不需要去掉前导 0</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
80
leetcode-cn/problem (Chinese)/确认率 [confirmation-rate].html
Normal file
80
leetcode-cn/problem (Chinese)/确认率 [confirmation-rate].html
Normal file
@@ -0,0 +1,80 @@
|
||||
<p>表: <code>Signups</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+----------+
|
||||
| Column Name | Type |
|
||||
+----------------+----------+
|
||||
| user_id | int |
|
||||
| time_stamp | datetime |
|
||||
+----------------+----------+
|
||||
User_id是该表的主键。
|
||||
每一行都包含ID为user_id的用户的注册时间信息。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>表: <code>Confirmations</code></p>
|
||||
|
||||
<pre>
|
||||
+----------------+----------+
|
||||
| Column Name | Type |
|
||||
+----------------+----------+
|
||||
| user_id | int |
|
||||
| time_stamp | datetime |
|
||||
| action | ENUM |
|
||||
+----------------+----------+
|
||||
(user_id, time_stamp)是该表的主键。
|
||||
user_id是一个引用到注册表的外键。
|
||||
action是类型为('confirmed', 'timeout')的ENUM
|
||||
该表的每一行都表示ID为user_id的用户在time_stamp请求了一条确认消息,该确认消息要么被确认('confirmed'),要么被过期('timeout')。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>用户的 <strong>确认率</strong> 是 <code>'confirmed'</code> 消息的数量除以请求的确认消息的总数。没有请求任何确认消息的用户的确认率为 <code>0</code> 。确认率四舍五入到 <strong>小数点后两位</strong> 。</p>
|
||||
|
||||
<p>编写一个SQL查询来查找每个用户的 确认率 。<br />
|
||||
<br />
|
||||
以 任意顺序 返回结果表。<br />
|
||||
<br />
|
||||
查询结果格式如下所示。<br />
|
||||
<br />
|
||||
<strong>示例1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
Signups 表:
|
||||
+---------+---------------------+
|
||||
| user_id | time_stamp |
|
||||
+---------+---------------------+
|
||||
| 3 | 2020-03-21 10:16:13 |
|
||||
| 7 | 2020-01-04 13:57:59 |
|
||||
| 2 | 2020-07-29 23:09:44 |
|
||||
| 6 | 2020-12-09 10:39:37 |
|
||||
+---------+---------------------+
|
||||
Confirmations 表:
|
||||
+---------+---------------------+-----------+
|
||||
| user_id | time_stamp | action |
|
||||
+---------+---------------------+-----------+
|
||||
| 3 | 2021-01-06 03:30:46 | timeout |
|
||||
| 3 | 2021-07-14 14:00:00 | timeout |
|
||||
| 7 | 2021-06-12 11:57:29 | confirmed |
|
||||
| 7 | 2021-06-13 12:58:28 | confirmed |
|
||||
| 7 | 2021-06-14 13:59:27 | confirmed |
|
||||
| 2 | 2021-01-22 00:00:00 | confirmed |
|
||||
| 2 | 2021-02-28 23:59:59 | timeout |
|
||||
+---------+---------------------+-----------+
|
||||
<strong>输出:</strong>
|
||||
+---------+-------------------+
|
||||
| user_id | confirmation_rate |
|
||||
+---------+-------------------+
|
||||
| 6 | 0.00 |
|
||||
| 3 | 0.00 |
|
||||
| 7 | 1.00 |
|
||||
| 2 | 0.50 |
|
||||
+---------+-------------------+
|
||||
<strong>解释:
|
||||
</strong>用户 6 没有请求任何确认消息。确认率为 0。
|
||||
用户 3 进行了 2 次请求,都超时了。确认率为 0。
|
||||
用户 7 提出了 3 个请求,所有请求都得到了确认。确认率为 1。
|
||||
用户 2 做了 2 个请求,其中一个被确认,另一个超时。确认率为 1 / 2 = 0.5。</pre>
|
@@ -0,0 +1,32 @@
|
||||
<p>某班级考试成绩按非严格递增顺序记录于整数数组 <code>scores</code>,请返回目标成绩 <code>target</code> 的出现次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> scores = [2, 2, 3, 4, 4, 4, 5, 6, 6, 8], target = 4
|
||||
<strong>输出:</strong> 3</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> scores = [1, 2, 3, 5, 7, 9], target = 6
|
||||
<strong>输出:</strong> 0</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= scores.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= scores[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>scores</code> 是一个非递减数组</li>
|
||||
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>注意:</strong>本题与主站 34 题相同(仅返回值不同):<a href="https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/">https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/</a></p>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,29 @@
|
||||
<p>你选择掷出 <code>num</code> 个色子,请返回所有点数总和的概率。</p>
|
||||
|
||||
<p>你需要用一个浮点数数组返回答案,其中第 <code>i</code> 个元素代表这 <code>num</code> 个骰子所能掷出的点数集合中第 <code>i</code> 小的那个的概率。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 3
|
||||
<strong>输出:</strong>[0.00463,0.01389,0.02778,0.04630,0.06944,0.09722,0.11574,0.12500,0.12500,0.11574,0.09722,0.06944,0.04630,0.02778,0.01389,0.00463]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 5
|
||||
<strong>输出:</strong>[0.00013,0.00064,0.00193,0.00450,0.00900,0.01620,0.02636,0.03922,0.05401,0.06944,0.08372,0.09452,0.10031,0.10031,0.09452,0.08372,0.06944,0.05401,0.03922,0.02636,0.01620,0.00900,0.00450,0.00193,0.00064,0.00013]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 11</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
@@ -0,0 +1,27 @@
|
||||
<p>给定一棵二叉树的根节点 <code>root</code>,请左右翻转这棵二叉树,并返回其根节点。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode.cn/1694686821-qlvjod-%E7%BF%BB%E8%BD%AC%E4%BA%8C%E5%8F%89%E6%A0%91.png" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [5,7,9,8,3,2,4]
|
||||
<strong>输出:</strong>[5,9,7,4,2,3,8]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点数目范围在 <code>[0, 100]</code> 内</li>
|
||||
<li><code>-100 <= Node.val <= 100</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>注意:本题与主站 226 题相同:<a href="https://leetcode-cn.com/problems/invert-binary-tree/">https://leetcode-cn.com/problems/invert-binary-tree/</a></p>
|
||||
|
||||
<p> </p>
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user