Group the People Given the Group Size They Belong To(C++用户分组)

解题思路:

(1)使用map将组大小相同的人存放在一起

(2)再从map中根据键,以组大小为间隔取

class Solution {
public:
    vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
        unordered_map<int,vector<int>> mp;
        for(int i=0;i<groupSizes.size();i++) {
            mp[groupSizes[i]].push_back(i);
        }
        
        vector<vector<int>> vec;
        vector<int> v;
        unordered_map<int,vector<int>>::iterator it;
        for(it=mp.begin();it!=mp.end();it++) {
            for(int i=0;i<it->second.size();i++) {
                v.push_back(it->second[i]);
                if(i%it->first==it->first-1) {
                    vec.push_back(v);
                    v.clear();
                }
            }
        }
        return vec;
    }
};

 

上一篇:Count 1 in Binary


下一篇:LeetCode:1389. Create Target Array in the Given Order按既定顺序创建目标数组(C语言)