Skip to content

Commit 0863ccc

Browse files
committed
feat: add solutions to lc problem: No.1992
No.1992.Find All Groups of Farmland/
1 parent f9facaf commit 0863ccc

File tree

6 files changed

+248
-2
lines changed

6 files changed

+248
-2
lines changed

solution/1900-1999/1992.Find All Groups of Farmland/README.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,112 @@
6161

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

64+
判断是否为矩形左上角,需要满足三个条件:
65+
66+
- 元素值为 1;
67+
- 左边是边界或者是 0;
68+
- 上边是边界或者是 0。
69+
70+
然后遍历找到矩形的右边界和下边界。
71+
6472
<!-- tabs:start -->
6573

6674
### **Python3**
6775

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

7078
```python
71-
79+
class Solution:
80+
def findFarmland(self, land: List[List[int]]) -> List[List[int]]:
81+
m, n = len(land), len(land[0])
82+
ans = []
83+
for i in range(m):
84+
for j in range(n):
85+
if land[i][j] == 0 or (j > 0 and land[i][j-1] == 1) or (i > 0 and land[i-1][j] == 1):
86+
continue
87+
x, y = i, j
88+
while x + 1 < m and land[x + 1][j] == 1:
89+
x += 1
90+
while y + 1 < n and land[x][y + 1] == 1:
91+
y += 1
92+
ans.append([i, j, x, y])
93+
return ans
7294
```
7395

7496
### **Java**
7597

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

78100
```java
101+
class Solution {
102+
public int[][] findFarmland(int[][] land) {
103+
List<int[]> ans = new ArrayList<>();
104+
int m = land.length;
105+
int n = land[0].length;
106+
for (int i = 0; i < m; ++i) {
107+
for (int j = 0; j < n; ++j) {
108+
if (land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1)) {
109+
continue;
110+
}
111+
int x = i;
112+
int y = j;
113+
for (; x + 1 < m && land[x + 1][j] == 1; ++x);
114+
for (; y + 1 < n && land[x][y + 1] == 1; ++y);
115+
ans.add(new int[]{i, j, x, y});
116+
}
117+
}
118+
return ans.toArray(new int[ans.size()][4]);
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
vector<vector<int>> findFarmland(vector<vector<int>>& land) {
129+
vector<vector<int>> ans;
130+
int m = land.size();
131+
int n = land[0].size();
132+
for (int i = 0; i < m; ++i)
133+
{
134+
for (int j = 0; j < n; ++j)
135+
{
136+
if (land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1)) continue;
137+
int x = i;
138+
int y = j;
139+
for (; x + 1 < m && land[x + 1][j] == 1; ++x);
140+
for (; y + 1 < n && land[x][y + 1] == 1; ++y);
141+
ans.push_back({i, j, x, y});
142+
}
143+
}
144+
return ans;
145+
}
146+
};
147+
```
79148
149+
### **Go**
150+
151+
```go
152+
func findFarmland(land [][]int) [][]int {
153+
m, n := len(land), len(land[0])
154+
var ans [][]int
155+
for i := 0; i < m; i++ {
156+
for j := 0; j < n; j++ {
157+
if land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1) {
158+
continue
159+
}
160+
x, y := i, j
161+
for ; x+1 < m && land[x+1][j] == 1; x++ {
162+
}
163+
for ; y+1 < n && land[x][y+1] == 1; y++ {
164+
}
165+
ans = append(ans, []int{i, j, x, y})
166+
}
167+
}
168+
return ans
169+
}
80170
```
81171

82172
### **...**

solution/1900-1999/1992.Find All Groups of Farmland/README_EN.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,95 @@ There are no groups of farmland.
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def findFarmland(self, land: List[List[int]]) -> List[List[int]]:
64+
m, n = len(land), len(land[0])
65+
ans = []
66+
for i in range(m):
67+
for j in range(n):
68+
if land[i][j] == 0 or (j > 0 and land[i][j-1] == 1) or (i > 0 and land[i-1][j] == 1):
69+
continue
70+
x, y = i, j
71+
while x + 1 < m and land[x + 1][j] == 1:
72+
x += 1
73+
while y + 1 < n and land[x][y + 1] == 1:
74+
y += 1
75+
ans.append([i, j, x, y])
76+
return ans
6377
```
6478

6579
### **Java**
6680

6781
```java
82+
class Solution {
83+
public int[][] findFarmland(int[][] land) {
84+
List<int[]> ans = new ArrayList<>();
85+
int m = land.length;
86+
int n = land[0].length;
87+
for (int i = 0; i < m; ++i) {
88+
for (int j = 0; j < n; ++j) {
89+
if (land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1)) {
90+
continue;
91+
}
92+
int x = i;
93+
int y = j;
94+
for (; x + 1 < m && land[x + 1][j] == 1; ++x);
95+
for (; y + 1 < n && land[x][y + 1] == 1; ++y);
96+
ans.add(new int[]{i, j, x, y});
97+
}
98+
}
99+
return ans.toArray(new int[ans.size()][4]);
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
vector<vector<int>> findFarmland(vector<vector<int>>& land) {
110+
vector<vector<int>> ans;
111+
int m = land.size();
112+
int n = land[0].size();
113+
for (int i = 0; i < m; ++i)
114+
{
115+
for (int j = 0; j < n; ++j)
116+
{
117+
if (land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1)) continue;
118+
int x = i;
119+
int y = j;
120+
for (; x + 1 < m && land[x + 1][j] == 1; ++x);
121+
for (; y + 1 < n && land[x][y + 1] == 1; ++y);
122+
ans.push_back({i, j, x, y});
123+
}
124+
}
125+
return ans;
126+
}
127+
};
128+
```
68129
130+
### **Go**
131+
132+
```go
133+
func findFarmland(land [][]int) [][]int {
134+
m, n := len(land), len(land[0])
135+
var ans [][]int
136+
for i := 0; i < m; i++ {
137+
for j := 0; j < n; j++ {
138+
if land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1) {
139+
continue
140+
}
141+
x, y := i, j
142+
for ; x+1 < m && land[x+1][j] == 1; x++ {
143+
}
144+
for ; y+1 < n && land[x][y+1] == 1; y++ {
145+
}
146+
ans = append(ans, []int{i, j, x, y})
147+
}
148+
}
149+
return ans
150+
}
69151
```
70152

71153
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> findFarmland(vector<vector<int>>& land) {
4+
vector<vector<int>> ans;
5+
int m = land.size();
6+
int n = land[0].size();
7+
for (int i = 0; i < m; ++i)
8+
{
9+
for (int j = 0; j < n; ++j)
10+
{
11+
if (land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1)) continue;
12+
int x = i;
13+
int y = j;
14+
for (; x + 1 < m && land[x + 1][j] == 1; ++x);
15+
for (; y + 1 < n && land[x][y + 1] == 1; ++y);
16+
ans.push_back({i, j, x, y});
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func findFarmland(land [][]int) [][]int {
2+
m, n := len(land), len(land[0])
3+
var ans [][]int
4+
for i := 0; i < m; i++ {
5+
for j := 0; j < n; j++ {
6+
if land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1) {
7+
continue
8+
}
9+
x, y := i, j
10+
for ; x+1 < m && land[x+1][j] == 1; x++ {
11+
}
12+
for ; y+1 < n && land[x][y+1] == 1; y++ {
13+
}
14+
ans = append(ans, []int{i, j, x, y})
15+
}
16+
}
17+
return ans
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[][] findFarmland(int[][] land) {
3+
List<int[]> ans = new ArrayList<>();
4+
int m = land.length;
5+
int n = land[0].length;
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
if (land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1)) {
9+
continue;
10+
}
11+
int x = i;
12+
int y = j;
13+
for (; x + 1 < m && land[x + 1][j] == 1; ++x);
14+
for (; y + 1 < n && land[x][y + 1] == 1; ++y);
15+
ans.add(new int[]{i, j, x, y});
16+
}
17+
}
18+
return ans.toArray(new int[ans.size()][4]);
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def findFarmland(self, land: List[List[int]]) -> List[List[int]]:
3+
m, n = len(land), len(land[0])
4+
ans = []
5+
for i in range(m):
6+
for j in range(n):
7+
if land[i][j] == 0 or (j > 0 and land[i][j-1] == 1) or (i > 0 and land[i-1][j] == 1):
8+
continue
9+
x, y = i, j
10+
while x + 1 < m and land[x + 1][j] == 1:
11+
x += 1
12+
while y + 1 < n and land[x][y + 1] == 1:
13+
y += 1
14+
ans.append([i, j, x, y])
15+
return ans

0 commit comments

Comments
 (0)