使用C++ STL中的優先隊列
因為創建的優先隊列默認建立的是大頂堆。
所以我們最后存放的答案數組還需要進行一次反轉。
class Solution {
public:vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {priority_queue<pair<int,int>> q;int m = mat.size();int n = mat[0].size();for(int i = 0;i < m;i++){int cnt = 0;for(int j = 0;j < n;j++){if(mat[i][j] == 0) break;cnt++;}q.push({cnt,i});}while(q.size() > k) q.pop();vector<int> res;while(q.size()){res.emplace_back(q.top().second);q.pop();}reverse(res.begin(),res.end());return res;}
};
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态