算法——冗余!哈希表、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表达式

相关推荐
GreenTea7 小时前
vLLM 与 SGLang KV Cache 底层实现机制深度调研报告
前端·后端·算法
Bode_20027 小时前
多资源规划(含生产调度、库存管理及产能规划)的创新优化算法
算法·调度
Logic1018 小时前
C语言/数据结构位运算题解:异或XOR找出蛋糕订单中的“VIP独特口味“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
土司大王8 小时前
LeetCode hot100——35.搜索插入位置:Java 二分模板、左闭右开区间与插入点分析
java·算法·leetcode
微三云生态系统架构师-彭丹8 小时前
远方好物S2B2C系统架构:一级分销与保证金托管的合规技术实现
人工智能·算法
土司大王8 小时前
LeetCode hot100——34.在排序数组中查找元素的第一个和最后一个位置:Java 二分模板、边界分析
java·算法·leetcode
Logic1019 小时前
C语言/数据结构位运算题解:异或XOR找出飞行棋中的“独特颜色棋子“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
爱吃苹果的日记本9 小时前
数据结构第三课(时间复杂度)
数据结构·学习
罗西的思考9 小时前
机器人模型(WM / WAM / VLA)综合分析与对比:从「看」到「想」再到「做」
人工智能·算法·机器学习
Logic10110 小时前
C语言/数据动态规划题解:翻转一次子数组后的最大子数组和——四状态DP(O(n)时间O(1)空间)
c语言·数据结构·动态规划·时间复杂度·算法题·状态机dp·最大子数组和