LeetCode hot 100 day1
1. 两数之和
题目链接
关于 unordered_map 的总结
1. key 的唯一性:
在 unordered_map 中,key 必须是唯一的,即每个 key 只能出现一次。如果你尝试插入重复的 key,新的值会覆盖已存在的值。
2. 插入重复 key 的行为:
当插入一个已有的 key 时,unordered_map 会覆盖原来对应的 value。
3. 如何检查 key 是否已存在:
你可以使用 unordered_map 提供的 find() 方法来检查一个 key 是否已经存在。如果 find() 返回 end(),则表示该 key 不存在
是根据key来查找 而不是根据value;
unordered_map 常用方法总结
unordered_map 是 C++ 标准库中提供的一个关联容器,它使用哈希表来存储键值对,能够提供快速的插入、查找和删除操作。下面是 unordered_map 的常用方法及示例:
- 插入元素:
insert():将一个元素插入到 unordered_map 中。
unordered_map<int, string> umap;
umap.insert({1, "apple"}); // 插入 {key=1, value="apple"}
- 访问元素:
运算符:通过 key 获取对应的 value,如果 key 不存在,会插入该 key 的默认值。
unordered_map<int, string> umap;
umap[1] = "apple"; // 直接通过 key 访问或插入
cout << umap[1]; // 输出 "apple"
- 查找元素:
find():查找一个元素,如果找到,返回指向该元素的迭代器,否则返回 end()。
unordered_map<int, string> umap;
umap[1] = "apple";auto it = umap.find(1); // 查找 key = 1
if (it != umap.end()) {cout << "Found, value: " << it->second << endl; // 输出 "Found, value: apple"
} else {cout << "Not found." << endl;
}
- 删除元素:
erase():删除指定的元素。
unordered_map<int, string> umap;
umap[1] = "apple";
umap.erase(1); // 删除 key=1 的元素
- 检查元素是否存在:
count():返回 key 的出现次数(对于 unordered_map,返回值是 0 或 1)。
unordered_map<int, string> umap;
umap[1] = "apple";if (umap.count(1)) { // 如果 key = 1 存在cout << "Key 1 exists." << endl;
} else {cout << "Key 1 does not exist." << endl;
}
- 获取容器的大小:
size():返回 unordered_map 中的元素个数。
unordered_map<int, string> umap;
umap[1] = "apple";
umap[2] = "banana";
cout << "Size: " << umap.size() << endl; // 输出 Size: 2
- 清空容器:
clear():删除 unordered_map 中的所有元素。unordered_map<int, string> umap;
umap[1] = "apple";
umap.clear(); // 清空所有元素
cout << "Size after clear: " << umap.size() << endl; // 输出 Size after clear: 0
- 遍历容器:
for 循环:通过迭代器遍历 unordered_map 中的所有元素。
unordered_map<int, string> umap;
umap[1] = "apple";
umap[2] = "banana";for (auto it = umap.begin(); it != umap.end(); ++it) {cout << "Key: " << it->first << ", Value: " << it->second << endl;
}
- 获取桶数量(哈希桶数量):
bucket_count():返回哈希表中的桶数量。
unordered_map<int, string> umap;
umap[1] = "apple";
umap[2] = "banana";
cout << "Bucket count: " << umap.bucket_count() << endl;
AC
class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int, int> mp; // 创建一个哈希表,用于存储数字和它们的索引vector<int> ans; // 用于存储最终答案// 第一遍遍历数组 nums,将每个元素及其索引存储到哈希表 mp 中// mp[nums[i]] = i 表示数字 nums[i] 对应的索引是 ifor (int i = 0; i < nums.size(); i++) {mp[nums[i]] = i; // 存储每个数字及其对应的索引}// 第二遍遍历数组 nums,寻找两个数的和等于 targetfor (int i = 0; i < nums.size(); i++) {int k = target - nums[i]; // 计算出所需的另一个数字 k// 如果哈希表中存在数字 k,说明找到了两个数if (mp.find(k) != mp.end()) {auto it = mp.find(k); // 查找数字 k 的索引// 检查 k 对应的索引是否与当前的 i 不同,避免用同一个数两次if (it->second != i) {// 找到结果,返回索引ans.push_back(it->second); // 将数字 k 的索引加入答案ans.push_back(i); // 将当前数字 nums[i] 的索引加入答案return ans; // 直接返回答案}}}return ans; // 如果没有找到合适的组合,返回空的答案}
};
49. 字母异位词分组
题目链接
暴力解法会超时
class Solution {
public:vector<vector<string>> groupAnagrams(vector<string>& strs) {vector<vector<string>> ans;vector<string> path;vector<bool> visited(strs.size(),false);for(int i=0;i<strs.size();i++){if(visited[i]) continue;string p=strs[i];path.push_back(strs[i]);sort(p.begin(),p.end());for(int j=i+1;j<strs.size();j++){string k=strs[j];sort(k.begin(),k.end());if(p==k){path.push_back(strs[j]);visited[j]=true;}}ans.push_back(path);path.clear();}return ans;}
};
哈希表
class Solution {
public:vector<vector<string>> groupAnagrams(vector<string>& strs) {vector<vector<string>> ans; // 用于存储最终的字母异位词分组unordered_map<string, vector<string>> mp; // 哈希表,存储每个排序后的字符串及其对应的字母异位词组// 遍历每个输入的字符串for (int i = 0; i < strs.size(); i++) {string p = strs[i]; // 获取当前字符串sort(p.begin(), p.end()); // 将字符串排序,得到它的“标准形式”// 将排序后的字符串作为key,将原始字符串加入到对应的字母异位词组中mp[p].push_back(strs[i]);}// 遍历哈希表,将每个字母异位词组(值)加入到结果列表中for (auto& it : mp) {ans.push_back(it.second); // it.second 是一个字母异位词组(vector<string>)}// 返回结果,包含所有的字母异位词分组return ans;}
};