应用场景:提供更多信息
/*
* Template for using hash map to find duplicates.
* Replace ReturnType with the actual type of your return value.
*/
ReturnType aggregateByKey_hashmap(vector<Type>& keys) {
// Replace Type and InfoType with actual type of your key and value
unordered_map<Type, InfoType> hashtable;
for (Type key : keys) {
if (hashmap.count(key) > 0) {
if (hashmap[key] satisfies the requirement) {
return needed_information;
}
}
// Value can be any information you needed (e.g. index)
hashmap[key] = value;
}
return needed_information;
}
两数之和
题解:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
//两数之和 哈希映射版
unordered_map<int,int> hashmap;
//数组个数
int n = nums.size();
//遍历数组值
for(int i = 0;i<n;i++){
//使用数组值做键 索引值做值
//查找是否有target-hashmap[key]的值
if(hashmap.find(target-nums[i])!=hashmap.end()){
//hashmap.count(target-nums[i])>0)
//存在 返回索引
return {i,hashmap[target-nums[i]]};
}else{
//不存在 加入哈希映射中
hashmap[nums[i]] = i;
//hashmap.insert(make_pair(nums[i],i));
}
}
//出了for循环
return {};
}
};
同构字符串
题解:
class Solution {
public:
bool isIsomorphic(string s, string t) {
//一对一映射 目测只要相同的字母对应位置的字母相同即可
//得到s和t的长度
int s_len = s.length();
int t_len = t.length();
//不等必定不满足映射关系
if(s_len!=t_len){
return false;
}
unordered_map<char,char> hashmap;
//相等的情况下 以有相同字符的字符串的字符为键 另一个字符串字符为值
for(int i = 0;i<s_len;i++){
if(hashmap.count(s[i])>0){
//存在 查看之前的值和现值是否相同 不同则返回false
if(hashmap[s[i]] != t[i]){
return false;
}
}else{
//不存在 加入
hashmap[s[i]]=t[i];
}
}
hashmap.clear();
//没有事先判断哪个字符串有相同字符 所以 键值反着走一次
for(int i = 0;i<s_len;i++){
if(hashmap.count(t[i])>0){
//存在 查看之前的值和现值是否相同 不同则返回false
if(hashmap[t[i]] != s[i]){
return false;
}
}else{
//不存在 加入
hashmap[t[i]]=s[i];
}
}
//出了循环都没返回 则说明相同字母映射的字母都相同 满足映射关系
return true;
}
};
两个列表的最小索引总和
题解:
class Solution {
public:
vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
//有多个喜爱的餐厅时 选用索引和最小的那个 如果最小索引和有多个 则全部输出
//list1和list2的长度
int len1 = list1.size();
int len2 = list2.size();
//哈希映射
unordered_map<string,int> hashmap;
//先将list1的值加入哈希映射中
for(int i = 0;i<len1;i++){
hashmap.insert(make_pair(list1[i],i));
}
//索引值的和
int min_sum = len1+len2;
//遍历list2
for(int i = 0;i<len2;i++){
if(hashmap.count(list2[i])>0){
//存在共同餐厅 记录下索引和索引
if((i+hashmap[list2[i]])<min_sum){
min_sum = i+hashmap[list2[i]];
}
}
}
vector<string> result;
for(int i=0;i<len2;i++){
if(hashmap.count(list2[i])>0){
//将最小索引和对应的餐厅均加入数组
if((i+hashmap[list2[i]]) == min_sum){
result.push_back(list2[i]);
}
}
}
return result;
}
};
应用场景:按键聚合
模板:
/*
* Template for using hash map to find duplicates.
* Replace ReturnType with the actual type of your return value.
*/
ReturnType aggregateByKey_hashmap(vector<Type>& keys) {
// Replace Type and InfoType with actual type of your key and value
unordered_map<Type, InfoType> hashtable;
for (Type key : keys) {
if (hashmap.count(key) > 0) {
update hashmap[key];
}
// Value can be any information you needed (e.g. index)
hashmap[key] = value;
}
return needed_information;
}
字符串中的第一个唯一字符
题解:
class Solution {
public:
int firstUniqChar(string s) {
//字符为键 出现次数为值
unordered_map<char,int> hashmap;
//字符串长度
int n = s.length();
//使用一个数组保存
for(int i = 0;i<n;i++){
if(hashmap.count(s[i])>0){
//存在 更新
hashmap[s[i]]++;
}else{
hashmap[s[i]] = 1;
}
}
//遍历哈希映射
//记录下第一个不重复的字符
char c = '\0';
int min_index = n;
for(auto it = hashmap.begin();it!=hashmap.end();it++){
if((*it).second == 1){
c = (*it).first;
//找到字符对应的索引
string::iterator result = find(s.begin(),s.end(),c);
int index = distance(s.begin(), result);
if(index<n){
min_index = index;
}
}
}
//判断是否有
if(min_index == n){
return -1;
}else{
return min_index;
}
}
};
两个数组的交集II
题解:
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
//哈希映射
unordered_map<int,int> hashmap;
//数组的长度
int len1 = nums1.size();
int len2 = nums2.size();
//遍历数组1 加入哈希映射
for(int i = 0;i<len1;i++){
if(hashmap.count(nums1[i])>0){
//存在 值加1
hashmap[nums1[i]]++;
}else{
//不存在
hashmap[nums1[i]] = 1;
}
}
vector<int> result;
//遍历数组2
for(int i = 0;i<len2;i++){
if(hashmap.count(nums2[i])>0){
//存在
result.push_back(nums2[i]);
hashmap[nums2[i]]--;
if(hashmap[nums2[i]]<=0){
//最后一个了 应该删除
hashmap.erase(nums2[i]);
}
}
}
return result;
}
};
存在重复元素II
题解:
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
//哈希映射
unordered_map<int,int> hashmap;
//遍历数组
for(int i = 0;i<nums.size();i++){
//改值是否在哈希映射键中
if(hashmap.count(nums[i])>0){
//存在 索引值的差是否大于k
if(abs(hashmap[nums[i]]-i) <= k){
//小于
return true;
}else{
//大于k 则为以防后面还有重复值 更新新的索引值
hashmap[nums[i]] = i;
}
}else{
//不存在
hashmap[nums[i]] = i;
}
}
//出来了
return false;
}
};