大家好,欢迎来到IT知识分享网。
 
一,sort排序的自定义排序方法
bool cmp(node a,node b) { 
    } struct cmp{ 
    bool operator() (const node & a,const node & b) { 
    } }; 
二,结构体的重载
里面两种都可以
struct node{ 
    int key; int value; int cnt; int time; // bool operator < (const node &a) const // { 
    // return cnt==a.cnt ? time<a.time : cnt<a.cnt; // } friend bool operator < (const node &a,const node b) { 
    return b.cnt==a.cnt ? a.time<b.time : a.cnt<b.cnt; } }; 
三,普通符号的重载
写在结构体外面的重载方法,这个就相当于把结构体中friend去掉。
bool operator < (const node &a,const node b) { 
    } 
struct cmp{ 
    bool operator() (const node & a,const node & b) { 
    } }; 
例子
NC140 排序
sort排序中自定义排序要写在类外面,不然会出错。
 bool cmp(int a,int b) { 
    return a<b; } class Solution { 
    public: vector<int> MySort(vector<int>& arr) { 
    // write code here sort(arr.begin(),arr.end(),cmp); return arr; } }; 
NC94 LFU缓存结构设计
set排序时自定的方法时要重载小于号。
 class Solution { 
    public: struct node{ 
    int key; int value; int cnt; int time; // bool operator < (const node &a) const // { 
    // return cnt==a.cnt ? time<a.time : cnt<a.cnt; // } friend bool operator < (const node &a,const node &b) { 
    return a.cnt!=b.cnt ? a.cnt<b.cnt :a.time<b.time; } }; map<int ,node> mp; set<node> st; int time=0; void set(int key,int value,int k) { 
    if(mp.count(key)==0) { 
    if(st.size()>=k) { 
    node a=*st.begin(); st.erase(st.begin()); mp.erase(a.key); } node b; b.key=key; b.value=value; b.time=time++; b.cnt=1; mp[key]=b; st.insert(b); } else { 
    node a=mp[key]; st.erase(a); a.value=value; a.cnt++; a.time=time++; st.insert(a); } } int get(int key) { 
    if(mp.count(key)) { 
    node a=mp[key]; st.erase(a); a.time=time++; a.cnt++; st.insert(a); mp[key]=a; return a.value; } else return -1; } vector<int> LFU(vector<vector<int> >& operators, int k) { 
    // write code here vector<int> v; if (operators.empty() || k <= 0) { 
    return v; } for(int i=0;i<operators.size();i++) { 
    if(operators[i][0]==1) { 
    set(operators[i][1],operators[i][2],k); } else if(operators[i][0]==2) { 
    v.push_back(get(operators[i][1])); } } return v; } }; 
NC97 字符串出现次数的TopK问题
优先队列排序时,自定义方法时重载小于号,还要定义一个结构体。
 class Solution { 
    public: struct cmp{ 
    bool operator () (const pair<string,int> &a,const pair<string,int> &b ) { 
    return a.second!=b.second ? a.second<b.second : a.first>b.first; } }; vector<vector<string> > topKstrings(vector<string>& strings, int k) { 
    // write code here vector<string> s=strings; //priority_queue< pair<string,int>,vector<pair<string,int> > ,cmp > q; priority_queue< pair<string,int>,vector<pair<string,int> > ,cmp > q; map<string,int> mp; vector<vector<string> > vec; for(int i=0;i<s.size();i++) { 
    mp[s[i]]++; } map<string ,int> ::iterator it=mp.begin(); for(;it!=mp.end();it++) { 
    q.push(make_pair(it->first,it->second) ); } for(int i=0;i<k;i++) { 
    vector<string> v; string str1=q.top().first; int x=q.top().second; // cout<<str1<<" "<<x<<endl; q.pop(); // string str2=""; //  // while(x) // { 
    // str2+=char(x%10+'0'); // x/=10; //  // } //  // reverse(str2.begin(),str2.end()); //  // cout<<str1<<" "<<str2<<endl; v.push_back(str1); v.push_back(to_string(x)); vec.push_back(v); } return vec; } }; 
两数之和
int cmp(pair<int,int> a,pair<int,int> b) { 
    //注意这里返回值是1,0,而不是1,-1,也就是返回值是真假两种,不为0就是真。 if(a.first<b.first) return 1; else return 0; } class Solution { 
    public: / * * @param numbers int整型vector * @param target int整型 * @return int整型vector */ vector<int> twoSum(vector<int>& numbers, int target) { 
    // write code here vector<pair<int,int> > v; for(int i=0;i<numbers.size();i++) { 
    v.push_back(make_pair(numbers[i],i+1)); } sort(v.begin(),v.end(),cmp); int l=0,r=numbers.size()-1; vector<int> vec; while(l<r) { 
    if(v[l].first+v[r].first==target) { 
    vec.push_back(min(v[l].second,v[r].second)); vec.push_back(max(v[l].second,v[r].second)); break; } else if(v[l].first+v[r].first>target) { 
    r--; } else { 
    l++; } } return vec; } }; 
                                                        免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/121114.html