剑指 Offer II 哈希表

030. 插入、删除和随机访问都是 O(1) 的容器

hash存数组下标 删除时 思路清奇地使用swap交换要删除的值x与nums数组尾巴y 使得能O(1)pop_back

  unordered_map<int,int>m;
    vector<int>nums;
    /** Initialize your data structure here. */
    RandomizedSet() {

    }
    
    /** Inserts a xue to the set. Returns true if the set did not already contain the specified element. */
    bool insert(int x) {
        if(m.count(x)==0)
        {
            nums.push_back(x);
            m[x]=nums.size()-1;
            return true;
        }
        return false;

    }
    
    /** Removes a xue from the set. Returns true if the set contained the specified element. */
    bool remove(int x) {
        if(!m.count(x))return false;
        int y=nums.back();
        int px=m[x],py=m[y];

        swap(nums[px],nums[py]);
        swap(m[x],m[y]);
        nums.pop_back();
        m.erase(x);
        return true;

    }
    
    /** Get a random element from the set. */
    int getRandom() {
        return nums[rand()%nums.size()];
    }
};

好题031. 最近最少使用缓存

 struct Node
    {
        int key,val;
        Node *left,*right;
        Node(int _key,int _val): key(_key) ,val(_val),left(NULL),right(NULL){}
    }*L,*R;
    int n;
    unordered_map<int,Node*>hash;
    LRUCache(int capacity) {
        n=capacity;
        L=new Node(-1,-1);
        R=new Node (-1,-1);
        L->right=R;
        R->left=L;
    }
    
    void remove (Node *p)
    {
        p->left->right=p->right;
        p->right->left=p->left;
    
    }
    void insert(Node *p)
    {
        p->left=L;
        p->right=L->right;

        L->right->left=p;
        L->right=p;

    }
    int get(int key) {
        if(hash.count(key)==0)return -1;
        auto p=hash[key];
        remove(p);
        insert(p);
         return p->val;
    }
    
    void put(int key, int value) {
        if(hash.count(key))
        {
            auto p=hash[key];
            p->val=value;
            remove(p);
            insert(p);
        }
        else 
        {
             if(hash.size()==n)//彻底erase只会出现在这里
             {
                 auto p=R->left;
                 remove(p);
                 hash.erase(p->key);
                 delete p;
             }
             auto p=new Node(key,value);
             hash[key]=p;//别忘了hash
             insert(p);

        }

    }

032. 有效的变位词

class Solution {
    public boolean isAnagram(String s, String t) {
        if(s.equals(t))return false;
        if(s.length()!=t.length())return false;
        int a[]=new int [26];
         int b[]=new int [26];
         for(int i=0;i<s.length();i++)
         {
             int x=(int)s.charAt(i);
             a[x-'a']++;
               x=(int)t.charAt(i);
              b[x-'a']++;
           
         }   
         for(int i=0;i<26;i++)
         if(a[i]!=b[i])return false;

        return true;

    }
}

033. 变位词组

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
     
       List<List<String>>ans=new ArrayList<>();
        HashMap<String,List<String>>memo=new HashMap<>();
        for(String x:strs)
        {
           char []temp=x.toCharArray();
          Arrays.sort(temp);
           String t=new String (temp);
      // 这样不行???   memo.getOrDefault(t,new ArrayList<String> ()).add(x);
           if(!memo.containsKey(t))memo.put(t,new ArrayList<String> ());//建立空数组
         memo.get(t).add(x);
        }
        for(List<String>x :memo.values()) ans.add(x);       
        return ans;
    }
}

034. 外星语言是否排序

class Solution {
    HashMap <Character,Integer>memo =new HashMap<>();
    boolean check(String a,String b)
    {
        int n=a.length(),m=b.length();
        for(int i=0;i<Math.min(n,m);i++)
        {
            if(memo.get(a.charAt(i)) < memo.get(b.charAt(i)))return false;
            else if(memo.get(a.charAt(i)) > memo.get(b.charAt(i)))return true;
        }
        if(n>m)return true;
        return false;
    }
    public boolean isAlienSorted(String[] words, String order) {
        int n=words.length,t=0;
        for(char x :order.toCharArray()){
            memo.put(x,++t);
        }
        for(int i=0;i<n-1;i++)
        {
            if(check(words[i],words[i+1]))return false;
        }
        return true;
    }
}
posted @ 2022-03-03 22:39  liv_vil  阅读(28)  评论(0)    收藏  举报