classSolution{public:intfindLonelyPixel(vector<vector<char>>&picture){constintm=picture.size();constintn=picture[0].size();intans=0;vector<int>rows(m);// rows[i] := # of Bs in rows ivector<int>cols(n);// cols[i] := # of Bs in cols ifor(inti=0;i<m;++i)for(intj=0;j<n;++j)if(picture[i][j]=='B'){++rows[i];++cols[j];}for(inti=0;i<m;++i)if(rows[i]==1)// Only have to examine the rows when rows[i] == 1for(intj=0;j<n;++j)// After we met the 'B' in this rows, we break and search the next rowif(picture[i][j]=='B'){if(cols[j]==1)++ans;break;}returnans;}};
classSolution{publicintfindLonelyPixel(char[][]picture){finalintm=picture.length;finalintn=picture[0].length;intans=0;int[]rows=newint[m];// rows[i] := # of Bs in rows iint[]cols=newint[n];// cols[i] := # of Bs in cols ifor(inti=0;i<m;++i)for(intj=0;j<n;++j)if(picture[i][j]=='B'){++rows[i];++cols[j];}for(inti=0;i<m;++i)if(rows[i]==1)// Only have to examine the rows when rows[i] == 1for(intj=0;j<n;++j)// After we met the 'B' in this rows, we break and search the next rowif(picture[i][j]=='B'){if(cols[j]==1)++ans;break;}returnans;}}
classSolution:deffindLonelyPixel(self,picture:List[List[str]])->int:m=len(picture)n=len(picture[0])ans=0rows=[0]*m# rows[i] := # Of Bs in rows icols=[0]*n# cols[i] := # Of Bs in cols iforiinrange(m):forjinrange(n):ifpicture[i][j]=='B':rows[i]+=1cols[j]+=1foriinrange(m):ifrows[i]==1:# Only have to examine the rows when rows[i] == 1forjinrange(n):# After we met the 'B' in this rows, we break and search the next rowifpicture[i][j]=='B':ifcols[j]==1:ans+=1breakreturnans