算法——冗余!哈希表、vector、string适配器的混合使用


❀保持低旋律节奏->个人主页

专栏链接:《C++学习》、《Linux学习》



本章讲解三个小题,重点在于 unordered_map的使用,其涉及到底层适配器的混合使用,通过练习这三道题对于vecotr、string、hash、适配器是一个非常不错的锻炼方式。

文章目录

题一、前K个高频单词

原题目链接

cpp 复制代码
class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
    
    unordered_map<string,int>mp;
    for(const string&s:words)
    {
        mp[s]++;
    }

    vector<pair<string,int>>mp_to_vector;
    //Q4 为什么这里 const pair&p就错误?
    for(const auto&p:mp)
    //Q1{}的使用
    {
        mp_to_vector.push_back({p.first,p.second});
    }

    //Q2核心错误 直接将mp进行排序,正确代码应该是将mp存放到vector里面去 然后再进行排序
    sort(mp_to_vector.begin(),mp_to_vector.end(),[](const pair<string,int>&p1,const pair<string,int>&p2)
    {
        if(p1.second!=p2.second)
        return p1.second>p2.second;
        else
        return p1.first<p2.first;
    });
    //Q3排序的使用


    vector<string>ans;
    for(int i = 0;i<k;i++)
    {
         ans.push_back(mp_to_vector[i].first);
    }
    return ans;
    








//
    }
};

题二、两句话中不常见的单词

原题目链接

cpp 复制代码
class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
    
    unordered_map<string,int>mp;
    for(const string&s:words)
    {
        mp[s]++;
    }

    vector<pair<string,int>>mp_to_vector;
    //Q4 为什么这里 const pair&p就错误?
    for(const auto&p:mp)
    //Q1{}的使用
    {
        mp_to_vector.push_back({p.first,p.second});
    }

    //Q2核心错误 直接将mp进行排序,正确代码应该是将mp存放到vector里面去 然后再进行排序
    sort(mp_to_vector.begin(),mp_to_vector.end(),[](const pair<string,int>&p1,const pair<string,int>&p2)
    {
        if(p1.second!=p2.second)
        return p1.second>p2.second;
        else
        return p1.first<p2.first;
    });
    //Q3排序的使用


    vector<string>ans;
    for(int i = 0;i<k;i++)
    {
         ans.push_back(mp_to_vector[i].first);
    }
    return ans;
   
//
    }
};

题三、单词识别

原题目链接

cpp 复制代码
#include<iostream>
#include<vector>
#include<string>
#include<unordered_map>//哈希
#include<algorithm>//sort
#include<utility>//pair
#include<cctype>//tolower/toupper
using namespace std;

//toLowerCase函数------字符串大写转小写
string toLowerCase(const string& s) {
    string ans;
    for (auto x : s) {
        if (isupper(x))
            ans.push_back(tolower(x));
        else
            ans.push_back(x);
    }
    return ans;
}


//word函数------实现string转为有序可排列的vector
vector<string>Sort_String(const string& s) {
    vector<string>v;
    int start = 0;
    int end = 0;
    int sz = s.size();


    for (end = 0; s[end] != '.'; end++) {
        if (s[end] == ' ') {
            //Q1substr使用是一个小坑
            string tmp = s.substr(start, end - start);
            //转为小写
            string low_tmp = toLowerCase(tmp);
            v.push_back(low_tmp);
            start = end + 1;
        }
    }
    string tmp = s.substr(start, end - start);
    v.push_back(tmp);
    return v;
}


//Sort_My_String函数------字符数组转化为unordered_map------unordered_map 转化为可以排序 遍历的vector<pair<string,int>>
vector<pair<string, int>> Sorted_My_String(const vector<string>& v) {
    unordered_map<string, int> mp;
    for (const string& s : v) {
        mp[s]++;
    }
    vector<pair<string, int>>ans;

    //Q2 思考 这样写就错误------for (const pair& p : mp)
    //这样写就对------for const<const auto&p:mp>------const pair<string,int>&p : mp

    for (const pair<string, int>& p : mp) {
        //Q3思考中括号的作用是什么
        ans.push_back({ p.first, p.second });
    }

    //Q4思考自定义排序规则 如何实现的 []作用是什么
    sort(ans.begin(), ans.end(), [](const pair<string, int>& a,
    const pair<string, int>& b) {
        if (a.second != b.second)
            return a.second > b.second;
        else
            return a.first < b.first;
    }
        );
    return ans;
}



int main() {
    string s;
    getline(cin, s);
    vector<string>v = Sort_String(s);
    vector<pair<string, int>>ans = Sorted_My_String(v);
    for (const auto& p : ans) {
        cout << p.first << ":" << p.second << endl;
    }
    return 0;
}

{}初始聚合化


Lambda表达式

相关推荐
Evand J23 分钟前
【电机滤波例程1】PMSM永磁同步电机转速与位置估计,MATLAB程序:标准无迹卡尔曼滤波、模型说明与仿真验证
算法·matlab·电机·ekf·负载·卡尔曼滤波
打码的老程是远篁41 分钟前
手把手教你玩转大模型——3. Transformer 的位置编码——模型怎么知道谁在前,谁在后?
人工智能·算法
嘎嘎风42 分钟前
MiniMind 学习笔记之 06 一张图、一百行:MiniMind 的骨架
算法·源码
用户7341681035481 小时前
只盯被动响应做状态监测?论“控制信号”在变工况故障诊断中的重要作用
算法
Ivanqhz1 小时前
MQA、GQA、稀疏/滑动窗口注意力及混合范式
人工智能·算法·机器学习
罗湖老棍子1 小时前
荒岛野人(信息学奥赛一本通- P1637)(洛谷-P2421)
算法·数论·枚举·裴蜀定理·扩展欧几里得·扩展中国剩余定理
朝朝辞暮i1 小时前
C++ 第 40 章:ROS2 Publisher + Timer + Subscriber + Callback + Executor 完整闭环
开发语言·c++·算法·ros2
400分1 小时前
具身智能系列之从零到「机械臂会放试管」:π0.5 VLA 真机部署完全指南【第一节】
算法
小宋10211 小时前
一套服务托管多个 LoRA:适配器加载、租户隔离与热切换实战
大数据·人工智能·算法
Rosanci1 小时前
Codex 下载与本地部署实战:从安装到运行全流程指南
开发语言·前端·算法·chatgpt·codex