Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0fbb6e

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

File tree

38 files changed

+911
-135
lines changed

38 files changed

+911
-135
lines changed

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

Lines changed: 107 additions & 1 deletion
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">
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">
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
### **...**
Lines changed: 23 additions & 0 deletions
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‎

Lines changed: 19 additions & 0 deletions
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+
}
Lines changed: 22 additions & 0 deletions
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‎

Lines changed: 17 additions & 0 deletions
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‎

Lines changed: 8 additions & 7 deletions
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><strongclass="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><strongclass="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‎

Lines changed: 3 additions & 3 deletions
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><strongclass="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><strongclass="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><strongclass="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‎

Lines changed: 41 additions & 4 deletions
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
<!-- 这里可写通用的实现逻辑 -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /