本文主要是介绍carl 哈希表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.有效的字母异位词
class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {unordered_map<int, int> mymap;unordered_set<int> result;for (int i = 0; i < nums1.size(); i++){/* code */if (mymap.find(nums1[i])!=mymap.end()){mymap[nums1[i]]++;}else{mymap[nums1[i]]=1;}}for (int i = 0; i < nums2.size(); i++){if (mymap.find(nums2[i])!=mymap.end()){result.insert(nums2[i]);}}return vector<int>(result.begin(),result.end());}
};
2.两个数组的交集
class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {unordered_map<int, int> mymap;unordered_set<int> result;for (int i = 0; i < nums1.size(); i++){/* code */if (mymap.find(nums1[i])!=mymap.end()){mymap[nums1[i]]++;}else{mymap[nums1[i]]=1;}}for (int i = 0; i < nums2.size(); i++){if (mymap.find(nums2[i])!=mymap.end()){result.insert(nums2[i]);}}return vector<int>(result.begin(),result.end());}
};
3.快乐数
class Solution {
public:bool isHappy(int n) {unordered_set<int> myset;int sum = 0;while (n!=1){sum = 0;int tmp = n;//计算各位的平方和while (n!=0){sum =sum + (n % 10) * (n % 10);n = n / 10;}if (myset.find(sum)!=myset.end()){return false;}else{myset.insert(sum);}n = sum;}return true;}
};
4.两数之和
class Solution {
public:vector<int> twoSum(vector<int> &nums,int target){unordered_map<int,int> mymap;for (int i = 0; i < nums.size(); i++){if (mymap.find(nums[i])==mymap.end()){mymap[nums[i]]=i;}}for (int i = 0; i < nums.size(); i++){if (mymap.find(target-nums[i])!=mymap.end()){/* code */auto temp=mymap.find(target-nums[i]);if (i==temp->second){continue;}return vector<int>{i,temp->second};}}return vector<int>();}
};
5.四数相加
时间有点点超了 下面是改进的
class Solution
{
public:int fourSumCount(vector<int> &nums1, vector<int> &nums2, vector<int> &nums3, vector<int> &nums4){unordered_map<int,int> mymap;int count=0;for (int i = 0; i < nums1.size(); i++){for (int j = 0; j < nums2.size(); j++){if (mymap.count(nums1[i] + nums2[j]) == 0){mymap[nums1[i] + nums2[j]] =1;}else{auto temp = mymap.find(nums1[i] + nums2[j]);temp->second++;}}}for (int i = 0; i < nums3.size(); i++){for (int j = 0; j < nums4.size(); j++){auto temp=mymap.find(-(nums3[i] + nums4[j]));if (temp!=mymap.end()){count+=temp->second;}}}return count;}
};
6、赎金信
class Solution
{
public:bool canConstruct(string ransomNote,string magazine){int temp[26];for (int i = 0; i < 26; i++){temp[i]=0;}for (int i = 0; i < magazine.size(); i++){temp[magazine[i]-'a']++;}for (int i = 0; i < ransomNote.size(); i++){temp[ransomNote[i]-'a']--;}for (int i = 0; i < 26; i++){if (temp[i]<0){return false;}}return true; }
};
7.三数之和 不会
class Solution
{
public:vector<vector<int>> threeSum(vector<int> &nums){vector<vector<int>> result;sort(nums.begin(), nums.end());for (int i = 0; i < nums.size(); i++){if (nums[i] > 0){return result;}if (i > 0 && nums[i] == nums[i - 1]){/* code */continue;}int left = i + 1;int right = nums.size() - 1;while (right > left){if (nums[i] + nums[left] + nums[right] > 0)right--;else if (nums[i] + nums[left] + nums[right] < 0)left++;else{result.push_back(vector<int>{nums[i], nums[left], nums[right]});while ((right > left && nums[right] == nums[right - 1]))right--;while ((right > left && nums[left] == nums[left + 1]))left++;right--;left++;}}}return result;}
};
8.四数之和 抄的 基础是三数 先确定前两个之和
class Solution
{
public:vector<vector<int>> fourSum(vector<int> &nums, int target){vector<vector<int>> result;sort(nums.begin(), nums.end());for (int k = 0; k < nums.size(); k++){// 初步剪枝if (nums[k] > target && nums[k] >= 0){break;}// 对首元素去重if (k > 0 && nums[k] == nums[k - 1]){/* code */continue;}for (int i = k + 1; i < nums.size(); i++){// 二级剪枝if (nums[k] + nums[i]>target && nums[k] + nums[i]>=0){break;}//对nums[i]去重if (i > k+1 && nums[i] == nums[i - 1]){/* code */continue;}int left = i + 1;int right = nums.size() - 1;while (right > left){if ((long)nums[k]+nums[i] + nums[left] + nums[right] > target)right--;else if ((long)nums[k]+nums[i] + nums[left] + nums[right] < target)left++;else{result.push_back(vector<int>{nums[k],nums[i], nums[left], nums[right]});while ((right > left && nums[right] == nums[right - 1]))right--;while ((right > left && nums[left] == nums[left + 1]))left++;right--;left++;}}}}return result;}
};
这篇关于carl 哈希表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!