Skip to content

Commit f523d1f

Browse files
committed
LeetCode 付费题目描述 1000~1300
1 parent 98df9c5 commit f523d1f

File tree

136 files changed

+5059
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+5059
-136
lines changed

solution/1000-1099/1055.Shortest Way to Form String/README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,42 @@
44

55
## 题目描述
66
<!-- 这里写题目描述 -->
7-
None
7+
<p>对于任何字符串,我们可以通过删除其中一些字符(也可能不删除)来构造该字符串的子序列。</p>
8+
9+
<p>给定源字符串 <code>source</code> 和目标字符串 <code>target</code>,找出源字符串中能通过串联形成目标字符串的子序列的最小数量。如果无法通过串联源字符串中的子序列来构造目标字符串,则返回 <code>-1</code>。</p>
10+
11+
<p> </p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
15+
<pre><strong>输入:</strong>source = "abc", target = "abcbc"
16+
<strong>输出:</strong>2
17+
<strong>解释:</strong>目标字符串 "abcbc" 可以由 "abc" 和 "bc" 形成,它们都是源字符串 "abc" 的子序列。
18+
</pre>
19+
20+
<p><strong>示例 2:</strong></p>
21+
22+
<pre><strong>输入:</strong>source = "abc", target = "acdbc"
23+
<strong>输出:</strong>-1
24+
<strong>解释:</strong>由于目标字符串中包含字符 "d",所以无法由源字符串的子序列构建目标字符串。
25+
</pre>
26+
27+
<p><strong>示例 3:</strong></p>
28+
29+
<pre><strong>输入:</strong>source = "xyz", target = "xzyxz"
30+
<strong>输出:</strong>3
31+
<strong>解释:</strong>目标字符串可以按如下方式构建: "xz" + "y" + "xz"。
32+
</pre>
33+
34+
<p> </p>
35+
36+
<p><strong>提示:</strong></p>
37+
38+
<ol>
39+
<li><code>source</code> 和 <code>target</code> 两个字符串都只包含 "a"-"z" 的英文小写字母。</li>
40+
<li><code>source</code> 和 <code>target</code> 两个字符串的长度介于 <code>1</code> 和 <code>1000</code> 之间。</li>
41+
</ol>
42+
843

944

1045
## 解法

solution/1000-1099/1055.Shortest Way to Form String/README_EN.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,42 @@
33
[中文文档](/solution/1000-1099/1055.Shortest%20Way%20to%20Form%20String/README.md)
44

55
## Description
6-
None
6+
<p>From any string, we can form a <i>subsequence</i> of that string by deleting some number of characters (possibly no deletions).</p>
7+
8+
<p>Given two strings <code>source</code> and <code>target</code>, return the minimum number of subsequences of <code>source</code> such that their concatenation equals <code>target</code>. If the task is impossible, return <code>-1</code>.</p>
9+
10+
<p> </p>
11+
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input: </strong>source = <span id="example-input-1-1">"abc"</span>, target = <span id="example-input-1-2">"abcbc"</span>
16+
<strong>Output: </strong><span id="example-output-1">2</span>
17+
<strong>Explanation: </strong>The target "abcbc" can be formed by "abc" and "bc", which are subsequences of source "abc".
18+
</pre>
19+
20+
<p><strong>Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input: </strong>source = <span id="example-input-2-1">"abc"</span>, target = <span id="example-input-2-2">"acdbc"</span>
24+
<strong>Output: </strong><span id="example-output-2">-1</span>
25+
<strong>Explanation: </strong>The target string cannot be constructed from the subsequences of source string due to the character "d" in target string.
26+
</pre>
27+
28+
<p><strong>Example 3:</strong></p>
29+
30+
<pre>
31+
<strong>Input: </strong>source = <span id="example-input-3-1">"xyz"</span>, target = <span id="example-input-3-2">"xzyxz"</span>
32+
<strong>Output: </strong><span id="example-output-3">3</span>
33+
<strong>Explanation: </strong>The target string can be constructed as follows "xz" + "y" + "xz".
34+
</pre>
35+
<p> </p>
36+
<p><strong>Constraints:</strong></p>
37+
38+
<ul>
39+
<li>Both the <code>source</code> and <code>target</code> strings consist of only lowercase English letters from "a"-"z".</li>
40+
<li>The lengths of <code>source</code> and <code>target</code> string are between <code>1</code> and <code>1000</code>.</li>
41+
</ul>
742

843

944
## Solutions

solution/1000-1099/1056.Confusing Number/README.md

+61-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,67 @@
44

55
## 题目描述
66
<!-- 这里写题目描述 -->
7-
None
7+
<p>给定一个数字 <code>N</code>,当它满足以下条件的时候返回 <code>true</code>:</p>
8+
9+
<p>原数字旋转 180° 以后可以得到新的数字。</p>
10+
11+
<p>如 0, 1, 6, 8, 9 旋转 180° 以后,得到了新的数字 0, 1, 9, 8, 6 。</p>
12+
13+
<p>2, 3, 4, 5, 7 旋转 180° 后,得到的<strong>不是</strong>数字。</p>
14+
15+
<p>易混淆数 (confusing number) 在旋转180°以后,可以得到和原来<strong>不同</strong>的数,且新数字的每一位都是有效的。</p>
16+
17+
<p> </p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/23/1268_1.png" style="height: 90px; width: 180px;"></p>
22+
23+
<pre><strong>输入:</strong>6
24+
<strong>输出:</strong>true
25+
<strong>解释:
26+
</strong>把 6 旋转 180° 以后得到 9,9 是有效数字且 9!=6 。
27+
</pre>
28+
29+
<p><strong>示例 2:</strong></p>
30+
31+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/23/1268_2.png" style="height: 90px; width: 180px;"></p>
32+
33+
<pre><strong>输入:</strong>89
34+
<strong>输出:</strong>true
35+
<strong>解释:
36+
</strong>把 89 旋转 180° 以后得到 68,<code>86</code> 是有效数字且 86!=89 。
37+
</pre>
38+
39+
<p><strong>示例 3:</strong></p>
40+
41+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/26/1268_3.png" style="height: 121px; width: 301px;"></p>
42+
43+
<pre><strong>输入:</strong>11
44+
<strong>输出:</strong>false
45+
<strong>解释:
46+
</strong>把 11 旋转 180° 以后得到 11,11 是有效数字但是值保持不变,所以 11 不是易混淆数字。
47+
</pre>
48+
49+
<p><strong>示例 4:</strong></p>
50+
51+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/23/1268_4.png" style="height: 90px; width: 180px;"></p>
52+
53+
<pre><strong>输入:</strong>25
54+
<strong>输出:</strong>false
55+
<strong>解释:</strong>
56+
把 25 旋转 180° 以后得到的不是数字。
57+
</pre>
58+
59+
<p> </p>
60+
61+
<p><strong>提示:</strong></p>
62+
63+
<ol>
64+
<li><code>0 <= N <= 10^9</code></li>
65+
<li>可以忽略掉旋转后得到的前导零,例如,如果我们旋转后得到 <code>0008</code> 那么该数字就是 <code>8</code> 。</li>
66+
</ol>
67+
868

969

1070
## 解法

solution/1000-1099/1056.Confusing Number/README_EN.md

+58-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,64 @@
33
[中文文档](/solution/1000-1099/1056.Confusing%20Number/README.md)
44

55
## Description
6-
None
6+
<p>Given a number <code>N</code>, return <code>true</code> if and only if it is a <em>confusing number</em>, which satisfies the following condition:</p>
7+
8+
<p>We can rotate digits by 180 degrees to form new digits. When 0, 1, 6, 8, 9 are rotated 180 degrees, they become 0, 1, 9, 8, 6 respectively. When 2, 3, 4, 5 and 7 are rotated 180 degrees, they become invalid. A <em>confusing number</em> is a number that when rotated 180 degrees becomes a <strong>different</strong> number with each digit valid.</p>
9+
10+
<p> </p>
11+
12+
<p><strong>Example 1:</strong></p>
13+
14+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/23/1268_1.png" style="width: 180px; height: 90px;" /></p>
15+
16+
<pre>
17+
<strong>Input: </strong><span id="example-input-1-1">6</span>
18+
<strong>Output: </strong><span id="example-output-1">true</span>
19+
<strong>Explanation: </strong>
20+
We get <code>9</code> after rotating <code>6</code>, <code>9</code> is a valid number and <code>9!=6</code>.
21+
</pre>
22+
23+
<p><strong>Example 2:</strong></p>
24+
25+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/23/1268_2.png" style="width: 180px; height: 90px;" /></p>
26+
27+
<pre>
28+
<strong>Input: </strong><span id="example-input-2-1">89</span>
29+
<strong>Output: </strong><span id="example-output-2">true</span>
30+
<strong>Explanation: </strong>
31+
We get <code>68</code> after rotating <code>89</code>, <code>86</code> is a valid number and <code>86!=89</code>.
32+
</pre>
33+
34+
<p><strong>Example 3:</strong></p>
35+
36+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/26/1268_3.png" style="width: 301px; height: 121px;" /></p>
37+
38+
<pre>
39+
<strong>Input: </strong><span id="example-input-3-1">11</span>
40+
<strong>Output: </strong><span id="example-output-3">false</span>
41+
<strong>Explanation: </strong>
42+
We get <code>11</code> after rotating <code>11</code>, <code>11</code> is a valid number but the value remains the same, thus <code>11</code> is not a confusing number.
43+
</pre>
44+
45+
<p><strong>Example 4:</strong></p>
46+
47+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/23/1268_4.png" style="width: 180px; height: 90px;" /></p>
48+
49+
<pre>
50+
<strong>Input: </strong><span id="example-input-4-1">25</span>
51+
<strong>Output: </strong><span id="example-output-4">false</span>
52+
<strong>Explanation: </strong>
53+
We get an invalid number after rotating <code>25</code>.
54+
</pre>
55+
56+
<p> </p>
57+
58+
<p><strong>Note:</strong></p>
59+
60+
<ol>
61+
<li><code>0 <= N <= 10^9</code></li>
62+
<li>After the rotation we can ignore leading zeros, for example if after rotation we have <code>0008</code> then this number is considered as just <code>8</code>.</li>
63+
</ol>
764

865

966
## Solutions

solution/1000-1099/1057.Campus Bikes/README.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,46 @@
44

55
## 题目描述
66
<!-- 这里写题目描述 -->
7-
None
7+
<p>在由 2D 网格表示的校园里有 <code>n</code> 位工人(<code>worker</code>)和 <code>m</code> 辆自行车(<code>bike</code>),<code>n <= m</code>。所有工人和自行车的位置都用网格上的 2D 坐标表示。</p>
8+
9+
<p>我们需要为每位工人分配一辆自行车。在所有可用的自行车和工人中,我们选取彼此之间曼哈顿距离最短的工人自行车对  (worker, bike) ,并将其中的自行车分配給工人。如果有多个 (worker, bike) 对之间的曼哈顿距离相同,那么我们选择工人索引最小的那对。类似地,如果有多种不同的分配方法,则选择自行车索引最小的一对。不断重复这一过程,直到所有工人都分配到自行车为止。</p>
10+
11+
<p>给定两点 <code>p1</code> 和 <code>p2</code> 之间的曼哈顿距离为 <code>Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|</code>。</p>
12+
13+
<p>返回长度为 <code>n</code> 的向量 <code>ans</code>,其中 <code>a[i]</code> 是第 <code>i</code> 位工人分配到的自行车的索引(从 0 开始)。</p>
14+
15+
<p> </p>
16+
17+
<p><strong>示例 1:</strong></p>
18+
19+
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/03/16/1261_example_1_v2.png" style="height: 264px; width: 264px;"></p>
20+
21+
<pre><strong>输入:</strong>workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]]
22+
<strong>输出:</strong>[1,0]
23+
<strong>解释:</strong>
24+
工人 1 分配到自行车 0,因为他们最接近且不存在冲突,工人 0 分配到自行车 1 。所以输出是 [1,0]。
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/03/16/1261_example_2_v2.png" style="height: 264px; width: 264px;"></p>
30+
31+
<pre><strong>输入:</strong>workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]]
32+
<strong>输出:</strong>[0,2,1]
33+
<strong>解释:</strong>
34+
工人 0 首先分配到自行车 0 。工人 1 和工人 2 与自行车 2 距离相同,因此工人 1 分配到自行车 2,工人 2 将分配到自行车 1 。因此输出为 [0,2,1]。
35+
</pre>
36+
37+
<p> </p>
38+
39+
<p><strong>提示:</strong></p>
40+
41+
<ol>
42+
<li><code>0 <= workers[i][j], bikes[i][j] < 1000</code></li>
43+
<li>所有工人和自行车的位置都不相同。</li>
44+
<li><code>1 <= workers.length <= bikes.length <= 1000</code></li>
45+
</ol>
46+
847

948

1049
## 解法

solution/1000-1099/1057.Campus Bikes/README_EN.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,47 @@
33
[中文文档](/solution/1000-1099/1057.Campus%20Bikes/README.md)
44

55
## Description
6-
None
6+
<p>On a campus represented as a 2D grid, there are <code>N</code> workers and <code>M</code> bikes, with <code>N <= M</code>. Each worker and bike is a 2D coordinate on this grid.</p>
7+
8+
<p>Our goal is to assign a bike to each worker. Among the available bikes and workers, we choose the (worker, bike) pair with the shortest Manhattan distance between each other, and assign the bike to that worker. (If there are multiple (worker, bike) pairs with the same shortest Manhattan distance, we choose the pair with the smallest worker index; if there are multiple ways to do that, we choose the pair with the smallest bike index). We repeat this process until there are no available workers.</p>
9+
10+
<p>The Manhattan distance between two points <code>p1</code> and <code>p2</code> is <code>Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|</code>.</p>
11+
12+
<p>Return a vector <code>ans</code> of length <code>N</code>, where <code>ans[i]</code> is the index (0-indexed) of the bike that the <code>i</code>-th worker is assigned to.</p>
13+
14+
<p> </p>
15+
16+
<p><strong>Example 1:</strong></p>
17+
18+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/06/1261_example_1_v2.png" style="width: 264px; height: 264px;" /></p>
19+
20+
<pre>
21+
<strong>Input: </strong>workers = <span id="example-input-1-1">[[0,0],[2,1]]</span>, bikes = <span id="example-input-1-2">[[1,2],[3,3]]</span>
22+
<strong>Output: </strong><span id="example-output-1">[1,0]</span>
23+
<strong>Explanation: </strong>
24+
Worker 1 grabs Bike 0 as they are closest (without ties), and Worker 0 is assigned Bike 1. So the output is [1, 0].
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/03/06/1261_example_2_v2.png" style="width: 264px; height: 264px;" /></p>
30+
31+
<pre>
32+
<strong>Input: </strong>workers = <span id="example-input-2-1">[[0,0],[1,1],[2,0]]</span>, bikes = <span id="example-input-2-2">[[1,0],[2,2],[2,1]]</span>
33+
<strong>Output: </strong><span id="example-output-2">[0,2,1]</span>
34+
<strong>Explanation: </strong>
35+
Worker 0 grabs Bike 0 at first. Worker 1 and Worker 2 share the same distance to Bike 2, thus Worker 1 is assigned to Bike 2, and Worker 2 will take Bike 1. So the output is [0,2,1].
36+
</pre>
37+
38+
<p> </p>
39+
40+
<p><strong>Note:</strong></p>
41+
42+
<ol>
43+
<li><code>0 <= workers[i][j], bikes[i][j] < 1000</code></li>
44+
<li>All worker and bike locations are distinct.</li>
45+
<li><code>1 <= workers.length <= bikes.length <= 1000</code></li>
46+
</ol>
747

848

949
## Solutions

solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,37 @@
44

55
## 题目描述
66
<!-- 这里写题目描述 -->
7-
None
7+
<p>给定一系列价格 <code>[p<sub>1</sub>,p<sub>2</sub>...,p<sub>n</sub>]</code> 和一个目标 <code>target</code>,将每个价格 <code>p<sub>i</sub></code> 舍入为 <code>Round<sub>i</sub>(p<sub>i</sub>)</code> 以使得舍入数组 <code>[Round<sub>1</sub>(p<sub>1</sub>),Round<sub>2</sub>(p<sub>2</sub>)...,Round<sub>n</sub>(p<sub>n</sub>)]</code> 之和达到给定的目标值 <code>target</code>。每次舍入操作 <code>Round<sub>i</sub>(p<sub>i</sub>)</code> 可以是向下舍 <code>Floor(p<sub>i</sub>)</code> 也可以是向上入 <code>Ceil(p<sub>i</sub>)</code>。</p>
8+
9+
<p>如果舍入数组之和无论如何都无法达到目标值 <code>target</code>,就返回 <code>-1</code>。否则,以保留到小数点后三位的字符串格式返回最小的舍入误差,其定义为 Σ |Round<sub>i</sub>(p<sub>i</sub>) - (p<sub>i</sub>)|( i 从 1 到 n )。</p>
10+
11+
<p> </p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
15+
<pre><strong>输入:</strong>prices = ["0.700","2.800","4.900"], target = 8
16+
<strong>输出:</strong>"1.000"
17+
<strong>解释: </strong>
18+
使用 Floor,Ceil 和 Ceil 操作得到 (0.7 - 0) + (3 - 2.8) + (5 - 4.9) = 0.7 + 0.2 + 0.1 = 1.0 。
19+
</pre>
20+
21+
<p><strong>示例 2:</strong></p>
22+
23+
<pre><strong>输入:</strong>prices = ["1.500","2.500","3.500"], target = 10
24+
<strong>输出:</strong>"-1"
25+
<strong>解释:</strong>
26+
达到目标是不可能的。</pre>
27+
28+
<p> </p>
29+
30+
<p><strong>提示:</strong></p>
31+
32+
<ol>
33+
<li><code>1 <= prices.length <= 500</code></li>
34+
<li>表示价格的每个字符串 <code>prices[i]</code> 都代表一个介于 0 和 1000 之间的实数,并且正好有 3 个小数位。</li>
35+
<li><code>target</code> 介于 0 和 1000000 之间。</li>
36+
</ol>
37+
838

939

1040
## 解法

solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README_EN.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,39 @@
33
[中文文档](/solution/1000-1099/1058.Minimize%20Rounding%20Error%20to%20Meet%20Target/README.md)
44

55
## Description
6-
None
6+
<p>Given an array of prices <code>[p<sub>1</sub>,p<sub>2</sub>...,p<sub>n</sub>]</code> and a <code>target</code>, round each price <code>p<sub>i</sub></code> to <code>Round<sub>i</sub>(p<sub>i</sub>)</code> so that the rounded array <code>[Round<sub>1</sub>(p<sub>1</sub>),Round<sub>2</sub>(p<sub>2</sub>)...,Round<sub>n</sub>(p<sub>n</sub>)]</code> sums to the given <code>target</code>. Each operation <code>Round<sub>i</sub>(p<sub>i</sub>)</code> could be either <code>Floor(p<sub>i</sub>)</code> or <code>Ceil(p<sub>i</sub>)</code>.</p>
7+
8+
<p>Return the string <code>"-1"</code> if the rounded array is impossible to sum to <code>target</code>. Otherwise, return the smallest rounding error, which is defined as Σ |Round<sub>i</sub>(p<sub>i</sub>) - (p<sub>i</sub>)| for <italic>i</italic> from 1 to <italic>n</italic>, as a string with three places after the decimal.</p>
9+
10+
<p> </p>
11+
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input: </strong>prices = <span id="example-input-1-1">["0.700","2.800","4.900"]</span>, target = <span id="example-input-1-2">8</span>
16+
<strong>Output: </strong><span id="example-output-1">"1.000"</span>
17+
<strong>Explanation: </strong>
18+
Use Floor, Ceil and Ceil operations to get (0.7 - 0) + (3 - 2.8) + (5 - 4.9) = 0.7 + 0.2 + 0.1 = 1.0 .
19+
</pre>
20+
21+
<p><strong>Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input: </strong>prices = <span id="example-input-2-1">["1.500","2.500","3.500"]</span>, target = <span id="example-input-2-2">10</span>
25+
<strong>Output: </strong><span id="example-output-2">"-1"</span>
26+
<strong>Explanation: </strong>
27+
It is impossible to meet the target.
28+
</pre>
29+
30+
<p> </p>
31+
32+
<p><strong>Note:</strong></p>
33+
34+
<ol>
35+
<li><code>1 <= prices.length <= 500</code>.</li>
36+
<li>Each string of prices <code>prices[i]</code> represents a real number which is between 0 and 1000 and has exactly 3 decimal places.</li>
37+
<li><code>target</code> is between 0 and 1000000.</li>
38+
</ol>
739

840

941
## Solutions

0 commit comments

Comments
 (0)