Skip to content

Commit ffe34f1

Browse files
committed
feat: add solutions to lc/lcp problems
1 parent 79a7e1b commit ffe34f1

File tree

38 files changed

+928
-135
lines changed

38 files changed

+928
-135
lines changed

lcp/LCP 70. 沙地治理/README.md

+107-1
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,128 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:找规律**
48+
49+
我们画图观察,可以发现,第一行只有一个三角形,一定要涂色,而从最后一行开始,到第二行结束,每四行的涂色方案是一样的:
50+
51+
1. 最后一行涂色坐标为 $(n, 1)$, $(n, 3)$, ..., $(n, 2n - 1)$。
52+
1. 第 $n - 1$ 行涂色坐标为 $(n - 1, 2)$。
53+
1. 第 $n - 2$ 行涂色坐标为 $(n - 2, 3)$, $(n - 2, 5)$, ..., $(n - 2, 2n - 5)$。
54+
1. 第 $n - 3$ 行涂色坐标为 $(n - 3, 1)$。
55+
56+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2070.%20%E6%B2%99%E5%9C%B0%E6%B2%BB%E7%90%86/images/demo.png" style="width: 50%">
57+
58+
因此,我们可以按照上述规律,先给第一行涂色,然后从最后一行开始,每四行涂色一次,直到第二行结束。
59+
60+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2070.%20%E6%B2%99%E5%9C%B0%E6%B2%BB%E7%90%86/images/demo2.png" style="width: 80%">
61+
62+
时间复杂度 $(n^2)$,其中 $n$ 为题目给定的参数。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
63+
4764
<!-- tabs:start -->
4865

4966
### **Python3**
5067

5168
<!-- 这里可写当前语言的特殊实现逻辑 -->
5269

5370
```python
54-
71+
class Solution:
72+
def sandyLandManagement(self, size: int) -> List[List[int]]:
73+
ans = [[1, 1]]
74+
k = 0
75+
for i in range(size, 1, -1):
76+
if k == 0:
77+
for j in range(1, i << 1, 2):
78+
ans.append([i, j])
79+
elif k == 1:
80+
ans.append([i, 2])
81+
elif k == 2:
82+
for j in range(3, i << 1, 2):
83+
ans.append([i, j])
84+
else:
85+
ans.append([i, 1])
86+
k = (k + 1) % 4
87+
return ans
5588
```
5689

5790
### **Java**
5891

5992
<!-- 这里可写当前语言的特殊实现逻辑 -->
6093

6194
```java
95+
class Solution {
96+
public int[][] sandyLandManagement(int size) {
97+
List<int[]> ans = new ArrayList<>();
98+
ans.add(new int[] {1, 1});
99+
for (int i = size, k = 0; i > 1; --i, k = (k + 1) % 4) {
100+
if (k == 0) {
101+
for (int j = 1; j < i << 1; j += 2) {
102+
ans.add(new int[] {i, j});
103+
}
104+
} else if (k == 1) {
105+
ans.add(new int[] {i, 2});
106+
} else if (k == 2) {
107+
for (int j = 3; j < i << 1; j += 2) {
108+
ans.add(new int[] {i, j});
109+
}
110+
} else {
111+
ans.add(new int[] {i, 1});
112+
}
113+
}
114+
return ans.toArray(new int[0][]);
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
vector<vector<int>> sandyLandManagement(int size) {
125+
vector<vector<int>> ans;
126+
ans.push_back({1, 1});
127+
for (int i = size, k = 0; i > 1; --i, k = (k + 1) % 4) {
128+
if (k == 0) {
129+
for (int j = 1; j < i << 1; j += 2) {
130+
ans.push_back({i, j});
131+
}
132+
} else if (k == 1) {
133+
ans.push_back({i, 2});
134+
} else if (k == 2) {
135+
for (int j = 3; j < i << 1; j += 2) {
136+
ans.push_back({i, j});
137+
}
138+
} else {
139+
ans.push_back({i, 1});
140+
}
141+
}
142+
return ans;
143+
}
144+
};
145+
```
62146
147+
### **Go**
148+
149+
```go
150+
func sandyLandManagement(size int) (ans [][]int) {
151+
ans = append(ans, []int{1, 1})
152+
for i, k := size, 0; i > 1; i, k = i-1, (k+1)%4 {
153+
if k == 0 {
154+
for j := 1; j < i<<1; j += 2 {
155+
ans = append(ans, []int{i, j})
156+
}
157+
} else if k == 1 {
158+
ans = append(ans, []int{i, 2})
159+
} else if k == 2 {
160+
for j := 3; j < i<<1; j += 2 {
161+
ans = append(ans, []int{i, j})
162+
}
163+
} else {
164+
ans = append(ans, []int{i, 1})
165+
}
166+
}
167+
return
168+
}
63169
```
64170

65171
### **...**

lcp/LCP 70. 沙地治理/Solution.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> sandyLandManagement(int size) {
4+
vector<vector<int>> ans;
5+
ans.push_back({1, 1});
6+
for (int i = size, k = 0; i > 1; --i, k = (k + 1) % 4) {
7+
if (k == 0) {
8+
for (int j = 1; j < i << 1; j += 2) {
9+
ans.push_back({i, j});
10+
}
11+
} else if (k == 1) {
12+
ans.push_back({i, 2});
13+
} else if (k == 2) {
14+
for (int j = 3; j < i << 1; j += 2) {
15+
ans.push_back({i, j});
16+
}
17+
} else {
18+
ans.push_back({i, 1});
19+
}
20+
}
21+
return ans;
22+
}
23+
};

lcp/LCP 70. 沙地治理/Solution.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func sandyLandManagement(size int) (ans [][]int) {
2+
ans = append(ans, []int{1, 1})
3+
for i, k := size, 0; i > 1; i, k = i-1, (k+1)%4 {
4+
if k == 0 {
5+
for j := 1; j < i<<1; j += 2 {
6+
ans = append(ans, []int{i, j})
7+
}
8+
} else if k == 1 {
9+
ans = append(ans, []int{i, 2})
10+
} else if k == 2 {
11+
for j := 3; j < i<<1; j += 2 {
12+
ans = append(ans, []int{i, j})
13+
}
14+
} else {
15+
ans = append(ans, []int{i, 1})
16+
}
17+
}
18+
return
19+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int[][] sandyLandManagement(int size) {
3+
List<int[]> ans = new ArrayList<>();
4+
ans.add(new int[] {1, 1});
5+
for (int i = size, k = 0; i > 1; --i, k = (k + 1) % 4) {
6+
if (k == 0) {
7+
for (int j = 1; j < i << 1; j += 2) {
8+
ans.add(new int[] {i, j});
9+
}
10+
} else if (k == 1) {
11+
ans.add(new int[] {i, 2});
12+
} else if (k == 2) {
13+
for (int j = 3; j < i << 1; j += 2) {
14+
ans.add(new int[] {i, j});
15+
}
16+
} else {
17+
ans.add(new int[] {i, 1});
18+
}
19+
}
20+
return ans.toArray(new int[0][]);
21+
}
22+
}

lcp/LCP 70. 沙地治理/Solution.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def sandyLandManagement(self, size: int) -> List[List[int]]:
3+
ans = [[1, 1]]
4+
k = 0
5+
for i in range(size, 1, -1):
6+
if k == 0:
7+
for j in range(1, i << 1, 2):
8+
ans.append([i, j])
9+
elif k == 1:
10+
ans.append([i, 2])
11+
elif k == 2:
12+
for j in range(3, i << 1, 2):
13+
ans.append([i, j])
14+
else:
15+
ans.append([i, 1])
16+
k = (k + 1) % 4
17+
return ans
30.5 KB
Loading
57.5 KB
Loading

solution/0000-0099/0026.Remove Duplicates from Sorted Array/README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给你一个 <strong>升序排列</strong> 的数组 <code>nums</code> ,请你<strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank"> 原地</a></strong> 删除重复出现的元素,使每个元素 <strong>只出现一次</strong> ,返回删除后数组的新长度。元素的 <strong>相对顺序</strong> 应该保持 <strong>一致</strong> 。</p>
9+
<p>给你一个 <strong>升序排列</strong> 的数组 <code>nums</code> ,请你<strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank"> 原地</a></strong> 删除重复出现的元素,使每个元素 <strong>只出现一次</strong> ,返回删除后数组的新长度。元素的 <strong>相对顺序</strong> 应该保持 <strong>一致</strong> 。然后返回 <code>nums</code> 中唯一元素的个数。</p>
1010

11-
<p>由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 <code>k</code> 个元素,那么&nbsp;<code>nums</code>&nbsp;的前 <code>k</code> 个元素应该保存最终结果。</p>
11+
<p>考虑 <code>nums</code> 的唯一元素的数量为 <code>k</code> ,你需要做以下事情确保你的题解可以被通过:</p>
1212

13-
<p>将最终结果插入&nbsp;<code>nums</code> 的前 <code>k</code> 个位置后返回 <code>k</code> 。</p>
14-
15-
<p>不要使用额外的空间,你必须在 <strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地 </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>
13+
<ul>
14+
<li>更改数组 <code>nums</code> ,使 <code>nums</code> 的前 <code>k</code> 个元素包含唯一元素,并按照它们最初在 <code>nums</code> 中出现的顺序排列。<code>nums</code>&nbsp;的其余元素与 <code>nums</code> 的大小不重要。</li>
15+
<li>返回 <code>k</code>&nbsp;。</li>
16+
</ul>
1617

1718
<p><strong>判题标准:</strong></p>
1819

@@ -33,15 +34,15 @@ for (int i = 0; i &lt; k; i++) {
3334

3435
<p>&nbsp;</p>
3536

36-
<p><strong>示例 1:</strong></p>
37+
<p><strong class="example">示例 1:</strong></p>
3738

3839
<pre>
3940
<strong>输入:</strong>nums = [1,1,2]
4041
<strong>输出:</strong>2, nums = [1,2,_]
4142
<strong>解释:</strong>函数应该返回新的长度 <strong><code>2</code></strong> ,并且原数组 <em>nums </em>的前两个元素被修改为 <strong><code>1</code></strong>, <strong><code>2 </code></strong><code>。</code>不需要考虑数组中超出新长度后面的元素。
4243
</pre>
4344

44-
<p><strong>示例 2:</strong></p>
45+
<p><strong class="example">示例 2:</strong></p>
4546

4647
<pre>
4748
<strong>输入:</strong>nums = [0,0,1,1,1,2,2,3,3,4]

solution/0100-0199/0140.Word Break II/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212

1313
<p>&nbsp;</p>
1414

15-
<p><strong>示例 1:</strong></p>
15+
<p><strong class="example">示例 1:</strong></p>
1616

1717
<pre>
1818
<strong>输入:</strong>s = "<code>catsanddog</code>", wordDict = <code>["cat","cats","and","sand","dog"]</code>
1919
<strong>输出:</strong><code>["cats and dog","cat sand dog"]</code>
2020
</pre>
2121

22-
<p><strong>示例 2:</strong></p>
22+
<p><strong class="example">示例 2:</strong></p>
2323

2424
<pre>
2525
<strong>输入:</strong>s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
2626
<strong>输出:</strong>["pine apple pen apple","pineapple pen apple","pine applepen apple"]
2727
<strong>解释:</strong> 注意你可以重复使用字典中的单词。
2828
</pre>
2929

30-
<p><strong>示例&nbsp;3:</strong></p>
30+
<p><strong class="example">示例&nbsp;3:</strong></p>
3131

3232
<pre>
3333
<strong>输入:</strong>s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]

solution/0100-0199/0163.Missing Ranges/README.md

+41-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,51 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定一个排序的整数数组 <em><strong>nums&nbsp;</strong></em>,其中元素的范围在&nbsp;<strong>闭区间</strong>&nbsp;<strong>[<em>lower, upper</em>]</strong>&nbsp;当中,返回不包含在数组中的缺失区间。</p>
9+
<p>给定一个 <strong>排序</strong> 的整数数组 <code>nums</code><em><strong>&nbsp;</strong></em>,其中元素的范围在闭区间&nbsp;<code>[lower, upper]</code>&nbsp;当中。</p>
1010

11-
<p><strong>示例:</strong></p>
11+
<p>如果一个数字 <code>x</code> 在 <code>[lower, upper]</code>&nbsp;区间内,并且 <code>x</code> 不在 <code>nums</code> 中,则认为 <code>x</code> <strong>缺失</strong></p>
1212

13-
<pre><strong>输入: </strong><strong><em>nums</em></strong> = <code>[0, 1, 3, 50, 75]</code>, <strong><em>lower</em></strong> = 0 和 <strong><em>upper</em></strong> = 99,
14-
<strong>输出: </strong><code>[&quot;2&quot;, &quot;4-&gt;49&quot;, &quot;51-&gt;74&quot;, &quot;76-&gt;99&quot;]</code>
13+
<p>返回&nbsp;<strong>准确涵盖所有缺失数字&nbsp;</strong>的 <strong>最小排序</strong> 区间列表。也就是说,<code>nums</code> 的任何元素都不在任何区间内,并且每个缺失的数字都在其中一个区间内。</p>
14+
15+
<p>列表中的每个区间 <code>[a,b]</code> 应该输出:</p>
16+
17+
<ul>
18+
<li>如果&nbsp;&nbsp;<code>a != b</code>&nbsp;输出&nbsp;<code>"a-&gt;b"</code></li>
19+
<li>如果&nbsp;<code>a == b</code>&nbsp;输出 <code>"a"</code></li>
20+
</ul>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong class="example">示例 1:</strong></p>
25+
26+
<pre>
27+
<strong>输入: </strong>nums = <code>[0, 1, 3, 50, 75]</code>, lower = 0 , upper = 99
28+
<strong>输出: </strong><code>["2", "4-&gt;49", "51-&gt;74", "76-&gt;99"]</code>
29+
<strong>解释:</strong>返回的区间是:
30+
[2,2] --&gt; "2"
31+
[4,49] --&gt; "4-&gt;49"
32+
[51,74] --&gt; "51-&gt;74"
33+
[76,99] --&gt; "76-&gt;99"
1534
</pre>
1635

36+
<p><strong class="example">示例 2:</strong></p>
37+
38+
<pre>
39+
<strong>输入:</strong> nums = [-1], lower = -1, upper = -1
40+
<strong>输出:</strong> []
41+
<b>解释:</b>&nbsp;没有缺失的区间,因为没有缺失的数字。</pre>
42+
43+
<p>&nbsp;</p>
44+
45+
<p><strong>提示:</strong></p>
46+
47+
<ul>
48+
<li><code>-10<sup>9</sup>&nbsp;&lt;= lower &lt;= upper &lt;= 10<sup>9</sup></code></li>
49+
<li><code>0 &lt;= nums.length &lt;= 100</code></li>
50+
<li><code>lower &lt;= nums[i] &lt;= upper</code></li>
51+
<li><code>nums 的所有值都是唯一的。</code></li>
52+
</ul>
53+
1754
## 解法
1855

1956
<!-- 这里可写通用的实现逻辑 -->

solution/0100-0199/0169.Majority Element/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
## Solutions
3232

33-
**Approach 1: Moore Voting Algorithm**
33+
**Approach 1: Moore Voting Algorithm**
3434

3535
The basic steps of the Moore voting algorithm are as follows:
3636

solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p>给定一个整数数组 <code>prices</code> ,它的第<em> </em><code>i</code> 个元素 <code>prices[i]</code> 是一支给定的股票在第 <code>i</code><em> </em>天的价格。</p>
9+
<p>给定一个整数数组&nbsp;<code>prices</code> ,它的第<em> </em><code>i</code> 个元素&nbsp;<code>prices[i]</code> 是一支给定的股票在第 <code>i</code><em> </em>天的价格,和一个整型 <code>k</code> 。</p>
1010

11-
<p>设计一个算法来计算你所能获取的最大利润。你最多可以完成 <strong>k</strong> 笔交易。</p>
11+
<p>设计一个算法来计算你所能获取的最大利润。你最多可以完成 <code>k</code> 笔交易。也就是说,你最多可以买 <code>k</code> 次,卖 <code>k</code> 次。</p>
1212

1313
<p><strong>注意:</strong>你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。</p>
1414

15-
<p> </p>
15+
<p>&nbsp;</p>
1616

17-
<p><strong>示例 1:</strong></p>
17+
<p><strong class="example">示例 1:</strong></p>
1818

1919
<pre>
2020
<strong>输入:</strong>k = 2, prices = [2,4,1]
2121
<strong>输出:</strong>2
2222
<strong>解释:</strong>在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。</pre>
2323

24-
<p><strong>示例 2:</strong></p>
24+
<p><strong class="example">示例 2:</strong></p>
2525

2626
<pre>
2727
<strong>输入:</strong>k = 2, prices = [3,2,6,5,0,3]
2828
<strong>输出:</strong>7
2929
<strong>解释:</strong>在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
3030
随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。</pre>
3131

32-
<p> </p>
32+
<p>&nbsp;</p>
3333

3434
<p><strong>提示:</strong></p>
3535

3636
<ul>
37-
<li><code>0 <= k <= 100</code></li>
38-
<li><code>0 <= prices.length <= 1000</code></li>
39-
<li><code>0 <= prices[i] <= 1000</code></li>
37+
<li><code>0 &lt;= k &lt;= 100</code></li>
38+
<li><code>0 &lt;= prices.length &lt;= 1000</code></li>
39+
<li><code>0 &lt;= prices[i] &lt;= 1000</code></li>
4040
</ul>
4141

4242
## 解法

0 commit comments

Comments
 (0)