Skip to content

Commit bd9c3cb

Browse files
committed
feat: add solutions to lc problems: No.1886,1887,1888,1889
1 parent 5d5e1a6 commit bd9c3cb

File tree

29 files changed

+1262
-4
lines changed

29 files changed

+1262
-4
lines changed

solution/0200-0299/0237.Delete Node in a Linked List/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,24 @@ func deleteNode(node *ListNode) {
128128
}
129129
```
130130

131+
### **C++**
132+
133+
```cpp
134+
/**
135+
* Definition for singly-linked list.
136+
* struct ListNode {
137+
* int val;
138+
* ListNode *next;
139+
* ListNode(int x) : val(x), next(NULL) {}
140+
* };
141+
*/
142+
class Solution {
143+
public:
144+
void deleteNode(ListNode* node) {
145+
node->val = node->next->val;
146+
node->next = node->next->next;
147+
}
148+
};
149+
```
150+
131151
<!-- tabs:end -->

solution/0200-0299/0237.Delete Node in a Linked List/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,24 @@ func deleteNode(node *ListNode) {
135135
}
136136
```
137137

138+
### **C++**
139+
140+
```cpp
141+
/**
142+
* Definition for singly-linked list.
143+
* struct ListNode {
144+
* int val;
145+
* ListNode *next;
146+
* ListNode(int x) : val(x), next(NULL) {}
147+
* };
148+
*/
149+
class Solution {
150+
public:
151+
void deleteNode(ListNode* node) {
152+
node->val = node->next->val;
153+
node->next = node->next->next;
154+
}
155+
};
156+
```
157+
138158
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
void deleteNode(struct ListNode* node) {
9+
node->val = node->next->val;
10+
node->next = node->next->next;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
void deleteNode(ListNode* node) {
12+
node->val = node->next->val;
13+
node->next = node->next->next;
14+
}
15+
};

solution/1800-1899/1884.Egg Drop With 2 Eggs and N Floors/README.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
1-
# [1884. ](https://leetcode-cn.com/problems/egg-drop-with-2-eggs-and-n-floors)
1+
# [1884. 鸡蛋掉落-两枚鸡蛋](https://leetcode-cn.com/problems/egg-drop-with-2-eggs-and-n-floors)
22

33
[English Version](/solution/1800-1899/1884.Egg%20Drop%20With%202%20Eggs%20and%20N%20Floors/README_EN.md)
44

55
## 题目描述
66

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

9-
None
9+
<p>给你 <strong>2 枚相同 </strong>的鸡蛋,和一栋从第 <code>1</code> 层到第 <code>n</code> 层共有 <code>n</code> 层楼的建筑。</p>
10+
11+
<p>已知存在楼层 <code>f</code> ,满足 <code>0 <= f <= n</code> ,任何从 <strong>高于 </strong><code>f</code> 的楼层落下的鸡蛋都<strong> 会碎 </strong>,从 <strong><code>f</code> 楼层或比它低 </strong>的楼层落下的鸡蛋都 <strong>不会碎 </strong>。</p>
12+
13+
<p>每次操作,你可以取一枚<strong> 没有碎</strong> 的鸡蛋并把它从任一楼层 <code>x</code> 扔下(满足 <code>1 <= x <= n</code>)。如果鸡蛋碎了,你就不能再次使用它。如果某枚鸡蛋扔下后没有摔碎,则可以在之后的操作中<strong> 重复使用 </strong>这枚鸡蛋。</p>
14+
15+
<p>请你计算并返回要确定 <code>f</code> <strong>确切的值 </strong>的 <strong>最小操作次数</strong> 是多少?</p>
16+
17+
<p> </p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>n = 2
23+
<strong>输出:</strong>2
24+
<strong>解释:</strong>我们可以将第一枚鸡蛋从 1 楼扔下,然后将第二枚从 2 楼扔下。
25+
如果第一枚鸡蛋碎了,可知 f = 0;
26+
如果第二没鸡蛋碎了,但第一枚没碎,可知 f = 1;
27+
否则,当两个鸡蛋都没碎时,可知 f = 2。
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
<pre>
33+
<strong>输入:</strong>n = 100
34+
<strong>输出:</strong>14
35+
</pre>
36+
37+
<p> </p>
38+
39+
<p><strong>提示:</strong></p>
40+
41+
<ul>
42+
<li><code>1 <= n <= 1000</code></li>
43+
</ul>
44+
1045

1146
## 解法
1247

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# [1886. 判断矩阵经轮转后是否一致](https://leetcode-cn.com/problems/determine-whether-matrix-can-be-obtained-by-rotation)
2+
3+
[English Version](/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你两个大小为 <code>n x n</code> 的二进制矩阵 <code>mat</code> 和 <code>target</code> 。现<strong> 以 90 度顺时针轮转 </strong>矩阵 <code>mat</code> 中的元素 <strong>若干次</strong> ,如果能够使 <code>mat</code> 与 <code>target</code> 一致,返回 <code>true</code> ;否则,返回<em> </em><code>false</code><em> 。</em></p>
10+
11+
<p> </p>
12+
13+
<p><strong>示例 1:</strong></p>
14+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/images/grid3.png" style="width: 301px; height: 121px;" />
15+
<pre>
16+
<strong>输入:</strong>mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
17+
<strong>输出:</strong>true
18+
<strong>解释:</strong>顺时针轮转 90 度一次可以使 mat 和 target 一致。
19+
</pre>
20+
21+
<p><strong>示例 2:</strong></p>
22+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/images/grid4.png" style="width: 301px; height: 121px;" />
23+
<pre>
24+
<strong>输入:</strong>mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
25+
<strong>输出:</strong>false
26+
<strong>解释:</strong>无法通过轮转矩阵中的元素使 equal 与 target 一致。
27+
</pre>
28+
29+
<p><strong>示例 3:</strong></p>
30+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/images/grid4-1.png" style="width: 661px; height: 184px;" />
31+
<pre>
32+
<strong>输入:</strong>mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]
33+
<strong>输出:</strong>true
34+
<strong>解释:</strong>顺时针轮转 90 度两次可以使 mat 和 target 一致。
35+
</pre>
36+
37+
<p> </p>
38+
39+
<p><strong>提示:</strong></p>
40+
41+
<ul>
42+
<li><code>n == mat.length == target.length</code></li>
43+
<li><code>n == mat[i].length == target[i].length</code></li>
44+
<li><code>1 <= n <= 10</code></li>
45+
<li><code>mat[i][j]</code> 和 <code>target[i][j]</code> 不是 <code>0</code> 就是 <code>1</code></li>
46+
</ul>
47+
48+
49+
## 解法
50+
51+
<!-- 这里可写通用的实现逻辑 -->
52+
53+
旋转矩阵,判断矩阵是否一致。
54+
55+
<!-- tabs:start -->
56+
57+
### **Python3**
58+
59+
<!-- 这里可写当前语言的特殊实现逻辑 -->
60+
61+
```python
62+
class Solution:
63+
def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:
64+
def rotate(matrix):
65+
n = len(matrix)
66+
for i in range(n // 2):
67+
for j in range(i, n - 1 - i):
68+
t = matrix[i][j]
69+
matrix[i][j] = matrix[n - j - 1][i]
70+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]
71+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]
72+
matrix[j][n - i - 1] = t
73+
for _ in range(4):
74+
if mat == target:
75+
return True
76+
rotate(mat)
77+
return False
78+
```
79+
80+
### **Java**
81+
82+
<!-- 这里可写当前语言的特殊实现逻辑 -->
83+
84+
```java
85+
class Solution {
86+
public boolean findRotation(int[][] mat, int[][] target) {
87+
int times = 4;
88+
while (times-- > 0) {
89+
if (equals(mat, target)) {
90+
return true;
91+
}
92+
rotate(mat);
93+
}
94+
return false;
95+
}
96+
97+
private void rotate(int[][] matrix) {
98+
int n = matrix.length;
99+
for (int i = 0; i < n / 2; ++i) {
100+
for (int j = i; j < n - 1 - i; ++j) {
101+
int t = matrix[i][j];
102+
matrix[i][j] = matrix[n - j - 1][i];
103+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
104+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
105+
matrix[j][n - i - 1] = t;
106+
}
107+
}
108+
}
109+
110+
private boolean equals(int[][] nums1, int[][] nums2) {
111+
int n = nums1.length;
112+
for (int i = 0; i < n; ++i) {
113+
for (int j = 0; j < n; ++j) {
114+
if (nums1[i][j] != nums2[i][j]) {
115+
return false;
116+
}
117+
}
118+
}
119+
return true;
120+
}
121+
}
122+
```
123+
124+
### **...**
125+
126+
```
127+
128+
```
129+
130+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# [1886. Determine Whether Matrix Can Be Obtained By Rotation](https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation)
2+
3+
[中文文档](/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/README.md)
4+
5+
## Description
6+
7+
<p>Given two <code>n x n</code> binary matrices <code>mat</code> and <code>target</code>, return <code>true</code><em> if it is possible to make </em><code>mat</code><em> equal to </em><code>target</code><em> by <strong>rotating</strong> </em><code>mat</code><em> in <strong>90-degree increments</strong>, or </em><code>false</code><em> otherwise.</em></p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong>Example 1:</strong></p>
11+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/images/grid3.png" style="width: 301px; height: 121px;" />
12+
<pre>
13+
<strong>Input:</strong> mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
14+
<strong>Output:</strong> true
15+
<strong>Explanation: </strong>We can rotate mat 90 degrees clockwise to make mat equal target.
16+
</pre>
17+
18+
<p><strong>Example 2:</strong></p>
19+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/images/grid4.png" style="width: 301px; height: 121px;" />
20+
<pre>
21+
<strong>Input:</strong> mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
22+
<strong>Output:</strong> false
23+
<strong>Explanation:</strong> It is impossible to make mat equal to target by rotating mat.
24+
</pre>
25+
26+
<p><strong>Example 3:</strong></p>
27+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1800-1899/1886.Determine%20Whether%20Matrix%20Can%20Be%20Obtained%20By%20Rotation/images/grid4-1.png" style="width: 661px; height: 184px;" />
28+
<pre>
29+
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]
30+
<strong>Output:</strong> true
31+
<strong>Explanation: </strong>We can rotate mat 90 degrees clockwise two times to make mat equal target.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>n == mat.length == target.length</code></li>
39+
<li><code>n == mat[i].length == target[i].length</code></li>
40+
<li><code>1 &lt;= n &lt;= 10</code></li>
41+
<li><code>mat[i][j]</code> and <code>target[i][j]</code> are either <code>0</code> or <code>1</code>.</li>
42+
</ul>
43+
44+
45+
## Solutions
46+
47+
<!-- tabs:start -->
48+
49+
### **Python3**
50+
51+
```python
52+
class Solution:
53+
def findRotation(self, mat: List[List[int]], target: List[List[int]]) -> bool:
54+
def rotate(matrix):
55+
n = len(matrix)
56+
for i in range(n // 2):
57+
for j in range(i, n - 1 - i):
58+
t = matrix[i][j]
59+
matrix[i][j] = matrix[n - j - 1][i]
60+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]
61+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]
62+
matrix[j][n - i - 1] = t
63+
for _ in range(4):
64+
if mat == target:
65+
return True
66+
rotate(mat)
67+
return False
68+
```
69+
70+
### **Java**
71+
72+
```java
73+
class Solution {
74+
public boolean findRotation(int[][] mat, int[][] target) {
75+
int times = 4;
76+
while (times-- > 0) {
77+
if (equals(mat, target)) {
78+
return true;
79+
}
80+
rotate(mat);
81+
}
82+
return false;
83+
}
84+
85+
private void rotate(int[][] matrix) {
86+
int n = matrix.length;
87+
for (int i = 0; i < n / 2; ++i) {
88+
for (int j = i; j < n - 1 - i; ++j) {
89+
int t = matrix[i][j];
90+
matrix[i][j] = matrix[n - j - 1][i];
91+
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
92+
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
93+
matrix[j][n - i - 1] = t;
94+
}
95+
}
96+
}
97+
98+
private boolean equals(int[][] nums1, int[][] nums2) {
99+
int n = nums1.length;
100+
for (int i = 0; i < n; ++i) {
101+
for (int j = 0; j < n; ++j) {
102+
if (nums1[i][j] != nums2[i][j]) {
103+
return false;
104+
}
105+
}
106+
}
107+
return true;
108+
}
109+
}
110+
```
111+
112+
### **...**
113+
114+
```
115+
116+
```
117+
118+
<!-- tabs:end -->

0 commit comments

Comments
 (0)