c++ one pass solution


  • 0
    K
    class Solution {
    public:
        int findLonelyPixel(vector<vector<char>>& picture) {
            int n = picture.size();
            if(!n) return 0;
            int m = picture[0].size(), res = 0;
            vector<int> rows, cols(m); // rows store colum number of 'B' in a row that has only one 'B'
            for (int i = 0; i < n; ++i) {
                int lastB = 0, count = 0; // for each row, lastB store the last col number for a 'B' cell, count store how many 'B' in a row
                for (int j = 0; j < m; ++j) {
                    if (picture[i][j] == 'B') {
                        ++count;
                        lastB = j;
                        ++cols[j];
                    }
                }
                if (count == 1) rows.push_back(lastB);
            }
            for (auto &a : rows) if (cols[a] == 1) ++res;
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.