C++: Map and Vector


  • 0
    P
    int findBlackPixel(vector<vector<char>>& picture, int N) {
        int count= 0; 
        vector<vector<char>>::iterator it1;
        vector<char>::iterator it2;
        vector<int> col;
        map<string, int> mymap;
        it1 = picture.begin();
        
        
        for(int i = 0; i< it1->size(); i++){
            col.push_back(0);
            
        }
       
        
        for (; it1 != picture.end(); it1++){
            if(N > picture.size() || N> it1->size()) return 0;
            string st;
            int ct = 0;
            int index =0;
            it2 = it1->begin();
            for(; it2 != it1->end(); it2++){
                st+= *it2;
                if (*it2 == 'B'){
                      
                  ct++;
                  col[index]++;
                 
                } 
                index++;
            }
            if(mymap.find(st) != mymap.end()){
                mymap[st] = mymap[st]+1;
            }else{
                if(ct == N)
                   mymap[st] = 1;
            }
        }
        count = 0;
        map<string, int>::iterator mapit;
        mapit = mymap.begin();
        int col_index = 0;
        for(; mapit != mymap.end(); mapit++){
            int count2 =0;
            if (mapit->second == N){
                for (int j=0; j <mapit->first.length(); j++){
                    if (mapit->first[j] == 'B'){
                        if (col[j] == N) count2++;
                    }
                }
                count += count2*N;
            } 
        }
      return count;  
    }

Log in to reply
 

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