目录
1--有效的字母异位词
2--两个数组的交集
3--两数之和
4--四数相加II
5--三数之和
6--四数之和
1--有效的字母异位词
利用哈希表存储每个字母的出现次数,比较两个字符串各个字母出现次数是否相等即可;
#include <iostream>
#include <string>
#include <vector>
class Solution {
public:
bool isAnagram(std::string s, std::string t) {
// 用数组实现哈希表
std::vector<int> hash(26, 0);
for(auto i = 0; i < s.length(); i++){
hash[s[i] - 'a']++;
}
// 遍历字符串t
for(auto i = 0; i < t.length(); i++){
hash[t[i] - 'a']--;
}
// 遍历哈希表
for(int i = 0; i < 26; i++){
if(hash[i] != 0) return false;
}
return true;
}
};
int main(int argc, char argv[]){
// s = "anagram", t = "nagaram"
std::string s = "anagram", t = "nagaram";
Solution S1;
bool res = S1.isAnagram(s, t);
if(res) std::cout << "true" << std::endl;
else std::cout << "false" << std::endl;
return 0;
}
2--两个数组的交集
利用哈希表存储数组1的元素,接着遍历数组2并判断元素是否存储在哈希表中,将交集元素存储并返回;
#include <iostream>
#include <unordered_set>
#include <vector>
class Solution {
public:
std::vector<int> intersection(std::vector<int>& nums1, std::vector<int>& nums2) {
std::unordered_set<int> hash1;
for(int i = 0; i < nums1.size(); i++){
hash1.insert(nums1[i]);
}
std::unordered_set<int> result;
for(int i = 0; i < nums2.size(); i++){
if(hash1.find(nums2[i]) != hash1.end()){
result.insert(nums2[i]);
}
}
std::vector<int> res;
for(auto item : result){
res.push_back(item);
}
return res;
}
};
int main(int argc, char argv[]){
// nums1 = [1,2,2,1], nums2 = [2,2]
std::vector<int> nums1 = {1, 2, 2, 1}, nums2 = {2, 2};
Solution S1;
std::vector<int> res = S1.intersection(nums1, nums2);
for(auto item : res) std::cout << item << " ";
std::cout << std::endl;
return 0;
}
3--两数之和
利用哈希表存储,key 为 target - nums[i],value 为 i;遍历数组判断当前nums[i]是否在哈希表中出现,返回匹配的两个结果对应的索引即可;
#include <iostream>
#include <unordered_map>
#include <vector>
class Solution {
public:
std::vector<int> twoSum(std::vector<int>& nums, int target) {
std::vector<int> res;
std::unordered_map<int, int> hash;
for(int i = 0; i < nums.size(); i++){
if(hash.find(nums[i]) != hash.end()){
res.push_back(hash[nums[i]]);
res.push_back(i);
return res;
}
hash.emplace(target - nums[i], i);
}
return res;
}
};
int main(int argc, char argv[]){
// nums = [2,7,11,15], target = 9
std::vector<int> nums = {2, 7, 11, 15};
int target = 9;
Solution S1;
std::vector<int> res = S1.twoSum(nums, target);
for(auto item : res) std::cout << item << " ";
std::cout << std::endl;
return 0;
}
4--四数相加II
利用哈希表,其中key为两个数组对应元素的和,value为出现的次数;接着遍历剩下两个数组的元素和是否在哈希表中出现,记录出现的次数即匹配结果;
#include <iostream>
#include <unordered_map>
#include <vector>
class Solution {
public:
int fourSumCount(std::vector<int>& nums1, std::vector<int>& nums2, std::vector<int>& nums3, std::vector<int>& nums4) {
std::unordered_map<int, int> hash;
for(int i = 0; i < nums1.size(); i++){
for(int j = 0; j < nums2.size(); j++){
hash[(nums1[i] + nums2[j])] ++;
}
}
int res = 0;
for(int i = 0; i < nums3.size(); i++){
for(int j = 0; j < nums3.size(); j++){
if(hash.find(0 - (nums3[i] + nums4[j])) != hash.end()){
res += hash[0 - (nums3[i] + nums4[j])];
}
}
}
return res;
}
};
int main(int argc, char argv[]){
// nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
std::vector<int> nums1 = {1, 2}, nums2 = {-2, -1}, nums3 = {-1, 2}, nums4 = {0, 2};
Solution S1;
int res = S1.fourSumCount(nums1, nums2, nums3, nums4);
std::cout << res << std::endl;
return 0;
}