标签:pre nod class ++ res css 结构体排序 vector 解释
class Solution { public: typedef struct node { int val; int index; }; static bool cmp(Solution::node a,Solution::node b){ return a.val==b.val?a.index<b.index:a.val<b.val; } vector<int> kWeakestRows(vector<vector<int>> &mat, int k) { int len=mat.size(); node res[len]; for (int i = 0; i < len; i++) { int cnt = 0; for (int j = 0; j < mat[0].size(); j++) { if (mat[i][j]) cnt++; } res[i].index=i; res[i].val=cnt; } sort(res,res+len,cmp); vector<int>v; for(int i=0;i<k;i++){ v.push_back(res[i].index); } return v; } };
class Solution { public: struct node { int val; int index; bool operator<(const node &b){ return val==b.val?index<b.index:val<b.val; } }; vector<int> kWeakestRows(vector<vector<int>> &mat, int k) { int len=mat.size(); node res[len]; for (int i = 0; i < len; i++) { int cnt = 0; for (int j = 0; j < mat[0].size(); j++) { if (mat[i][j]) cnt++; } res[i].index=i; res[i].val=cnt; } sort(res,res+len); vector<int>v; for(int i=0;i<k;i++){ v.push_back(res[i].index); } return v; } };
标签:pre nod class ++ res css 结构体排序 vector 解释
原文地址:https://www.cnblogs.com/52dxer/p/12540214.html