@@ -49,21 +49,129 @@ Return the number of servers that communicate with any other server.</p>
49
49
<li><code>grid[i][j] == 0 or 1</code></li>
50
50
</ul >
51
51
52
-
53
52
## Solutions
54
53
55
54
<!-- tabs:start -->
56
55
57
56
### ** Python3**
58
57
59
58
``` 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
61
76
```
62
77
63
78
### ** Java**
64
79
65
80
``` 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
+ ```
66
147
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
+ }
67
175
```
68
176
69
177
### ** ...**
0 commit comments