week7

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;   
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值