LeetCode-哈希表

mid-2080 区间内查询数字的频率

子数组中一个值在给到下标范围内出现的次数
hash表 二分查找

#include <vector>
#include "map"
using namespace std;
//leetcode submit region begin(Prohibit modification and deletion)
class RangeFreqQuery {
public:
    map<int, vector<int>> map_valueToList; //map key-每个数字 value-出现的坐标组成的list
    RangeFreqQuery(vector<int>& arr) {
        for (int i = 0; i < arr.size(); i++) {
            vector<int> list;
            map<int, vector<int>>::iterator it;
            it = map_valueToList.find(arr[i]);
            if (it != map_valueToList.end()) {
                it->second.push_back(i);
            } else {
                list.push_back(i);
                map_valueToList.insert(map<int, vector<int>>::value_type(arr[i], list));
            }
        }
    }
    
    int query(int left, int right, int value) {
        map<int, vector<int>>::iterator it;
        it = map_valueToList.find(value);
        vector<int> list = it->second;
        //个数就是list中<right的最大值的index - list中>left的最小值的index + 1
        //找list中值 >= left的最小值的位置
        //二分
        int l = 0, r = list.size()-1;
        int min = 0, max = 0;
        while (l <= r) {
            int mid = r - (l + r)/2;
            if (list[mid] >= left) {
                min = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        //找list中值 <= right的最小值的位置
        l = 0, r = list.size()-1;
        while (l <= r) {
            int mid = r - (l + r)/2;
            if (list[mid] <= right) {
                max = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return max-min+1;
    }
};

mid-1366 通过投票对团队排名

hash表
排序

#include <vector>
#include "string"
#include "map"
#include "algorithm"
using namespace std;
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
    //map<char,list> key-字符 value-出现在list当前位置的次数
    //对map的value排序获取key
    static bool cmp(const pair<char, vector<int>>& v1, const pair<char, vector<int>>&v2) {
        for (int i = 0; i < v1.second.size(); i++) {
            if (v1.second[i] == v2.second[i]) continue;
            return v1.second[i] > v2.second[i];
        }
        return v1.first < v2.first; //评分list完全相同的,按字符升序排列
    }
    string rankTeams(vector<string>& votes) {
        map<char, vector<int>> mapCnt;
        for (int i = 0; i < votes.size(); i++) {
            int num = votes[0].size();
            for (int j = 0; j < num; j++) {
                vector<int> cnt(26);
                map<char, vector<int>>::iterator it;
                char ch = votes[i].at(j);
                it = mapCnt.find(ch);
                if (it != mapCnt.end()) {
                    it->second[j]++;
                } else {
                    cnt[j] = 1;
                    mapCnt.insert(map<char, vector<int>>::value_type(ch, cnt));
                }
            }
        }
        vector<pair<char, vector<int>>> arr(mapCnt.begin(), mapCnt.end());
        stable_sort(arr.begin(), arr.end(), cmp);//内部快排会交换位置,用stable_sort
        string res;
        for (int i = 0; i < arr.size(); i++) {
            res += arr[i].first;
        }
        return res;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值