Skip to content

Commit ef38dc8

Browse files
committed
feat: add solutions to lc problem: No.1267.Count Servers That
Communicate
1 parent 87166a2 commit ef38dc8

File tree

6 files changed

+327
-4
lines changed

6 files changed

+327
-4
lines changed

solution/1200-1299/1267.Count Servers that Communicate/README.md

+116-2
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,141 @@
5252
<li><code>grid[i][j] == 0 or 1</code></li>
5353
</ul>
5454

55-
5655
## 解法
5756

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

59+
对每一行、每一列的服务器数量进行统计。
60+
61+
然后遍历每个服务器,若当前服务器所在的行或者列的服务器数量超过 1,说明当前服务器满足条件,累加结果 res。
62+
63+
遍历结束后,返回 res 即可。
64+
6065
<!-- tabs:start -->
6166

6267
### **Python3**
6368

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

6671
```python
67-
72+
class Solution:
73+
def countServers(self, grid: List[List[int]]) -> int:
74+
m, n = len(grid), len(grid[0])
75+
rows = [0] * m
76+
cols = [0] * n
77+
for i in range(m):
78+
for j in range(n):
79+
if grid[i][j] == 1:
80+
rows[i] += 1
81+
cols[j] += 1
82+
res = 0
83+
for i in range(m):
84+
for j in range(n):
85+
if grid[i][j] == 1:
86+
if rows[i] > 1 or cols[j] > 1:
87+
res += 1
88+
return res
6889
```
6990

7091
### **Java**
7192

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

7495
```java
96+
class Solution {
97+
public int countServers(int[][] grid) {
98+
int m = grid.length, n = grid[0].length;
99+
int[] rows = new int[m];
100+
int[] cols = new int[n];
101+
for (int i = 0; i < m; ++i) {
102+
for (int j = 0; j < n; ++j) {
103+
if (grid[i][j] == 1) {
104+
++rows[i];
105+
++cols[j];
106+
}
107+
}
108+
}
109+
int res = 0;
110+
for (int i = 0; i < m; ++i) {
111+
for (int j = 0; j < n; ++j) {
112+
if (grid[i][j] == 1) {
113+
if (rows[i] > 1 || cols[j] > 1) {
114+
++res;
115+
}
116+
}
117+
}
118+
}
119+
return res;
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
int countServers(vector<vector<int>>& grid) {
130+
int m = grid.size(), n = grid[0].size();
131+
vector<int> rows(m);
132+
vector<int> cols(n);
133+
for (int i = 0; i < m; ++i)
134+
{
135+
for (int j = 0; j < n; ++j)
136+
{
137+
if (grid[i][j] == 1)
138+
{
139+
++rows[i];
140+
++cols[j];
141+
}
142+
}
143+
}
144+
int res = 0;
145+
for (int i = 0; i < m; ++i)
146+
{
147+
for (int j = 0; j < n; ++j)
148+
{
149+
if (grid[i][j] == 1)
150+
{
151+
if (rows[i] > 1 || cols[j] > 1)
152+
{
153+
++res;
154+
}
155+
}
156+
}
157+
}
158+
return res;
159+
}
160+
};
161+
```
75162
163+
### **Go**
164+
165+
```go
166+
func countServers(grid [][]int) int {
167+
m, n := len(grid), len(grid[0])
168+
rows := make([]int, m)
169+
cols := make([]int, n)
170+
for i := 0; i < m; i++ {
171+
for j := 0; j < n; j++ {
172+
if grid[i][j] == 1 {
173+
rows[i]++
174+
cols[j]++
175+
}
176+
}
177+
}
178+
res := 0
179+
for i := 0; i < m; i++ {
180+
for j := 0; j < n; j++ {
181+
if grid[i][j] == 1 {
182+
if rows[i] > 1 || cols[j] > 1 {
183+
res++
184+
}
185+
}
186+
}
187+
}
188+
return res
189+
}
76190
```
77191

78192
### **...**

solution/1200-1299/1267.Count Servers that Communicate/README_EN.md

+110-2
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,129 @@ Return the number of servers&nbsp;that communicate with any other server.</p>
4949
<li><code>grid[i][j] == 0 or 1</code></li>
5050
</ul>
5151

52-
5352
## Solutions
5453

5554
<!-- tabs:start -->
5655

5756
### **Python3**
5857

5958
```python
60-
59+
class Solution:
60+
def countServers(self, grid: List[List[int]]) -> int:
61+
m, n = len(grid), len(grid[0])
62+
rows = [0] * m
63+
cols = [0] * n
64+
for i in range(m):
65+
for j in range(n):
66+
if grid[i][j] == 1:
67+
rows[i] += 1
68+
cols[j] += 1
69+
res = 0
70+
for i in range(m):
71+
for j in range(n):
72+
if grid[i][j] == 1:
73+
if rows[i] > 1 or cols[j] > 1:
74+
res += 1
75+
return res
6176
```
6277

6378
### **Java**
6479

6580
```java
81+
class Solution {
82+
public int countServers(int[][] grid) {
83+
int m = grid.length, n = grid[0].length;
84+
int[] rows = new int[m];
85+
int[] cols = new int[n];
86+
for (int i = 0; i < m; ++i) {
87+
for (int j = 0; j < n; ++j) {
88+
if (grid[i][j] == 1) {
89+
++rows[i];
90+
++cols[j];
91+
}
92+
}
93+
}
94+
int res = 0;
95+
for (int i = 0; i < m; ++i) {
96+
for (int j = 0; j < n; ++j) {
97+
if (grid[i][j] == 1) {
98+
if (rows[i] > 1 || cols[j] > 1) {
99+
++res;
100+
}
101+
}
102+
}
103+
}
104+
return res;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int countServers(vector<vector<int>>& grid) {
115+
int m = grid.size(), n = grid[0].size();
116+
vector<int> rows(m);
117+
vector<int> cols(n);
118+
for (int i = 0; i < m; ++i)
119+
{
120+
for (int j = 0; j < n; ++j)
121+
{
122+
if (grid[i][j] == 1)
123+
{
124+
++rows[i];
125+
++cols[j];
126+
}
127+
}
128+
}
129+
int res = 0;
130+
for (int i = 0; i < m; ++i)
131+
{
132+
for (int j = 0; j < n; ++j)
133+
{
134+
if (grid[i][j] == 1)
135+
{
136+
if (rows[i] > 1 || cols[j] > 1)
137+
{
138+
++res;
139+
}
140+
}
141+
}
142+
}
143+
return res;
144+
}
145+
};
146+
```
66147
148+
### **Go**
149+
150+
```go
151+
func countServers(grid [][]int) int {
152+
m, n := len(grid), len(grid[0])
153+
rows := make([]int, m)
154+
cols := make([]int, n)
155+
for i := 0; i < m; i++ {
156+
for j := 0; j < n; j++ {
157+
if grid[i][j] == 1 {
158+
rows[i]++
159+
cols[j]++
160+
}
161+
}
162+
}
163+
res := 0
164+
for i := 0; i < m; i++ {
165+
for j := 0; j < n; j++ {
166+
if grid[i][j] == 1 {
167+
if rows[i] > 1 || cols[j] > 1 {
168+
res++
169+
}
170+
}
171+
}
172+
}
173+
return res
174+
}
67175
```
68176

69177
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
int countServers(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
vector<int> rows(m);
6+
vector<int> cols(n);
7+
for (int i = 0; i < m; ++i)
8+
{
9+
for (int j = 0; j < n; ++j)
10+
{
11+
if (grid[i][j] == 1)
12+
{
13+
++rows[i];
14+
++cols[j];
15+
}
16+
}
17+
}
18+
int res = 0;
19+
for (int i = 0; i < m; ++i)
20+
{
21+
for (int j = 0; j < n; ++j)
22+
{
23+
if (grid[i][j] == 1)
24+
{
25+
if (rows[i] > 1 || cols[j] > 1)
26+
{
27+
++res;
28+
}
29+
}
30+
}
31+
}
32+
return res;
33+
}
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func countServers(grid [][]int) int {
2+
m, n := len(grid), len(grid[0])
3+
rows := make([]int, m)
4+
cols := make([]int, n)
5+
for i := 0; i < m; i++ {
6+
for j := 0; j < n; j++ {
7+
if grid[i][j] == 1 {
8+
rows[i]++
9+
cols[j]++
10+
}
11+
}
12+
}
13+
res := 0
14+
for i := 0; i < m; i++ {
15+
for j := 0; j < n; j++ {
16+
if grid[i][j] == 1 {
17+
if rows[i] > 1 || cols[j] > 1 {
18+
res++
19+
}
20+
}
21+
}
22+
}
23+
return res
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int countServers(int[][] grid) {
3+
int m = grid.length, n = grid[0].length;
4+
int[] rows = new int[m];
5+
int[] cols = new int[n];
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
if (grid[i][j] == 1) {
9+
++rows[i];
10+
++cols[j];
11+
}
12+
}
13+
}
14+
int res = 0;
15+
for (int i = 0; i < m; ++i) {
16+
for (int j = 0; j < n; ++j) {
17+
if (grid[i][j] == 1) {
18+
if (rows[i] > 1 || cols[j] > 1) {
19+
++res;
20+
}
21+
}
22+
}
23+
}
24+
return res;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def countServers(self, grid: List[List[int]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
rows = [0] * m
5+
cols = [0] * n
6+
for i in range(m):
7+
for j in range(n):
8+
if grid[i][j] == 1:
9+
rows[i] += 1
10+
cols[j] += 1
11+
res = 0
12+
for i in range(m):
13+
for j in range(n):
14+
if grid[i][j] == 1:
15+
if rows[i] > 1 or cols[j] > 1:
16+
res += 1
17+
return res

0 commit comments

Comments
 (0)