-
Notifications
You must be signed in to change notification settings - Fork 0
/
3128. 直角三角形(Accepted).cpp
88 lines (68 loc) · 2.25 KB
/
3128. 直角三角形(Accepted).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// class Solution {
// public:
// int checkForRow(vector<vector<int>>&grid,int i , int j, int n , int m){
// int count =0;
// for(int k=0;k<m;k++) if(k!=j && grid[i][k]==1) count++;
// return count;
// }
// int checkForCol(vector<vector<int>>&grid,int i , int j, int n ,int m){
// int count=0;
// for(int k=0;k<n;k++) if(i!=k && grid[k][j]==1) count++;
// return count;
// }
// long long numberOfRightTriangles(vector<vector<int>>& grid) {
// long long count =0;
// int n = grid.size();
// int m = grid[0].size();
// for(int i=0;i<n;i++){
// for(int j=0;j<m;j++){
// if(grid[i][j]==1){
// int row = checkForRow(grid,i,j,n,m);
// int col = checkForCol(grid,i,j,n,m);
// //cout<<row<<" "<<col<<endl;
// if(row>=1 && col>=1) count+= row * col;
// }
// }
// }
// return count;
// }
// };
class Solution {
public:
void fillRow(vector<int>&row,vector<vector<int>>&grid,int n , int m){
for(int i=0;i<n;i++){
int count =0;
for(int k=0;k<m;k++) if(grid[i][k]==1) count++;
row[i] = count;
}
return ;
}
void fillCol(vector<int>&col,vector<vector<int>>&grid,int n ,int m){
for(int j=0;j<m;j++){
int count=0;
for(int k=0;k<n;k++) if( grid[k][j]==1) count++;
col[j] = count;
}
return ;
}
long long numberOfRightTriangles(vector<vector<int>>& grid) {
long long count =0;
int n = grid.size();
int m = grid[0].size();
vector<int>row(n+1,0);
vector<int>col(m+1,0);
fillRow(row,grid,n,m);
fillCol(col,grid,n,m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]==1){
int r = row[i]-1;
int c = col[j]-1;
//cout<<r<<" "<<c<<endl;
if(r>=1 && c>=1) count+= r * c;
}
}
}
return count;
}
};