8.17 哈希表中等 347 Top K Frequent Elements review 128 451

347 Top K Frequent Elements

在这里插入图片描述

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        //是前k个高频 the k most frequent elements 的元素
        vector<int> res;
        //哈希表用于中间态
        unordered_map<int,int> count;
       
        //核心在于/难点:前k个元素如何提取
        //题目中不管是怎么顺序,只要是前k个频次的数就行,而且关于的前k个肯定是需要排序的
        for(int i = 0 ; i < nums.size();i++){
            count[nums[i]]++;
        }
        vector<pair<int,int>> freq(count.begin(),count.end());
        sort(freq.begin(),freq.end(), [](const pair<int,int>&a,const pair<int,int>&b){
        return a.second>b.second;
    });
        for(int i = 0 ; i < k ; i++){
            res.push_back(freq[i].first);
        }
        return res;
    }
};

review 128 Longest Consecutive Sequence【哈希表】

//第一次提交
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        //哈希表先记下来,用于查找连续值是否存在于nums中
        int n = nums.size();
        unordered_set<int> hash(nums.begin() , nums.end());
        //查找且一次遍历
        int maxLen = 0;
        for(int i = 0; i < n ; i++){
            int num = nums[i];
            int len = 0;
            while(hash.find(num++) != hash.end()){
                len++;
            }
            //比较
            maxLen = std::max(len ,maxLen);
        }
        return maxLen;
    }
};

问题 :时间超时,减少嵌套循环,肯定是查找那部分出了问题
没有独立完成!!周日了懈怠了,脑子转不动了,明天休息(づ ̄ 3 ̄)づ

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        //哈希表先记下来,用于查找连续值是否存在于nums中
        int n = nums.size();
        unordered_set<int> hash(nums.begin() , nums.end());
        //查找且一次遍历
        int maxLen = 0;
        for(int n : hash){
            //如果是起点,就直接查完,如果不是跳过
            if(hash.find(n-1) == hash.end()){
                int len = 1 ;
                int curr =n ;
                while(hash.find(curr+1) != hash.end()){
                    curr++;
                    len++;
                }
                maxLen = max(maxLen,len);
            }
        }
        return maxLen;
    }
};

review 451 Sort Characters By Frequency【排序】

注意string.append()函数的参数区分!!

std::string str = "a";
str.append(5, 'b'); // 追加 "bbbbb"
//这会将 5 个 'b' 字符追加到 str 中,结果是 "abbbbb"。
std::string str = "Hello";
str.append(" World", 3); // 追加 " Wo"
//这会将 " World" 的前 3 个字符追加到 str 后面,结果是 "Hello Wo"。
class Solution {
public:
    string frequencySort(string s) {
        //记录字母频率按顺序?
        //递减序列 基于chara出现频率 
        //计数
        unordered_map<char,int> count;
        for(char ch :s){
            count[ch]++;
        }
        //排序 将哈希表转为vector<pair<char,int>> + sigma函数
        vector<pair<char,int>> freq(count.begin(),count.end());
        sort(freq.begin() , freq.end() , [](const pair<char,int>& a,const pair<char,int>& b){
            return a.second > b.second;
        });
        //输出
        string res = "";
        for(const auto p: freq){
           res.append(p.second, p.first);
        }
        return res;
    }
};
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值