class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int>hash;
for(int i=0;i<nums.size();i++)
{
if(hash.count(target-nums[i]))
return {hash[target-nums[i]],i};
hash[nums[i]]=i;
}
return {-1,-1};
}
};
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
vector<string> res;
unordered_map<string,int> hash;
for(int i=0;i+10<=s.size();i++)
{
string t=s.substr(i,10);
if(hash[t]==1) res.push_back(t);
hash[t]++;
}
return res;
}
};
解决冲突:1.拉链法 2 开发寻址法
class MyHashMap {
public:
/** Initialize your data structure here. */
int N=20011;
vector<list<pair<int,int>>> h;
MyHashMap() {
h=vector<list<pair<int,int>>>(N);
}
list<pair<int,int>>::iterator find(int key)
{
int t=key%N;
for(auto it=h[t].begin();it!=h[t].end();it++)
{
if(it->first ==key)
return it;
}
return h[t].end();
}
/** value will always be non-negative. */
void put(int key, int value) {
int t=key%N;
auto it=find(key);
if(it!=h[t].end()) it->second=value;
else h[t].push_back({key,value});
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int get(int key) {
int t=key%N;
auto it=find(key);
if(it==h[t].end()) return -1;
else return it->second;
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void remove(int key) {
int t=key%N;
auto it=find(key);
if(it!=h[t].end()) h[t].erase(it);
}
};
/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap* obj = new MyHashMap();
* obj->put(key,value);
* int param_2 = obj->get(key);
* obj->remove(key);
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int cnt=0;
unordered_map<string,int>hash;
unordered_map<int,int> cout;
vector<TreeNode*> res;
string dfs(TreeNode* root)
{
if(!root)
return to_string(hash["#"]);
auto left=dfs(root->left);
auto right=dfs(root->right);
string tree=to_string(root->val)+','+left+','+right;
if(hash.count(tree)==0) hash[tree]=++cnt;
int t=hash[tree];
cout[t]++;
if(cout[t]==2) res.push_back(root);
return to_string(t);
}
vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
hash["#"]=++cnt;
dfs(root);
return res;
}
};
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map<int,int>hash;
hash[0]=1;
int res;
for(int i=0,sum=0;i<nums.size();i++)
{
sum+=nums[i];
res+=hash[sum-k];
hash[sum]++;
}
return res;
}
};
并查集:
合并两个集合
判断两个点是否在同一个集合中
class Solution {
public:
vector<int> Father;
int findFather(int x)
{
if(Father[x]!=x) Father[x]=findFather(Father[x]);
return Father[x];
}
int findCircleNum(vector<vector<int>>& M) {
int n=M.size();
int res=n;
for(int i=0;i<n;i++)
Father.push_back(i);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(!M[i][j]) continue;
if(findFather(i)!=findFather(j))
{
res--;
Father[findFather(j)]=findFather(i);
}
}
}
return res;
}
};
class Solution {
public:
vector<int>p;
int find(int x)
{
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
int n=edges.size();
for(int i=0;i<=n;i++)
p.push_back(i);
for(auto e:edges)
{
int a=e[0],b=e[1];
if(find(a)==find(b)) return{a,b};
p[find(a)]=find(b);
}
return {-1,-1};
}
};
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string,int>hash;
typedef pair<int,string> PIS;
priority_queue<PIS> heap;
for(auto word:words)
hash[word]++;
for(auto temp:hash)
{
PIS t(-temp.second,temp.first);
heap.push(t);
if(heap.size()>k) heap.pop();
}
vector<string> res(k);
for(int i=k-1;i>=0;i--)
{
res[i]=heap.top().second;
heap.pop();
}
return res;
}
};