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

相关推荐
weixin_457760002 小时前
OpenCV 图像处理基础算法详解(一)
图像处理·opencv·算法
做怪小疯子3 小时前
LeetCode 热题 100——链表——相交链表
算法·leetcode·链表
立志成为大牛的小牛4 小时前
数据结构——五十一、散列表的基本概念(王道408)
开发语言·数据结构·学习·程序人生·算法·散列表
杨福瑞4 小时前
数据结构:双向链表(3)
c语言·数据结构·链表
Coovally AI模型快速验证4 小时前
去噪扩散模型,根本不去噪?何恺明新论文回归「去噪」本质
人工智能·深度学习·算法·机器学习·计算机视觉·数据挖掘·回归
歌_顿4 小时前
attention、transform、bert 复习总结 1
人工智能·算法
MicroTech20255 小时前
MLGO微算法科技时空卷积与双重注意机制驱动的脑信号多任务分类算法
科技·算法·分类
txp玩Linux5 小时前
rk3568上解析webrtc音频降噪算法处理流程
算法·音视频·webrtc
立志成为大牛的小牛5 小时前
数据结构——五十二、散列函数的构造(王道408)
数据结构·笔记·程序人生·考研·算法