My C++ solution O(mn)


  • 0
    F

    class Solution { /*['W', 'W', 'B']
    ['W', 'B', 'W']

                     */
    

    public:
    int findLonelyPixel(vector<vector<char>>& picture) {
    int res=0;
    int hang = picture.size();
    int lie = picture[0].size();
    vector<char> tmp(hang,'W');
    vector<vector<char>> numlie(lie,tmp);
    for(int i=0;i<lie;i++){
    for(int j=0;j<hang;j++)
    numlie[i][j] = picture[j][i];
    }
    for(int i=0;i<hang;i++){
    int ind = countBisOne(picture[i]);
    if( ind != -1 ){
    vector<char> checklie = numlie[ind];
    if(countBisOne(checklie) == i ) res++;
    }
    }
    return res;
    }

    int countBisOne(vector<char> nums){
        int count = 0;
        int index = -1;
        for(int i=0;i<nums.size();i++){
            if(nums[i]=='B') {
                index = i;
                count++;
            }
        }
        if(count==1) return index;
        else return -1;
    }
    

    };


Log in to reply
 

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